linux/drivers/net/wireless/libertas/cfg.c
<<
>>
Prefs
   1/*
   2 * Implement cfg80211 ("iw") support.
   3 *
   4 * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
   5 * Holger Schurig <hs4233@mail.mn-solutions.de>
   6 *
   7 */
   8
   9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10
  11#include <linux/hardirq.h>
  12#include <linux/sched.h>
  13#include <linux/wait.h>
  14#include <linux/slab.h>
  15#include <linux/ieee80211.h>
  16#include <net/cfg80211.h>
  17#include <asm/unaligned.h>
  18
  19#include "decl.h"
  20#include "cfg.h"
  21#include "cmd.h"
  22#include "mesh.h"
  23
  24
  25#define CHAN2G(_channel, _freq, _flags) {        \
  26        .band             = IEEE80211_BAND_2GHZ, \
  27        .center_freq      = (_freq),             \
  28        .hw_value         = (_channel),          \
  29        .flags            = (_flags),            \
  30        .max_antenna_gain = 0,                   \
  31        .max_power        = 30,                  \
  32}
  33
  34static struct ieee80211_channel lbs_2ghz_channels[] = {
  35        CHAN2G(1,  2412, 0),
  36        CHAN2G(2,  2417, 0),
  37        CHAN2G(3,  2422, 0),
  38        CHAN2G(4,  2427, 0),
  39        CHAN2G(5,  2432, 0),
  40        CHAN2G(6,  2437, 0),
  41        CHAN2G(7,  2442, 0),
  42        CHAN2G(8,  2447, 0),
  43        CHAN2G(9,  2452, 0),
  44        CHAN2G(10, 2457, 0),
  45        CHAN2G(11, 2462, 0),
  46        CHAN2G(12, 2467, 0),
  47        CHAN2G(13, 2472, 0),
  48        CHAN2G(14, 2484, 0),
  49};
  50
  51#define RATETAB_ENT(_rate, _hw_value, _flags) { \
  52        .bitrate  = (_rate),                    \
  53        .hw_value = (_hw_value),                \
  54        .flags    = (_flags),                   \
  55}
  56
  57
  58/* Table 6 in section 3.2.1.1 */
  59static struct ieee80211_rate lbs_rates[] = {
  60        RATETAB_ENT(10,  0,  0),
  61        RATETAB_ENT(20,  1,  0),
  62        RATETAB_ENT(55,  2,  0),
  63        RATETAB_ENT(110, 3,  0),
  64        RATETAB_ENT(60,  9,  0),
  65        RATETAB_ENT(90,  6,  0),
  66        RATETAB_ENT(120, 7,  0),
  67        RATETAB_ENT(180, 8,  0),
  68        RATETAB_ENT(240, 9,  0),
  69        RATETAB_ENT(360, 10, 0),
  70        RATETAB_ENT(480, 11, 0),
  71        RATETAB_ENT(540, 12, 0),
  72};
  73
  74static struct ieee80211_supported_band lbs_band_2ghz = {
  75        .channels = lbs_2ghz_channels,
  76        .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  77        .bitrates = lbs_rates,
  78        .n_bitrates = ARRAY_SIZE(lbs_rates),
  79};
  80
  81
  82static const u32 cipher_suites[] = {
  83        WLAN_CIPHER_SUITE_WEP40,
  84        WLAN_CIPHER_SUITE_WEP104,
  85        WLAN_CIPHER_SUITE_TKIP,
  86        WLAN_CIPHER_SUITE_CCMP,
  87};
  88
  89/* Time to stay on the channel */
  90#define LBS_DWELL_PASSIVE 100
  91#define LBS_DWELL_ACTIVE  40
  92
  93
  94/***************************************************************************
  95 * Misc utility functions
  96 *
  97 * TLVs are Marvell specific. They are very similar to IEs, they have the
  98 * same structure: type, length, data*. The only difference: for IEs, the
  99 * type and length are u8, but for TLVs they're __le16.
 100 */
 101
 102/*
 103 * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
 104 * in the firmware spec
 105 */
 106static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
 107{
 108        int ret = -ENOTSUPP;
 109
 110        switch (auth_type) {
 111        case NL80211_AUTHTYPE_OPEN_SYSTEM:
 112        case NL80211_AUTHTYPE_SHARED_KEY:
 113                ret = auth_type;
 114                break;
 115        case NL80211_AUTHTYPE_AUTOMATIC:
 116                ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
 117                break;
 118        case NL80211_AUTHTYPE_NETWORK_EAP:
 119                ret = 0x80;
 120                break;
 121        default:
 122                /* silence compiler */
 123                break;
 124        }
 125        return ret;
 126}
 127
 128
 129/*
 130 * Various firmware commands need the list of supported rates, but with
 131 * the hight-bit set for basic rates
 132 */
 133static int lbs_add_rates(u8 *rates)
 134{
 135        size_t i;
 136
 137        for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
 138                u8 rate = lbs_rates[i].bitrate / 5;
 139                if (rate == 0x02 || rate == 0x04 ||
 140                    rate == 0x0b || rate == 0x16)
 141                        rate |= 0x80;
 142                rates[i] = rate;
 143        }
 144        return ARRAY_SIZE(lbs_rates);
 145}
 146
 147
 148/***************************************************************************
 149 * TLV utility functions
 150 *
 151 * TLVs are Marvell specific. They are very similar to IEs, they have the
 152 * same structure: type, length, data*. The only difference: for IEs, the
 153 * type and length are u8, but for TLVs they're __le16.
 154 */
 155
 156
 157/*
 158 * Add ssid TLV
 159 */
 160#define LBS_MAX_SSID_TLV_SIZE                   \
 161        (sizeof(struct mrvl_ie_header)          \
 162         + IEEE80211_MAX_SSID_LEN)
 163
 164static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
 165{
 166        struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
 167
 168        /*
 169         * TLV-ID SSID  00 00
 170         * length       06 00
 171         * ssid         4d 4e 54 45 53 54
 172         */
 173        ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
 174        ssid_tlv->header.len = cpu_to_le16(ssid_len);
 175        memcpy(ssid_tlv->ssid, ssid, ssid_len);
 176        return sizeof(ssid_tlv->header) + ssid_len;
 177}
 178
 179
 180/*
 181 * Add channel list TLV (section 8.4.2)
 182 *
 183 * Actual channel data comes from priv->wdev->wiphy->channels.
 184 */
 185#define LBS_MAX_CHANNEL_LIST_TLV_SIZE                                   \
 186        (sizeof(struct mrvl_ie_header)                                  \
 187         + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
 188
 189static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
 190                                    int last_channel, int active_scan)
 191{
 192        int chanscanparamsize = sizeof(struct chanscanparamset) *
 193                (last_channel - priv->scan_channel);
 194
 195        struct mrvl_ie_header *header = (void *) tlv;
 196
 197        /*
 198         * TLV-ID CHANLIST  01 01
 199         * length           0e 00
 200         * channel          00 01 00 00 00 64 00
 201         *   radio type     00
 202         *   channel           01
 203         *   scan type            00
 204         *   min scan time           00 00
 205         *   max scan time                 64 00
 206         * channel 2        00 02 00 00 00 64 00
 207         *
 208         */
 209
 210        header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
 211        header->len  = cpu_to_le16(chanscanparamsize);
 212        tlv += sizeof(struct mrvl_ie_header);
 213
 214        /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
 215                     last_channel); */
 216        memset(tlv, 0, chanscanparamsize);
 217
 218        while (priv->scan_channel < last_channel) {
 219                struct chanscanparamset *param = (void *) tlv;
 220
 221                param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
 222                param->channumber =
 223                        priv->scan_req->channels[priv->scan_channel]->hw_value;
 224                if (active_scan) {
 225                        param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
 226                } else {
 227                        param->chanscanmode.passivescan = 1;
 228                        param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
 229                }
 230                tlv += sizeof(struct chanscanparamset);
 231                priv->scan_channel++;
 232        }
 233        return sizeof(struct mrvl_ie_header) + chanscanparamsize;
 234}
 235
 236
 237/*
 238 * Add rates TLV
 239 *
 240 * The rates are in lbs_bg_rates[], but for the 802.11b
 241 * rates the high bit is set. We add this TLV only because
 242 * there's a firmware which otherwise doesn't report all
 243 * APs in range.
 244 */
 245#define LBS_MAX_RATES_TLV_SIZE                  \
 246        (sizeof(struct mrvl_ie_header)          \
 247         + (ARRAY_SIZE(lbs_rates)))
 248
 249/* Adds a TLV with all rates the hardware supports */
 250static int lbs_add_supported_rates_tlv(u8 *tlv)
 251{
 252        size_t i;
 253        struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
 254
 255        /*
 256         * TLV-ID RATES  01 00
 257         * length        0e 00
 258         * rates         82 84 8b 96 0c 12 18 24 30 48 60 6c
 259         */
 260        rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
 261        tlv += sizeof(rate_tlv->header);
 262        i = lbs_add_rates(tlv);
 263        tlv += i;
 264        rate_tlv->header.len = cpu_to_le16(i);
 265        return sizeof(rate_tlv->header) + i;
 266}
 267
 268/* Add common rates from a TLV and return the new end of the TLV */
 269static u8 *
 270add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
 271{
 272        int hw, ap, ap_max = ie[1];
 273        u8 hw_rate;
 274
 275        /* Advance past IE header */
 276        ie += 2;
 277
 278        lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
 279
 280        for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
 281                hw_rate = lbs_rates[hw].bitrate / 5;
 282                for (ap = 0; ap < ap_max; ap++) {
 283                        if (hw_rate == (ie[ap] & 0x7f)) {
 284                                *tlv++ = ie[ap];
 285                                *nrates = *nrates + 1;
 286                        }
 287                }
 288        }
 289        return tlv;
 290}
 291
 292/*
 293 * Adds a TLV with all rates the hardware *and* BSS supports.
 294 */
 295static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
 296{
 297        struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
 298        const u8 *rates_eid, *ext_rates_eid;
 299        int n = 0;
 300
 301        rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
 302        ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
 303
 304        /*
 305         * 01 00                   TLV_TYPE_RATES
 306         * 04 00                   len
 307         * 82 84 8b 96             rates
 308         */
 309        rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
 310        tlv += sizeof(rate_tlv->header);
 311
 312        /* Add basic rates */
 313        if (rates_eid) {
 314                tlv = add_ie_rates(tlv, rates_eid, &n);
 315
 316                /* Add extended rates, if any */
 317                if (ext_rates_eid)
 318                        tlv = add_ie_rates(tlv, ext_rates_eid, &n);
 319        } else {
 320                lbs_deb_assoc("assoc: bss had no basic rate IE\n");
 321                /* Fallback: add basic 802.11b rates */
 322                *tlv++ = 0x82;
 323                *tlv++ = 0x84;
 324                *tlv++ = 0x8b;
 325                *tlv++ = 0x96;
 326                n = 4;
 327        }
 328
 329        rate_tlv->header.len = cpu_to_le16(n);
 330        return sizeof(rate_tlv->header) + n;
 331}
 332
 333
 334/*
 335 * Add auth type TLV.
 336 *
 337 * This is only needed for newer firmware (V9 and up).
 338 */
 339#define LBS_MAX_AUTH_TYPE_TLV_SIZE \
 340        sizeof(struct mrvl_ie_auth_type)
 341
 342static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
 343{
 344        struct mrvl_ie_auth_type *auth = (void *) tlv;
 345
 346        /*
 347         * 1f 01  TLV_TYPE_AUTH_TYPE
 348         * 01 00  len
 349         * 01     auth type
 350         */
 351        auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
 352        auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
 353        auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
 354        return sizeof(*auth);
 355}
 356
 357
 358/*
 359 * Add channel (phy ds) TLV
 360 */
 361#define LBS_MAX_CHANNEL_TLV_SIZE \
 362        sizeof(struct mrvl_ie_header)
 363
 364static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
 365{
 366        struct mrvl_ie_ds_param_set *ds = (void *) tlv;
 367
 368        /*
 369         * 03 00  TLV_TYPE_PHY_DS
 370         * 01 00  len
 371         * 06     channel
 372         */
 373        ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
 374        ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
 375        ds->channel = channel;
 376        return sizeof(*ds);
 377}
 378
 379
 380/*
 381 * Add (empty) CF param TLV of the form:
 382 */
 383#define LBS_MAX_CF_PARAM_TLV_SIZE               \
 384        sizeof(struct mrvl_ie_header)
 385
 386static int lbs_add_cf_param_tlv(u8 *tlv)
 387{
 388        struct mrvl_ie_cf_param_set *cf = (void *)tlv;
 389
 390        /*
 391         * 04 00  TLV_TYPE_CF
 392         * 06 00  len
 393         * 00     cfpcnt
 394         * 00     cfpperiod
 395         * 00 00  cfpmaxduration
 396         * 00 00  cfpdurationremaining
 397         */
 398        cf->header.type = cpu_to_le16(TLV_TYPE_CF);
 399        cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
 400        return sizeof(*cf);
 401}
 402
 403/*
 404 * Add WPA TLV
 405 */
 406#define LBS_MAX_WPA_TLV_SIZE                    \
 407        (sizeof(struct mrvl_ie_header)          \
 408         + 128 /* TODO: I guessed the size */)
 409
 410static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
 411{
 412        size_t tlv_len;
 413
 414        /*
 415         * We need just convert an IE to an TLV. IEs use u8 for the header,
 416         *   u8      type
 417         *   u8      len
 418         *   u8[]    data
 419         * but TLVs use __le16 instead:
 420         *   __le16  type
 421         *   __le16  len
 422         *   u8[]    data
 423         */
 424        *tlv++ = *ie++;
 425        *tlv++ = 0;
 426        tlv_len = *tlv++ = *ie++;
 427        *tlv++ = 0;
 428        while (tlv_len--)
 429                *tlv++ = *ie++;
 430        /* the TLV is two bytes larger than the IE */
 431        return ie_len + 2;
 432}
 433
 434/*
 435 * Set Channel
 436 */
 437
 438static int lbs_cfg_set_channel(struct wiphy *wiphy,
 439        struct net_device *netdev,
 440        struct ieee80211_channel *channel,
 441        enum nl80211_channel_type channel_type)
 442{
 443        struct lbs_private *priv = wiphy_priv(wiphy);
 444        int ret = -ENOTSUPP;
 445
 446        lbs_deb_enter_args(LBS_DEB_CFG80211, "iface %s freq %d, type %d",
 447                           netdev_name(netdev), channel->center_freq, channel_type);
 448
 449        if (channel_type != NL80211_CHAN_NO_HT)
 450                goto out;
 451
 452        if (netdev == priv->mesh_dev)
 453                ret = lbs_mesh_set_channel(priv, channel->hw_value);
 454        else
 455                ret = lbs_set_channel(priv, channel->hw_value);
 456
 457 out:
 458        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
 459        return ret;
 460}
 461
 462
 463
 464/*
 465 * Scanning
 466 */
 467
 468/*
 469 * When scanning, the firmware doesn't send a nul packet with the power-safe
 470 * bit to the AP. So we cannot stay away from our current channel too long,
 471 * otherwise we loose data. So take a "nap" while scanning every other
 472 * while.
 473 */
 474#define LBS_SCAN_BEFORE_NAP 4
 475
 476
 477/*
 478 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
 479 * which isn't really an RSSI, as it becomes larger when moving away from
 480 * the AP. Anyway, we need to convert that into mBm.
 481 */
 482#define LBS_SCAN_RSSI_TO_MBM(rssi) \
 483        ((-(int)rssi + 3)*100)
 484
 485static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 486        struct cmd_header *resp)
 487{
 488        struct cfg80211_bss *bss;
 489        struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
 490        int bsssize;
 491        const u8 *pos;
 492        const u8 *tsfdesc;
 493        int tsfsize;
 494        int i;
 495        int ret = -EILSEQ;
 496
 497        lbs_deb_enter(LBS_DEB_CFG80211);
 498
 499        bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
 500
 501        lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
 502                        scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
 503
 504        if (scanresp->nr_sets == 0) {
 505                ret = 0;
 506                goto done;
 507        }
 508
 509        /*
 510         * The general layout of the scan response is described in chapter
 511         * 5.7.1. Basically we have a common part, then any number of BSS
 512         * descriptor sections. Finally we have section with the same number
 513         * of TSFs.
 514         *
 515         * cmd_ds_802_11_scan_rsp
 516         *   cmd_header
 517         *   pos_size
 518         *   nr_sets
 519         *   bssdesc 1
 520         *     bssid
 521         *     rssi
 522         *     timestamp
 523         *     intvl
 524         *     capa
 525         *     IEs
 526         *   bssdesc 2
 527         *   bssdesc n
 528         *   MrvlIEtypes_TsfFimestamp_t
 529         *     TSF for BSS 1
 530         *     TSF for BSS 2
 531         *     TSF for BSS n
 532         */
 533
 534        pos = scanresp->bssdesc_and_tlvbuffer;
 535
 536        lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
 537                        scanresp->bssdescriptsize);
 538
 539        tsfdesc = pos + bsssize;
 540        tsfsize = 4 + 8 * scanresp->nr_sets;
 541        lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
 542
 543        /* Validity check: we expect a Marvell-Local TLV */
 544        i = get_unaligned_le16(tsfdesc);
 545        tsfdesc += 2;
 546        if (i != TLV_TYPE_TSFTIMESTAMP) {
 547                lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
 548                goto done;
 549        }
 550
 551        /*
 552         * Validity check: the TLV holds TSF values with 8 bytes each, so
 553         * the size in the TLV must match the nr_sets value
 554         */
 555        i = get_unaligned_le16(tsfdesc);
 556        tsfdesc += 2;
 557        if (i / 8 != scanresp->nr_sets) {
 558                lbs_deb_scan("scan response: invalid number of TSF timestamp "
 559                             "sets (expected %d got %d)\n", scanresp->nr_sets,
 560                             i / 8);
 561                goto done;
 562        }
 563
 564        for (i = 0; i < scanresp->nr_sets; i++) {
 565                const u8 *bssid;
 566                const u8 *ie;
 567                int left;
 568                int ielen;
 569                int rssi;
 570                u16 intvl;
 571                u16 capa;
 572                int chan_no = -1;
 573                const u8 *ssid = NULL;
 574                u8 ssid_len = 0;
 575                DECLARE_SSID_BUF(ssid_buf);
 576
 577                int len = get_unaligned_le16(pos);
 578                pos += 2;
 579
 580                /* BSSID */
 581                bssid = pos;
 582                pos += ETH_ALEN;
 583                /* RSSI */
 584                rssi = *pos++;
 585                /* Packet time stamp */
 586                pos += 8;
 587                /* Beacon interval */
 588                intvl = get_unaligned_le16(pos);
 589                pos += 2;
 590                /* Capabilities */
 591                capa = get_unaligned_le16(pos);
 592                pos += 2;
 593
 594                /* To find out the channel, we must parse the IEs */
 595                ie = pos;
 596                /*
 597                 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
 598                 * interval, capabilities
 599                 */
 600                ielen = left = len - (6 + 1 + 8 + 2 + 2);
 601                while (left >= 2) {
 602                        u8 id, elen;
 603                        id = *pos++;
 604                        elen = *pos++;
 605                        left -= 2;
 606                        if (elen > left || elen == 0) {
 607                                lbs_deb_scan("scan response: invalid IE fmt\n");
 608                                goto done;
 609                        }
 610
 611                        if (id == WLAN_EID_DS_PARAMS)
 612                                chan_no = *pos;
 613                        if (id == WLAN_EID_SSID) {
 614                                ssid = pos;
 615                                ssid_len = elen;
 616                        }
 617                        left -= elen;
 618                        pos += elen;
 619                }
 620
 621                /* No channel, no luck */
 622                if (chan_no != -1) {
 623                        struct wiphy *wiphy = priv->wdev->wiphy;
 624                        int freq = ieee80211_channel_to_frequency(chan_no,
 625                                                        IEEE80211_BAND_2GHZ);
 626                        struct ieee80211_channel *channel =
 627                                ieee80211_get_channel(wiphy, freq);
 628
 629                        lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
 630                                     "%d dBm\n",
 631                                     bssid, capa, chan_no,
 632                                     print_ssid(ssid_buf, ssid, ssid_len),
 633                                     LBS_SCAN_RSSI_TO_MBM(rssi)/100);
 634
 635                        if (channel &&
 636                            !(channel->flags & IEEE80211_CHAN_DISABLED)) {
 637                                bss = cfg80211_inform_bss(wiphy, channel,
 638                                        bssid, get_unaligned_le64(tsfdesc),
 639                                        capa, intvl, ie, ielen,
 640                                        LBS_SCAN_RSSI_TO_MBM(rssi),
 641                                        GFP_KERNEL);
 642                                cfg80211_put_bss(bss);
 643                        }
 644                } else
 645                        lbs_deb_scan("scan response: missing BSS channel IE\n");
 646
 647                tsfdesc += 8;
 648        }
 649        ret = 0;
 650
 651 done:
 652        lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
 653        return ret;
 654}
 655
 656
 657/*
 658 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
 659 * TLV and a rates TLV. Determine the maximum size of them:
 660 */
 661#define LBS_SCAN_MAX_CMD_SIZE                   \
 662        (sizeof(struct cmd_ds_802_11_scan)      \
 663         + LBS_MAX_SSID_TLV_SIZE                \
 664         + LBS_MAX_CHANNEL_LIST_TLV_SIZE        \
 665         + LBS_MAX_RATES_TLV_SIZE)
 666
 667/*
 668 * Assumes priv->scan_req is initialized and valid
 669 * Assumes priv->scan_channel is initialized
 670 */
 671static void lbs_scan_worker(struct work_struct *work)
 672{
 673        struct lbs_private *priv =
 674                container_of(work, struct lbs_private, scan_work.work);
 675        struct cmd_ds_802_11_scan *scan_cmd;
 676        u8 *tlv; /* pointer into our current, growing TLV storage area */
 677        int last_channel;
 678        int running, carrier;
 679
 680        lbs_deb_enter(LBS_DEB_SCAN);
 681
 682        scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
 683        if (scan_cmd == NULL)
 684                goto out_no_scan_cmd;
 685
 686        /* prepare fixed part of scan command */
 687        scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
 688
 689        /* stop network while we're away from our main channel */
 690        running = !netif_queue_stopped(priv->dev);
 691        carrier = netif_carrier_ok(priv->dev);
 692        if (running)
 693                netif_stop_queue(priv->dev);
 694        if (carrier)
 695                netif_carrier_off(priv->dev);
 696
 697        /* prepare fixed part of scan command */
 698        tlv = scan_cmd->tlvbuffer;
 699
 700        /* add SSID TLV */
 701        if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
 702                tlv += lbs_add_ssid_tlv(tlv,
 703                                        priv->scan_req->ssids[0].ssid,
 704                                        priv->scan_req->ssids[0].ssid_len);
 705
 706        /* add channel TLVs */
 707        last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
 708        if (last_channel > priv->scan_req->n_channels)
 709                last_channel = priv->scan_req->n_channels;
 710        tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
 711                priv->scan_req->n_ssids);
 712
 713        /* add rates TLV */
 714        tlv += lbs_add_supported_rates_tlv(tlv);
 715
 716        if (priv->scan_channel < priv->scan_req->n_channels) {
 717                cancel_delayed_work(&priv->scan_work);
 718                if (netif_running(priv->dev))
 719                        queue_delayed_work(priv->work_thread, &priv->scan_work,
 720                                msecs_to_jiffies(300));
 721        }
 722
 723        /* This is the final data we are about to send */
 724        scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
 725        lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
 726                    sizeof(*scan_cmd));
 727        lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
 728                    tlv - scan_cmd->tlvbuffer);
 729
 730        __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
 731                le16_to_cpu(scan_cmd->hdr.size),
 732                lbs_ret_scan, 0);
 733
 734        if (priv->scan_channel >= priv->scan_req->n_channels) {
 735                /* Mark scan done */
 736                cancel_delayed_work(&priv->scan_work);
 737                lbs_scan_done(priv);
 738        }
 739
 740        /* Restart network */
 741        if (carrier)
 742                netif_carrier_on(priv->dev);
 743        if (running && !priv->tx_pending_len)
 744                netif_wake_queue(priv->dev);
 745
 746        kfree(scan_cmd);
 747
 748        /* Wake up anything waiting on scan completion */
 749        if (priv->scan_req == NULL) {
 750                lbs_deb_scan("scan: waking up waiters\n");
 751                wake_up_all(&priv->scan_q);
 752        }
 753
 754 out_no_scan_cmd:
 755        lbs_deb_leave(LBS_DEB_SCAN);
 756}
 757
 758static void _internal_start_scan(struct lbs_private *priv, bool internal,
 759        struct cfg80211_scan_request *request)
 760{
 761        lbs_deb_enter(LBS_DEB_CFG80211);
 762
 763        lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
 764                request->n_ssids, request->n_channels, request->ie_len);
 765
 766        priv->scan_channel = 0;
 767        priv->scan_req = request;
 768        priv->internal_scan = internal;
 769
 770        queue_delayed_work(priv->work_thread, &priv->scan_work,
 771                msecs_to_jiffies(50));
 772
 773        lbs_deb_leave(LBS_DEB_CFG80211);
 774}
 775
 776/*
 777 * Clean up priv->scan_req.  Should be used to handle the allocation details.
 778 */
 779void lbs_scan_done(struct lbs_private *priv)
 780{
 781        WARN_ON(!priv->scan_req);
 782
 783        if (priv->internal_scan)
 784                kfree(priv->scan_req);
 785        else
 786                cfg80211_scan_done(priv->scan_req, false);
 787
 788        priv->scan_req = NULL;
 789}
 790
 791static int lbs_cfg_scan(struct wiphy *wiphy,
 792        struct net_device *dev,
 793        struct cfg80211_scan_request *request)
 794{
 795        struct lbs_private *priv = wiphy_priv(wiphy);
 796        int ret = 0;
 797
 798        lbs_deb_enter(LBS_DEB_CFG80211);
 799
 800        if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
 801                /* old scan request not yet processed */
 802                ret = -EAGAIN;
 803                goto out;
 804        }
 805
 806        _internal_start_scan(priv, false, request);
 807
 808        if (priv->surpriseremoved)
 809                ret = -EIO;
 810
 811 out:
 812        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
 813        return ret;
 814}
 815
 816
 817
 818
 819/*
 820 * Events
 821 */
 822
 823void lbs_send_disconnect_notification(struct lbs_private *priv)
 824{
 825        lbs_deb_enter(LBS_DEB_CFG80211);
 826
 827        cfg80211_disconnected(priv->dev,
 828                0,
 829                NULL, 0,
 830                GFP_KERNEL);
 831
 832        lbs_deb_leave(LBS_DEB_CFG80211);
 833}
 834
 835void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
 836{
 837        lbs_deb_enter(LBS_DEB_CFG80211);
 838
 839        cfg80211_michael_mic_failure(priv->dev,
 840                priv->assoc_bss,
 841                event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
 842                        NL80211_KEYTYPE_GROUP :
 843                        NL80211_KEYTYPE_PAIRWISE,
 844                -1,
 845                NULL,
 846                GFP_KERNEL);
 847
 848        lbs_deb_leave(LBS_DEB_CFG80211);
 849}
 850
 851
 852
 853
 854/*
 855 * Connect/disconnect
 856 */
 857
 858
 859/*
 860 * This removes all WEP keys
 861 */
 862static int lbs_remove_wep_keys(struct lbs_private *priv)
 863{
 864        struct cmd_ds_802_11_set_wep cmd;
 865        int ret;
 866
 867        lbs_deb_enter(LBS_DEB_CFG80211);
 868
 869        memset(&cmd, 0, sizeof(cmd));
 870        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 871        cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
 872        cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
 873
 874        ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
 875
 876        lbs_deb_leave(LBS_DEB_CFG80211);
 877        return ret;
 878}
 879
 880/*
 881 * Set WEP keys
 882 */
 883static int lbs_set_wep_keys(struct lbs_private *priv)
 884{
 885        struct cmd_ds_802_11_set_wep cmd;
 886        int i;
 887        int ret;
 888
 889        lbs_deb_enter(LBS_DEB_CFG80211);
 890
 891        /*
 892         * command         13 00
 893         * size            50 00
 894         * sequence        xx xx
 895         * result          00 00
 896         * action          02 00     ACT_ADD
 897         * transmit key    00 00
 898         * type for key 1  01        WEP40
 899         * type for key 2  00
 900         * type for key 3  00
 901         * type for key 4  00
 902         * key 1           39 39 39 39 39 00 00 00
 903         *                 00 00 00 00 00 00 00 00
 904         * key 2           00 00 00 00 00 00 00 00
 905         *                 00 00 00 00 00 00 00 00
 906         * key 3           00 00 00 00 00 00 00 00
 907         *                 00 00 00 00 00 00 00 00
 908         * key 4           00 00 00 00 00 00 00 00
 909         */
 910        if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
 911            priv->wep_key_len[2] || priv->wep_key_len[3]) {
 912                /* Only set wep keys if we have at least one of them */
 913                memset(&cmd, 0, sizeof(cmd));
 914                cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 915                cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
 916                cmd.action = cpu_to_le16(CMD_ACT_ADD);
 917
 918                for (i = 0; i < 4; i++) {
 919                        switch (priv->wep_key_len[i]) {
 920                        case WLAN_KEY_LEN_WEP40:
 921                                cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
 922                                break;
 923                        case WLAN_KEY_LEN_WEP104:
 924                                cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
 925                                break;
 926                        default:
 927                                cmd.keytype[i] = 0;
 928                                break;
 929                        }
 930                        memcpy(cmd.keymaterial[i], priv->wep_key[i],
 931                               priv->wep_key_len[i]);
 932                }
 933
 934                ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
 935        } else {
 936                /* Otherwise remove all wep keys */
 937                ret = lbs_remove_wep_keys(priv);
 938        }
 939
 940        lbs_deb_leave(LBS_DEB_CFG80211);
 941        return ret;
 942}
 943
 944
 945/*
 946 * Enable/Disable RSN status
 947 */
 948static int lbs_enable_rsn(struct lbs_private *priv, int enable)
 949{
 950        struct cmd_ds_802_11_enable_rsn cmd;
 951        int ret;
 952
 953        lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
 954
 955        /*
 956         * cmd       2f 00
 957         * size      0c 00
 958         * sequence  xx xx
 959         * result    00 00
 960         * action    01 00    ACT_SET
 961         * enable    01 00
 962         */
 963        memset(&cmd, 0, sizeof(cmd));
 964        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 965        cmd.action = cpu_to_le16(CMD_ACT_SET);
 966        cmd.enable = cpu_to_le16(enable);
 967
 968        ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
 969
 970        lbs_deb_leave(LBS_DEB_CFG80211);
 971        return ret;
 972}
 973
 974
 975/*
 976 * Set WPA/WPA key material
 977 */
 978
 979/*
 980 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
 981 * get rid of WEXT, this should go into host.h
 982 */
 983
 984struct cmd_key_material {
 985        struct cmd_header hdr;
 986
 987        __le16 action;
 988        struct MrvlIEtype_keyParamSet param;
 989} __packed;
 990
 991static int lbs_set_key_material(struct lbs_private *priv,
 992                                int key_type,
 993                                int key_info,
 994                                u8 *key, u16 key_len)
 995{
 996        struct cmd_key_material cmd;
 997        int ret;
 998
 999        lbs_deb_enter(LBS_DEB_CFG80211);
1000
1001        /*
1002         * Example for WPA (TKIP):
1003         *
1004         * cmd       5e 00
1005         * size      34 00
1006         * sequence  xx xx
1007         * result    00 00
1008         * action    01 00
1009         * TLV type  00 01    key param
1010         * length    00 26
1011         * key type  01 00    TKIP
1012         * key info  06 00    UNICAST | ENABLED
1013         * key len   20 00
1014         * key       32 bytes
1015         */
1016        memset(&cmd, 0, sizeof(cmd));
1017        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1018        cmd.action = cpu_to_le16(CMD_ACT_SET);
1019        cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1020        cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1021        cmd.param.keytypeid = cpu_to_le16(key_type);
1022        cmd.param.keyinfo = cpu_to_le16(key_info);
1023        cmd.param.keylen = cpu_to_le16(key_len);
1024        if (key && key_len)
1025                memcpy(cmd.param.key, key, key_len);
1026
1027        ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1028
1029        lbs_deb_leave(LBS_DEB_CFG80211);
1030        return ret;
1031}
1032
1033
1034/*
1035 * Sets the auth type (open, shared, etc) in the firmware. That
1036 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1037 * command doesn't send an authentication frame at all, it just
1038 * stores the auth_type.
1039 */
1040static int lbs_set_authtype(struct lbs_private *priv,
1041                            struct cfg80211_connect_params *sme)
1042{
1043        struct cmd_ds_802_11_authenticate cmd;
1044        int ret;
1045
1046        lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
1047
1048        /*
1049         * cmd        11 00
1050         * size       19 00
1051         * sequence   xx xx
1052         * result     00 00
1053         * BSS id     00 13 19 80 da 30
1054         * auth type  00
1055         * reserved   00 00 00 00 00 00 00 00 00 00
1056         */
1057        memset(&cmd, 0, sizeof(cmd));
1058        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1059        if (sme->bssid)
1060                memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1061        /* convert auth_type */
1062        ret = lbs_auth_to_authtype(sme->auth_type);
1063        if (ret < 0)
1064                goto done;
1065
1066        cmd.authtype = ret;
1067        ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1068
1069 done:
1070        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1071        return ret;
1072}
1073
1074
1075/*
1076 * Create association request
1077 */
1078#define LBS_ASSOC_MAX_CMD_SIZE                     \
1079        (sizeof(struct cmd_ds_802_11_associate)    \
1080         - 512 /* cmd_ds_802_11_associate.iebuf */ \
1081         + LBS_MAX_SSID_TLV_SIZE                   \
1082         + LBS_MAX_CHANNEL_TLV_SIZE                \
1083         + LBS_MAX_CF_PARAM_TLV_SIZE               \
1084         + LBS_MAX_AUTH_TYPE_TLV_SIZE              \
1085         + LBS_MAX_WPA_TLV_SIZE)
1086
1087static int lbs_associate(struct lbs_private *priv,
1088                struct cfg80211_bss *bss,
1089                struct cfg80211_connect_params *sme)
1090{
1091        struct cmd_ds_802_11_associate_response *resp;
1092        struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1093                                                      GFP_KERNEL);
1094        const u8 *ssid_eid;
1095        size_t len, resp_ie_len;
1096        int status;
1097        int ret;
1098        u8 *pos = &(cmd->iebuf[0]);
1099        u8 *tmp;
1100
1101        lbs_deb_enter(LBS_DEB_CFG80211);
1102
1103        if (!cmd) {
1104                ret = -ENOMEM;
1105                goto done;
1106        }
1107
1108        /*
1109         * cmd              50 00
1110         * length           34 00
1111         * sequence         xx xx
1112         * result           00 00
1113         * BSS id           00 13 19 80 da 30
1114         * capabilities     11 00
1115         * listen interval  0a 00
1116         * beacon interval  00 00
1117         * DTIM period      00
1118         * TLVs             xx   (up to 512 bytes)
1119         */
1120        cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1121
1122        /* Fill in static fields */
1123        memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1124        cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1125        cmd->capability = cpu_to_le16(bss->capability);
1126
1127        /* add SSID TLV */
1128        ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1129        if (ssid_eid)
1130                pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1131        else
1132                lbs_deb_assoc("no SSID\n");
1133
1134        /* add DS param TLV */
1135        if (bss->channel)
1136                pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1137        else
1138                lbs_deb_assoc("no channel\n");
1139
1140        /* add (empty) CF param TLV */
1141        pos += lbs_add_cf_param_tlv(pos);
1142
1143        /* add rates TLV */
1144        tmp = pos + 4; /* skip Marvell IE header */
1145        pos += lbs_add_common_rates_tlv(pos, bss);
1146        lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1147
1148        /* add auth type TLV */
1149        if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1150                pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1151
1152        /* add WPA/WPA2 TLV */
1153        if (sme->ie && sme->ie_len)
1154                pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1155
1156        len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
1157                (u16)(pos - (u8 *) &cmd->iebuf);
1158        cmd->hdr.size = cpu_to_le16(len);
1159
1160        lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1161                        le16_to_cpu(cmd->hdr.size));
1162
1163        /* store for later use */
1164        memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1165
1166        ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1167        if (ret)
1168                goto done;
1169
1170        /* generate connect message to cfg80211 */
1171
1172        resp = (void *) cmd; /* recast for easier field access */
1173        status = le16_to_cpu(resp->statuscode);
1174
1175        /* Older FW versions map the IEEE 802.11 Status Code in the association
1176         * response to the following values returned in resp->statuscode:
1177         *
1178         *    IEEE Status Code                Marvell Status Code
1179         *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
1180         *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1181         *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1182         *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1183         *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1184         *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
1185         *
1186         * Other response codes:
1187         *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1188         *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1189         *                                    association response from the AP)
1190         */
1191        if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1192                switch (status) {
1193                case 0:
1194                        break;
1195                case 1:
1196                        lbs_deb_assoc("invalid association parameters\n");
1197                        status = WLAN_STATUS_CAPS_UNSUPPORTED;
1198                        break;
1199                case 2:
1200                        lbs_deb_assoc("timer expired while waiting for AP\n");
1201                        status = WLAN_STATUS_AUTH_TIMEOUT;
1202                        break;
1203                case 3:
1204                        lbs_deb_assoc("association refused by AP\n");
1205                        status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1206                        break;
1207                case 4:
1208                        lbs_deb_assoc("authentication refused by AP\n");
1209                        status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1210                        break;
1211                default:
1212                        lbs_deb_assoc("association failure %d\n", status);
1213                        /* v5 OLPC firmware does return the AP status code if
1214                         * it's not one of the values above.  Let that through.
1215                         */
1216                        break;
1217                }
1218        }
1219
1220        lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1221                      "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1222                      le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1223
1224        resp_ie_len = le16_to_cpu(resp->hdr.size)
1225                - sizeof(resp->hdr)
1226                - 6;
1227        cfg80211_connect_result(priv->dev,
1228                                priv->assoc_bss,
1229                                sme->ie, sme->ie_len,
1230                                resp->iebuf, resp_ie_len,
1231                                status,
1232                                GFP_KERNEL);
1233
1234        if (status == 0) {
1235                /* TODO: get rid of priv->connect_status */
1236                priv->connect_status = LBS_CONNECTED;
1237                netif_carrier_on(priv->dev);
1238                if (!priv->tx_pending_len)
1239                        netif_tx_wake_all_queues(priv->dev);
1240        }
1241
1242done:
1243        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1244        return ret;
1245}
1246
1247static struct cfg80211_scan_request *
1248_new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1249{
1250        struct cfg80211_scan_request *creq = NULL;
1251        int i, n_channels = 0;
1252        enum ieee80211_band band;
1253
1254        for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1255                if (wiphy->bands[band])
1256                        n_channels += wiphy->bands[band]->n_channels;
1257        }
1258
1259        creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1260                       n_channels * sizeof(void *),
1261                       GFP_ATOMIC);
1262        if (!creq)
1263                return NULL;
1264
1265        /* SSIDs come after channels */
1266        creq->ssids = (void *)&creq->channels[n_channels];
1267        creq->n_channels = n_channels;
1268        creq->n_ssids = 1;
1269
1270        /* Scan all available channels */
1271        i = 0;
1272        for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1273                int j;
1274
1275                if (!wiphy->bands[band])
1276                        continue;
1277
1278                for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1279                        /* ignore disabled channels */
1280                        if (wiphy->bands[band]->channels[j].flags &
1281                                                IEEE80211_CHAN_DISABLED)
1282                                continue;
1283
1284                        creq->channels[i] = &wiphy->bands[band]->channels[j];
1285                        i++;
1286                }
1287        }
1288        if (i) {
1289                /* Set real number of channels specified in creq->channels[] */
1290                creq->n_channels = i;
1291
1292                /* Scan for the SSID we're going to connect to */
1293                memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1294                creq->ssids[0].ssid_len = sme->ssid_len;
1295        } else {
1296                /* No channels found... */
1297                kfree(creq);
1298                creq = NULL;
1299        }
1300
1301        return creq;
1302}
1303
1304static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1305                           struct cfg80211_connect_params *sme)
1306{
1307        struct lbs_private *priv = wiphy_priv(wiphy);
1308        struct cfg80211_bss *bss = NULL;
1309        int ret = 0;
1310        u8 preamble = RADIO_PREAMBLE_SHORT;
1311
1312        if (dev == priv->mesh_dev)
1313                return -EOPNOTSUPP;
1314
1315        lbs_deb_enter(LBS_DEB_CFG80211);
1316
1317        if (!sme->bssid) {
1318                struct cfg80211_scan_request *creq;
1319
1320                /*
1321                 * Scan for the requested network after waiting for existing
1322                 * scans to finish.
1323                 */
1324                lbs_deb_assoc("assoc: waiting for existing scans\n");
1325                wait_event_interruptible_timeout(priv->scan_q,
1326                                                 (priv->scan_req == NULL),
1327                                                 (15 * HZ));
1328
1329                creq = _new_connect_scan_req(wiphy, sme);
1330                if (!creq) {
1331                        ret = -EINVAL;
1332                        goto done;
1333                }
1334
1335                lbs_deb_assoc("assoc: scanning for compatible AP\n");
1336                _internal_start_scan(priv, true, creq);
1337
1338                lbs_deb_assoc("assoc: waiting for scan to complete\n");
1339                wait_event_interruptible_timeout(priv->scan_q,
1340                                                 (priv->scan_req == NULL),
1341                                                 (15 * HZ));
1342                lbs_deb_assoc("assoc: scanning competed\n");
1343        }
1344
1345        /* Find the BSS we want using available scan results */
1346        bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1347                sme->ssid, sme->ssid_len,
1348                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
1349        if (!bss) {
1350                wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1351                          sme->bssid);
1352                ret = -ENOENT;
1353                goto done;
1354        }
1355        lbs_deb_assoc("trying %pM\n", bss->bssid);
1356        lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1357                      sme->crypto.cipher_group,
1358                      sme->key_idx, sme->key_len);
1359
1360        /* As this is a new connection, clear locally stored WEP keys */
1361        priv->wep_tx_key = 0;
1362        memset(priv->wep_key, 0, sizeof(priv->wep_key));
1363        memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1364
1365        /* set/remove WEP keys */
1366        switch (sme->crypto.cipher_group) {
1367        case WLAN_CIPHER_SUITE_WEP40:
1368        case WLAN_CIPHER_SUITE_WEP104:
1369                /* Store provided WEP keys in priv-> */
1370                priv->wep_tx_key = sme->key_idx;
1371                priv->wep_key_len[sme->key_idx] = sme->key_len;
1372                memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1373                /* Set WEP keys and WEP mode */
1374                lbs_set_wep_keys(priv);
1375                priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1376                lbs_set_mac_control(priv);
1377                /* No RSN mode for WEP */
1378                lbs_enable_rsn(priv, 0);
1379                break;
1380        case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1381                /*
1382                 * If we don't have no WEP, no WPA and no WPA2,
1383                 * we remove all keys like in the WPA/WPA2 setup,
1384                 * we just don't set RSN.
1385                 *
1386                 * Therefore: fall-through
1387                 */
1388        case WLAN_CIPHER_SUITE_TKIP:
1389        case WLAN_CIPHER_SUITE_CCMP:
1390                /* Remove WEP keys and WEP mode */
1391                lbs_remove_wep_keys(priv);
1392                priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1393                lbs_set_mac_control(priv);
1394
1395                /* clear the WPA/WPA2 keys */
1396                lbs_set_key_material(priv,
1397                        KEY_TYPE_ID_WEP, /* doesn't matter */
1398                        KEY_INFO_WPA_UNICAST,
1399                        NULL, 0);
1400                lbs_set_key_material(priv,
1401                        KEY_TYPE_ID_WEP, /* doesn't matter */
1402                        KEY_INFO_WPA_MCAST,
1403                        NULL, 0);
1404                /* RSN mode for WPA/WPA2 */
1405                lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1406                break;
1407        default:
1408                wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1409                          sme->crypto.cipher_group);
1410                ret = -ENOTSUPP;
1411                goto done;
1412        }
1413
1414        lbs_set_authtype(priv, sme);
1415        lbs_set_radio(priv, preamble, 1);
1416
1417        /* Do the actual association */
1418        ret = lbs_associate(priv, bss, sme);
1419
1420 done:
1421        if (bss)
1422                cfg80211_put_bss(bss);
1423        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1424        return ret;
1425}
1426
1427int lbs_disconnect(struct lbs_private *priv, u16 reason)
1428{
1429        struct cmd_ds_802_11_deauthenticate cmd;
1430        int ret;
1431
1432        memset(&cmd, 0, sizeof(cmd));
1433        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1434        /* Mildly ugly to use a locally store my own BSSID ... */
1435        memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1436        cmd.reasoncode = cpu_to_le16(reason);
1437
1438        ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1439        if (ret)
1440                return ret;
1441
1442        cfg80211_disconnected(priv->dev,
1443                        reason,
1444                        NULL, 0,
1445                        GFP_KERNEL);
1446        priv->connect_status = LBS_DISCONNECTED;
1447
1448        return 0;
1449}
1450
1451static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1452        u16 reason_code)
1453{
1454        struct lbs_private *priv = wiphy_priv(wiphy);
1455
1456        if (dev == priv->mesh_dev)
1457                return -EOPNOTSUPP;
1458
1459        lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
1460
1461        /* store for lbs_cfg_ret_disconnect() */
1462        priv->disassoc_reason = reason_code;
1463
1464        return lbs_disconnect(priv, reason_code);
1465}
1466
1467static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1468                                   struct net_device *netdev,
1469                                   u8 key_index, bool unicast,
1470                                   bool multicast)
1471{
1472        struct lbs_private *priv = wiphy_priv(wiphy);
1473
1474        if (netdev == priv->mesh_dev)
1475                return -EOPNOTSUPP;
1476
1477        lbs_deb_enter(LBS_DEB_CFG80211);
1478
1479        if (key_index != priv->wep_tx_key) {
1480                lbs_deb_assoc("set_default_key: to %d\n", key_index);
1481                priv->wep_tx_key = key_index;
1482                lbs_set_wep_keys(priv);
1483        }
1484
1485        return 0;
1486}
1487
1488
1489static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1490                           u8 idx, bool pairwise, const u8 *mac_addr,
1491                           struct key_params *params)
1492{
1493        struct lbs_private *priv = wiphy_priv(wiphy);
1494        u16 key_info;
1495        u16 key_type;
1496        int ret = 0;
1497
1498        if (netdev == priv->mesh_dev)
1499                return -EOPNOTSUPP;
1500
1501        lbs_deb_enter(LBS_DEB_CFG80211);
1502
1503        lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1504                      params->cipher, mac_addr);
1505        lbs_deb_assoc("add_key: key index %d, key len %d\n",
1506                      idx, params->key_len);
1507        if (params->key_len)
1508                lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1509                            params->key, params->key_len);
1510
1511        lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1512        if (params->seq_len)
1513                lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1514                            params->seq, params->seq_len);
1515
1516        switch (params->cipher) {
1517        case WLAN_CIPHER_SUITE_WEP40:
1518        case WLAN_CIPHER_SUITE_WEP104:
1519                /* actually compare if something has changed ... */
1520                if ((priv->wep_key_len[idx] != params->key_len) ||
1521                        memcmp(priv->wep_key[idx],
1522                               params->key, params->key_len) != 0) {
1523                        priv->wep_key_len[idx] = params->key_len;
1524                        memcpy(priv->wep_key[idx],
1525                               params->key, params->key_len);
1526                        lbs_set_wep_keys(priv);
1527                }
1528                break;
1529        case WLAN_CIPHER_SUITE_TKIP:
1530        case WLAN_CIPHER_SUITE_CCMP:
1531                key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1532                                                   ? KEY_INFO_WPA_UNICAST
1533                                                   : KEY_INFO_WPA_MCAST);
1534                key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1535                        ? KEY_TYPE_ID_TKIP
1536                        : KEY_TYPE_ID_AES;
1537                lbs_set_key_material(priv,
1538                                     key_type,
1539                                     key_info,
1540                                     params->key, params->key_len);
1541                break;
1542        default:
1543                wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1544                ret = -ENOTSUPP;
1545                break;
1546        }
1547
1548        return ret;
1549}
1550
1551
1552static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1553                           u8 key_index, bool pairwise, const u8 *mac_addr)
1554{
1555
1556        lbs_deb_enter(LBS_DEB_CFG80211);
1557
1558        lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1559                      key_index, mac_addr);
1560
1561#ifdef TODO
1562        struct lbs_private *priv = wiphy_priv(wiphy);
1563        /*
1564         * I think can keep this a NO-OP, because:
1565
1566         * - we clear all keys whenever we do lbs_cfg_connect() anyway
1567         * - neither "iw" nor "wpa_supplicant" won't call this during
1568         *   an ongoing connection
1569         * - TODO: but I have to check if this is still true when
1570         *   I set the AP to periodic re-keying
1571         * - we've not kzallec() something when we've added a key at
1572         *   lbs_cfg_connect() or lbs_cfg_add_key().
1573         *
1574         * This causes lbs_cfg_del_key() only called at disconnect time,
1575         * where we'd just waste time deleting a key that is not going
1576         * to be used anyway.
1577         */
1578        if (key_index < 3 && priv->wep_key_len[key_index]) {
1579                priv->wep_key_len[key_index] = 0;
1580                lbs_set_wep_keys(priv);
1581        }
1582#endif
1583
1584        return 0;
1585}
1586
1587
1588/*
1589 * Get station
1590 */
1591
1592static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1593                              u8 *mac, struct station_info *sinfo)
1594{
1595        struct lbs_private *priv = wiphy_priv(wiphy);
1596        s8 signal, noise;
1597        int ret;
1598        size_t i;
1599
1600        lbs_deb_enter(LBS_DEB_CFG80211);
1601
1602        sinfo->filled |= STATION_INFO_TX_BYTES |
1603                         STATION_INFO_TX_PACKETS |
1604                         STATION_INFO_RX_BYTES |
1605                         STATION_INFO_RX_PACKETS;
1606        sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1607        sinfo->tx_packets = priv->dev->stats.tx_packets;
1608        sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1609        sinfo->rx_packets = priv->dev->stats.rx_packets;
1610
1611        /* Get current RSSI */
1612        ret = lbs_get_rssi(priv, &signal, &noise);
1613        if (ret == 0) {
1614                sinfo->signal = signal;
1615                sinfo->filled |= STATION_INFO_SIGNAL;
1616        }
1617
1618        /* Convert priv->cur_rate from hw_value to NL80211 value */
1619        for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1620                if (priv->cur_rate == lbs_rates[i].hw_value) {
1621                        sinfo->txrate.legacy = lbs_rates[i].bitrate;
1622                        sinfo->filled |= STATION_INFO_TX_BITRATE;
1623                        break;
1624                }
1625        }
1626
1627        return 0;
1628}
1629
1630
1631
1632
1633/*
1634 * "Site survey", here just current channel and noise level
1635 */
1636
1637static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
1638        int idx, struct survey_info *survey)
1639{
1640        struct lbs_private *priv = wiphy_priv(wiphy);
1641        s8 signal, noise;
1642        int ret;
1643
1644        if (dev == priv->mesh_dev)
1645                return -EOPNOTSUPP;
1646
1647        if (idx != 0)
1648                ret = -ENOENT;
1649
1650        lbs_deb_enter(LBS_DEB_CFG80211);
1651
1652        survey->channel = ieee80211_get_channel(wiphy,
1653                ieee80211_channel_to_frequency(priv->channel,
1654                                               IEEE80211_BAND_2GHZ));
1655
1656        ret = lbs_get_rssi(priv, &signal, &noise);
1657        if (ret == 0) {
1658                survey->filled = SURVEY_INFO_NOISE_DBM;
1659                survey->noise = noise;
1660        }
1661
1662        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1663        return ret;
1664}
1665
1666
1667
1668
1669/*
1670 * Change interface
1671 */
1672
1673static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1674        enum nl80211_iftype type, u32 *flags,
1675               struct vif_params *params)
1676{
1677        struct lbs_private *priv = wiphy_priv(wiphy);
1678        int ret = 0;
1679
1680        if (dev == priv->mesh_dev)
1681                return -EOPNOTSUPP;
1682
1683        switch (type) {
1684        case NL80211_IFTYPE_MONITOR:
1685        case NL80211_IFTYPE_STATION:
1686        case NL80211_IFTYPE_ADHOC:
1687                break;
1688        default:
1689                return -EOPNOTSUPP;
1690        }
1691
1692        lbs_deb_enter(LBS_DEB_CFG80211);
1693
1694        if (priv->iface_running)
1695                ret = lbs_set_iface_type(priv, type);
1696
1697        if (!ret)
1698                priv->wdev->iftype = type;
1699
1700        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1701        return ret;
1702}
1703
1704
1705
1706/*
1707 * IBSS (Ad-Hoc)
1708 */
1709
1710/*
1711 * The firmware needs the following bits masked out of the beacon-derived
1712 * capability field when associating/joining to a BSS:
1713 *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1714 */
1715#define CAPINFO_MASK (~(0xda00))
1716
1717
1718static void lbs_join_post(struct lbs_private *priv,
1719                          struct cfg80211_ibss_params *params,
1720                          u8 *bssid, u16 capability)
1721{
1722        u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1723                   2 + 4 +                      /* basic rates */
1724                   2 + 1 +                      /* DS parameter */
1725                   2 + 2 +                      /* atim */
1726                   2 + 8];                      /* extended rates */
1727        u8 *fake = fake_ie;
1728        struct cfg80211_bss *bss;
1729
1730        lbs_deb_enter(LBS_DEB_CFG80211);
1731
1732        /*
1733         * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1734         * the real IE from the firmware. So we fabricate a fake IE based on
1735         * what the firmware actually sends (sniffed with wireshark).
1736         */
1737        /* Fake SSID IE */
1738        *fake++ = WLAN_EID_SSID;
1739        *fake++ = params->ssid_len;
1740        memcpy(fake, params->ssid, params->ssid_len);
1741        fake += params->ssid_len;
1742        /* Fake supported basic rates IE */
1743        *fake++ = WLAN_EID_SUPP_RATES;
1744        *fake++ = 4;
1745        *fake++ = 0x82;
1746        *fake++ = 0x84;
1747        *fake++ = 0x8b;
1748        *fake++ = 0x96;
1749        /* Fake DS channel IE */
1750        *fake++ = WLAN_EID_DS_PARAMS;
1751        *fake++ = 1;
1752        *fake++ = params->channel->hw_value;
1753        /* Fake IBSS params IE */
1754        *fake++ = WLAN_EID_IBSS_PARAMS;
1755        *fake++ = 2;
1756        *fake++ = 0; /* ATIM=0 */
1757        *fake++ = 0;
1758        /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1759         * but I don't know how this could be checked */
1760        *fake++ = WLAN_EID_EXT_SUPP_RATES;
1761        *fake++ = 8;
1762        *fake++ = 0x0c;
1763        *fake++ = 0x12;
1764        *fake++ = 0x18;
1765        *fake++ = 0x24;
1766        *fake++ = 0x30;
1767        *fake++ = 0x48;
1768        *fake++ = 0x60;
1769        *fake++ = 0x6c;
1770        lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1771
1772        bss = cfg80211_inform_bss(priv->wdev->wiphy,
1773                                  params->channel,
1774                                  bssid,
1775                                  0,
1776                                  capability,
1777                                  params->beacon_interval,
1778                                  fake_ie, fake - fake_ie,
1779                                  0, GFP_KERNEL);
1780        cfg80211_put_bss(bss);
1781
1782        memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1783        priv->wdev->ssid_len = params->ssid_len;
1784
1785        cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
1786
1787        /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1788        priv->connect_status = LBS_CONNECTED;
1789        netif_carrier_on(priv->dev);
1790        if (!priv->tx_pending_len)
1791                netif_wake_queue(priv->dev);
1792
1793        lbs_deb_leave(LBS_DEB_CFG80211);
1794}
1795
1796static int lbs_ibss_join_existing(struct lbs_private *priv,
1797        struct cfg80211_ibss_params *params,
1798        struct cfg80211_bss *bss)
1799{
1800        const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1801        struct cmd_ds_802_11_ad_hoc_join cmd;
1802        u8 preamble = RADIO_PREAMBLE_SHORT;
1803        int ret = 0;
1804
1805        lbs_deb_enter(LBS_DEB_CFG80211);
1806
1807        /* TODO: set preamble based on scan result */
1808        ret = lbs_set_radio(priv, preamble, 1);
1809        if (ret)
1810                goto out;
1811
1812        /*
1813         * Example CMD_802_11_AD_HOC_JOIN command:
1814         *
1815         * command         2c 00         CMD_802_11_AD_HOC_JOIN
1816         * size            65 00
1817         * sequence        xx xx
1818         * result          00 00
1819         * bssid           02 27 27 97 2f 96
1820         * ssid            49 42 53 53 00 00 00 00
1821         *                 00 00 00 00 00 00 00 00
1822         *                 00 00 00 00 00 00 00 00
1823         *                 00 00 00 00 00 00 00 00
1824         * type            02            CMD_BSS_TYPE_IBSS
1825         * beacon period   64 00
1826         * dtim period     00
1827         * timestamp       00 00 00 00 00 00 00 00
1828         * localtime       00 00 00 00 00 00 00 00
1829         * IE DS           03
1830         * IE DS len       01
1831         * IE DS channel   01
1832         * reserveed       00 00 00 00
1833         * IE IBSS         06
1834         * IE IBSS len     02
1835         * IE IBSS atim    00 00
1836         * reserved        00 00 00 00
1837         * capability      02 00
1838         * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1839         * fail timeout    ff 00
1840         * probe delay     00 00
1841         */
1842        memset(&cmd, 0, sizeof(cmd));
1843        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1844
1845        memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1846        memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1847        cmd.bss.type = CMD_BSS_TYPE_IBSS;
1848        cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1849        cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1850        cmd.bss.ds.header.len = 1;
1851        cmd.bss.ds.channel = params->channel->hw_value;
1852        cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1853        cmd.bss.ibss.header.len = 2;
1854        cmd.bss.ibss.atimwindow = 0;
1855        cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1856
1857        /* set rates to the intersection of our rates and the rates in the
1858           bss */
1859        if (!rates_eid) {
1860                lbs_add_rates(cmd.bss.rates);
1861        } else {
1862                int hw, i;
1863                u8 rates_max = rates_eid[1];
1864                u8 *rates = cmd.bss.rates;
1865                for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1866                        u8 hw_rate = lbs_rates[hw].bitrate / 5;
1867                        for (i = 0; i < rates_max; i++) {
1868                                if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1869                                        u8 rate = rates_eid[i+2];
1870                                        if (rate == 0x02 || rate == 0x04 ||
1871                                            rate == 0x0b || rate == 0x16)
1872                                                rate |= 0x80;
1873                                        *rates++ = rate;
1874                                }
1875                        }
1876                }
1877        }
1878
1879        /* Only v8 and below support setting this */
1880        if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1881                cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1882                cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1883        }
1884        ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1885        if (ret)
1886                goto out;
1887
1888        /*
1889         * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1890         *
1891         * response        2c 80
1892         * size            09 00
1893         * sequence        xx xx
1894         * result          00 00
1895         * reserved        00
1896         */
1897        lbs_join_post(priv, params, bss->bssid, bss->capability);
1898
1899 out:
1900        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1901        return ret;
1902}
1903
1904
1905
1906static int lbs_ibss_start_new(struct lbs_private *priv,
1907        struct cfg80211_ibss_params *params)
1908{
1909        struct cmd_ds_802_11_ad_hoc_start cmd;
1910        struct cmd_ds_802_11_ad_hoc_result *resp =
1911                (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1912        u8 preamble = RADIO_PREAMBLE_SHORT;
1913        int ret = 0;
1914        u16 capability;
1915
1916        lbs_deb_enter(LBS_DEB_CFG80211);
1917
1918        ret = lbs_set_radio(priv, preamble, 1);
1919        if (ret)
1920                goto out;
1921
1922        /*
1923         * Example CMD_802_11_AD_HOC_START command:
1924         *
1925         * command         2b 00         CMD_802_11_AD_HOC_START
1926         * size            b1 00
1927         * sequence        xx xx
1928         * result          00 00
1929         * ssid            54 45 53 54 00 00 00 00
1930         *                 00 00 00 00 00 00 00 00
1931         *                 00 00 00 00 00 00 00 00
1932         *                 00 00 00 00 00 00 00 00
1933         * bss type        02
1934         * beacon period   64 00
1935         * dtim period     00
1936         * IE IBSS         06
1937         * IE IBSS len     02
1938         * IE IBSS atim    00 00
1939         * reserved        00 00 00 00
1940         * IE DS           03
1941         * IE DS len       01
1942         * IE DS channel   01
1943         * reserved        00 00 00 00
1944         * probe delay     00 00
1945         * capability      02 00
1946         * rates           82 84 8b 96   (basic rates with have bit 7 set)
1947         *                 0c 12 18 24 30 48 60 6c
1948         * padding         100 bytes
1949         */
1950        memset(&cmd, 0, sizeof(cmd));
1951        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1952        memcpy(cmd.ssid, params->ssid, params->ssid_len);
1953        cmd.bsstype = CMD_BSS_TYPE_IBSS;
1954        cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1955        cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1956        cmd.ibss.header.len = 2;
1957        cmd.ibss.atimwindow = 0;
1958        cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1959        cmd.ds.header.len = 1;
1960        cmd.ds.channel = params->channel->hw_value;
1961        /* Only v8 and below support setting probe delay */
1962        if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1963                cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1964        /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1965        capability = WLAN_CAPABILITY_IBSS;
1966        cmd.capability = cpu_to_le16(capability);
1967        lbs_add_rates(cmd.rates);
1968
1969
1970        ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1971        if (ret)
1972                goto out;
1973
1974        /*
1975         * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1976         *
1977         * response        2b 80
1978         * size            14 00
1979         * sequence        xx xx
1980         * result          00 00
1981         * reserved        00
1982         * bssid           02 2b 7b 0f 86 0e
1983         */
1984        lbs_join_post(priv, params, resp->bssid, capability);
1985
1986 out:
1987        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1988        return ret;
1989}
1990
1991
1992static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1993                struct cfg80211_ibss_params *params)
1994{
1995        struct lbs_private *priv = wiphy_priv(wiphy);
1996        int ret = 0;
1997        struct cfg80211_bss *bss;
1998        DECLARE_SSID_BUF(ssid_buf);
1999
2000        if (dev == priv->mesh_dev)
2001                return -EOPNOTSUPP;
2002
2003        lbs_deb_enter(LBS_DEB_CFG80211);
2004
2005        if (!params->channel) {
2006                ret = -ENOTSUPP;
2007                goto out;
2008        }
2009
2010        ret = lbs_set_channel(priv, params->channel->hw_value);
2011        if (ret)
2012                goto out;
2013
2014        /* Search if someone is beaconing. This assumes that the
2015         * bss list is populated already */
2016        bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
2017                params->ssid, params->ssid_len,
2018                WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
2019
2020        if (bss) {
2021                ret = lbs_ibss_join_existing(priv, params, bss);
2022                cfg80211_put_bss(bss);
2023        } else
2024                ret = lbs_ibss_start_new(priv, params);
2025
2026
2027 out:
2028        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2029        return ret;
2030}
2031
2032
2033static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2034{
2035        struct lbs_private *priv = wiphy_priv(wiphy);
2036        struct cmd_ds_802_11_ad_hoc_stop cmd;
2037        int ret = 0;
2038
2039        if (dev == priv->mesh_dev)
2040                return -EOPNOTSUPP;
2041
2042        lbs_deb_enter(LBS_DEB_CFG80211);
2043
2044        memset(&cmd, 0, sizeof(cmd));
2045        cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2046        ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2047
2048        /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2049        lbs_mac_event_disconnected(priv);
2050
2051        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2052        return ret;
2053}
2054
2055
2056
2057
2058/*
2059 * Initialization
2060 */
2061
2062static struct cfg80211_ops lbs_cfg80211_ops = {
2063        .set_channel = lbs_cfg_set_channel,
2064        .scan = lbs_cfg_scan,
2065        .connect = lbs_cfg_connect,
2066        .disconnect = lbs_cfg_disconnect,
2067        .add_key = lbs_cfg_add_key,
2068        .del_key = lbs_cfg_del_key,
2069        .set_default_key = lbs_cfg_set_default_key,
2070        .get_station = lbs_cfg_get_station,
2071        .dump_survey = lbs_get_survey,
2072        .change_virtual_intf = lbs_change_intf,
2073        .join_ibss = lbs_join_ibss,
2074        .leave_ibss = lbs_leave_ibss,
2075};
2076
2077
2078/*
2079 * At this time lbs_private *priv doesn't even exist, so we just allocate
2080 * memory and don't initialize the wiphy further. This is postponed until we
2081 * can talk to the firmware and happens at registration time in
2082 * lbs_cfg_wiphy_register().
2083 */
2084struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2085{
2086        int ret = 0;
2087        struct wireless_dev *wdev;
2088
2089        lbs_deb_enter(LBS_DEB_CFG80211);
2090
2091        wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2092        if (!wdev) {
2093                dev_err(dev, "cannot allocate wireless device\n");
2094                return ERR_PTR(-ENOMEM);
2095        }
2096
2097        wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2098        if (!wdev->wiphy) {
2099                dev_err(dev, "cannot allocate wiphy\n");
2100                ret = -ENOMEM;
2101                goto err_wiphy_new;
2102        }
2103
2104        lbs_deb_leave(LBS_DEB_CFG80211);
2105        return wdev;
2106
2107 err_wiphy_new:
2108        kfree(wdev);
2109        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2110        return ERR_PTR(ret);
2111}
2112
2113
2114static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2115{
2116        struct region_code_mapping {
2117                const char *cn;
2118                int code;
2119        };
2120
2121        /* Section 5.17.2 */
2122        static const struct region_code_mapping regmap[] = {
2123                {"US ", 0x10}, /* US FCC */
2124                {"CA ", 0x20}, /* Canada */
2125                {"EU ", 0x30}, /* ETSI   */
2126                {"ES ", 0x31}, /* Spain  */
2127                {"FR ", 0x32}, /* France */
2128                {"JP ", 0x40}, /* Japan  */
2129        };
2130        size_t i;
2131
2132        lbs_deb_enter(LBS_DEB_CFG80211);
2133
2134        for (i = 0; i < ARRAY_SIZE(regmap); i++)
2135                if (regmap[i].code == priv->regioncode) {
2136                        regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2137                        break;
2138                }
2139
2140        lbs_deb_leave(LBS_DEB_CFG80211);
2141}
2142
2143
2144/*
2145 * This function get's called after lbs_setup_firmware() determined the
2146 * firmware capabities. So we can setup the wiphy according to our
2147 * hardware/firmware.
2148 */
2149int lbs_cfg_register(struct lbs_private *priv)
2150{
2151        struct wireless_dev *wdev = priv->wdev;
2152        int ret;
2153
2154        lbs_deb_enter(LBS_DEB_CFG80211);
2155
2156        wdev->wiphy->max_scan_ssids = 1;
2157        wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2158
2159        wdev->wiphy->interface_modes =
2160                        BIT(NL80211_IFTYPE_STATION) |
2161                        BIT(NL80211_IFTYPE_ADHOC);
2162        if (lbs_rtap_supported(priv))
2163                wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2164        if (lbs_mesh_activated(priv))
2165                wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2166
2167        wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
2168
2169        /*
2170         * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2171         * never seen a firmware without WPA
2172         */
2173        wdev->wiphy->cipher_suites = cipher_suites;
2174        wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2175        wdev->wiphy->reg_notifier = lbs_reg_notifier;
2176
2177        ret = wiphy_register(wdev->wiphy);
2178        if (ret < 0)
2179                pr_err("cannot register wiphy device\n");
2180
2181        priv->wiphy_registered = true;
2182
2183        ret = register_netdev(priv->dev);
2184        if (ret)
2185                pr_err("cannot register network device\n");
2186
2187        INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2188
2189        lbs_cfg_set_regulatory_hint(priv);
2190
2191        lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2192        return ret;
2193}
2194
2195int lbs_reg_notifier(struct wiphy *wiphy,
2196                struct regulatory_request *request)
2197{
2198        struct lbs_private *priv = wiphy_priv(wiphy);
2199        int ret;
2200
2201        lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
2202                        "callback for domain %c%c\n", request->alpha2[0],
2203                        request->alpha2[1]);
2204
2205        ret = lbs_set_11d_domain_info(priv, request, wiphy->bands);
2206
2207        lbs_deb_leave(LBS_DEB_CFG80211);
2208        return ret;
2209}
2210
2211void lbs_scan_deinit(struct lbs_private *priv)
2212{
2213        lbs_deb_enter(LBS_DEB_CFG80211);
2214        cancel_delayed_work_sync(&priv->scan_work);
2215}
2216
2217
2218void lbs_cfg_free(struct lbs_private *priv)
2219{
2220        struct wireless_dev *wdev = priv->wdev;
2221
2222        lbs_deb_enter(LBS_DEB_CFG80211);
2223
2224        if (!wdev)
2225                return;
2226
2227        if (priv->wiphy_registered)
2228                wiphy_unregister(wdev->wiphy);
2229
2230        if (wdev->wiphy)
2231                wiphy_free(wdev->wiphy);
2232
2233        kfree(wdev);
2234}
2235
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.