linux/drivers/net/wireless/rndis_wlan.c
<<
>>
Prefs
   1/*
   2 * Driver for RNDIS based wireless USB devices.
   3 *
   4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
   5 * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20 *
  21 *  Portions of this file are based on NDISwrapper project,
  22 *  Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
  23 *  http://ndiswrapper.sourceforge.net/
  24 */
  25
  26// #define      DEBUG                   // error path messages, extra info
  27// #define      VERBOSE                 // more; success messages
  28
  29#include <linux/module.h>
  30#include <linux/init.h>
  31#include <linux/netdevice.h>
  32#include <linux/etherdevice.h>
  33#include <linux/ethtool.h>
  34#include <linux/workqueue.h>
  35#include <linux/mutex.h>
  36#include <linux/mii.h>
  37#include <linux/usb.h>
  38#include <linux/usb/cdc.h>
  39#include <linux/ieee80211.h>
  40#include <linux/if_arp.h>
  41#include <linux/ctype.h>
  42#include <linux/spinlock.h>
  43#include <linux/slab.h>
  44#include <net/cfg80211.h>
  45#include <linux/usb/usbnet.h>
  46#include <linux/usb/rndis_host.h>
  47
  48
  49/* NOTE: All these are settings for Broadcom chipset */
  50static char modparam_country[4] = "EU";
  51module_param_string(country, modparam_country, 4, 0444);
  52MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  53
  54static int modparam_frameburst = 1;
  55module_param_named(frameburst, modparam_frameburst, int, 0444);
  56MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  57
  58static int modparam_afterburner = 0;
  59module_param_named(afterburner, modparam_afterburner, int, 0444);
  60MODULE_PARM_DESC(afterburner,
  61        "enable afterburner aka '125 High Speed Mode' (default: off)");
  62
  63static int modparam_power_save = 0;
  64module_param_named(power_save, modparam_power_save, int, 0444);
  65MODULE_PARM_DESC(power_save,
  66        "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  67
  68static int modparam_power_output = 3;
  69module_param_named(power_output, modparam_power_output, int, 0444);
  70MODULE_PARM_DESC(power_output,
  71        "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  72
  73static int modparam_roamtrigger = -70;
  74module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  75MODULE_PARM_DESC(roamtrigger,
  76        "set roaming dBm trigger: -80=optimize for distance, "
  77                                "-60=bandwidth (default: -70)");
  78
  79static int modparam_roamdelta = 1;
  80module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  81MODULE_PARM_DESC(roamdelta,
  82        "set roaming tendency: 0=aggressive, 1=moderate, "
  83                                "2=conservative (default: moderate)");
  84
  85static int modparam_workaround_interval;
  86module_param_named(workaround_interval, modparam_workaround_interval,
  87                                                        int, 0444);
  88MODULE_PARM_DESC(workaround_interval,
  89        "set stall workaround interval in msecs (0=disabled) (default: 0)");
  90
  91
  92/* various RNDIS OID defs */
  93#define OID_GEN_LINK_SPEED                      cpu_to_le32(0x00010107)
  94#define OID_GEN_RNDIS_CONFIG_PARAMETER          cpu_to_le32(0x0001021b)
  95
  96#define OID_GEN_XMIT_OK                         cpu_to_le32(0x00020101)
  97#define OID_GEN_RCV_OK                          cpu_to_le32(0x00020102)
  98#define OID_GEN_XMIT_ERROR                      cpu_to_le32(0x00020103)
  99#define OID_GEN_RCV_ERROR                       cpu_to_le32(0x00020104)
 100#define OID_GEN_RCV_NO_BUFFER                   cpu_to_le32(0x00020105)
 101
 102#define OID_802_3_CURRENT_ADDRESS               cpu_to_le32(0x01010102)
 103#define OID_802_3_MULTICAST_LIST                cpu_to_le32(0x01010103)
 104#define OID_802_3_MAXIMUM_LIST_SIZE             cpu_to_le32(0x01010104)
 105
 106#define OID_802_11_BSSID                        cpu_to_le32(0x0d010101)
 107#define OID_802_11_SSID                         cpu_to_le32(0x0d010102)
 108#define OID_802_11_INFRASTRUCTURE_MODE          cpu_to_le32(0x0d010108)
 109#define OID_802_11_ADD_WEP                      cpu_to_le32(0x0d010113)
 110#define OID_802_11_REMOVE_WEP                   cpu_to_le32(0x0d010114)
 111#define OID_802_11_DISASSOCIATE                 cpu_to_le32(0x0d010115)
 112#define OID_802_11_AUTHENTICATION_MODE          cpu_to_le32(0x0d010118)
 113#define OID_802_11_PRIVACY_FILTER               cpu_to_le32(0x0d010119)
 114#define OID_802_11_BSSID_LIST_SCAN              cpu_to_le32(0x0d01011a)
 115#define OID_802_11_ENCRYPTION_STATUS            cpu_to_le32(0x0d01011b)
 116#define OID_802_11_ADD_KEY                      cpu_to_le32(0x0d01011d)
 117#define OID_802_11_REMOVE_KEY                   cpu_to_le32(0x0d01011e)
 118#define OID_802_11_ASSOCIATION_INFORMATION      cpu_to_le32(0x0d01011f)
 119#define OID_802_11_CAPABILITY                   cpu_to_le32(0x0d010122)
 120#define OID_802_11_PMKID                        cpu_to_le32(0x0d010123)
 121#define OID_802_11_NETWORK_TYPES_SUPPORTED      cpu_to_le32(0x0d010203)
 122#define OID_802_11_NETWORK_TYPE_IN_USE          cpu_to_le32(0x0d010204)
 123#define OID_802_11_TX_POWER_LEVEL               cpu_to_le32(0x0d010205)
 124#define OID_802_11_RSSI                         cpu_to_le32(0x0d010206)
 125#define OID_802_11_RSSI_TRIGGER                 cpu_to_le32(0x0d010207)
 126#define OID_802_11_FRAGMENTATION_THRESHOLD      cpu_to_le32(0x0d010209)
 127#define OID_802_11_RTS_THRESHOLD                cpu_to_le32(0x0d01020a)
 128#define OID_802_11_SUPPORTED_RATES              cpu_to_le32(0x0d01020e)
 129#define OID_802_11_CONFIGURATION                cpu_to_le32(0x0d010211)
 130#define OID_802_11_POWER_MODE                   cpu_to_le32(0x0d010216)
 131#define OID_802_11_BSSID_LIST                   cpu_to_le32(0x0d010217)
 132
 133
 134/* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
 135#define WL_NOISE        -96     /* typical noise level in dBm */
 136#define WL_SIGMAX       -32     /* typical maximum signal level in dBm */
 137
 138
 139/* Assume that Broadcom 4320 (only chipset at time of writing known to be
 140 * based on wireless rndis) has default txpower of 13dBm.
 141 * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
 142 *  100% : 20 mW ~ 13dBm
 143 *   75% : 15 mW ~ 12dBm
 144 *   50% : 10 mW ~ 10dBm
 145 *   25% :  5 mW ~  7dBm
 146 */
 147#define BCM4320_DEFAULT_TXPOWER_DBM_100 13
 148#define BCM4320_DEFAULT_TXPOWER_DBM_75  12
 149#define BCM4320_DEFAULT_TXPOWER_DBM_50  10
 150#define BCM4320_DEFAULT_TXPOWER_DBM_25  7
 151
 152
 153/* codes for "status" field of completion messages */
 154#define RNDIS_STATUS_ADAPTER_NOT_READY          cpu_to_le32(0xc0010011)
 155#define RNDIS_STATUS_ADAPTER_NOT_OPEN           cpu_to_le32(0xc0010012)
 156
 157
 158/* Known device types */
 159#define RNDIS_UNKNOWN   0
 160#define RNDIS_BCM4320A  1
 161#define RNDIS_BCM4320B  2
 162
 163
 164/* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
 165 * slightly modified for datatype endianess, etc
 166 */
 167#define NDIS_802_11_LENGTH_SSID 32
 168#define NDIS_802_11_LENGTH_RATES 8
 169#define NDIS_802_11_LENGTH_RATES_EX 16
 170
 171enum ndis_80211_net_type {
 172        NDIS_80211_TYPE_FREQ_HOP,
 173        NDIS_80211_TYPE_DIRECT_SEQ,
 174        NDIS_80211_TYPE_OFDM_A,
 175        NDIS_80211_TYPE_OFDM_G
 176};
 177
 178enum ndis_80211_net_infra {
 179        NDIS_80211_INFRA_ADHOC,
 180        NDIS_80211_INFRA_INFRA,
 181        NDIS_80211_INFRA_AUTO_UNKNOWN
 182};
 183
 184enum ndis_80211_auth_mode {
 185        NDIS_80211_AUTH_OPEN,
 186        NDIS_80211_AUTH_SHARED,
 187        NDIS_80211_AUTH_AUTO_SWITCH,
 188        NDIS_80211_AUTH_WPA,
 189        NDIS_80211_AUTH_WPA_PSK,
 190        NDIS_80211_AUTH_WPA_NONE,
 191        NDIS_80211_AUTH_WPA2,
 192        NDIS_80211_AUTH_WPA2_PSK
 193};
 194
 195enum ndis_80211_encr_status {
 196        NDIS_80211_ENCR_WEP_ENABLED,
 197        NDIS_80211_ENCR_DISABLED,
 198        NDIS_80211_ENCR_WEP_KEY_ABSENT,
 199        NDIS_80211_ENCR_NOT_SUPPORTED,
 200        NDIS_80211_ENCR_TKIP_ENABLED,
 201        NDIS_80211_ENCR_TKIP_KEY_ABSENT,
 202        NDIS_80211_ENCR_CCMP_ENABLED,
 203        NDIS_80211_ENCR_CCMP_KEY_ABSENT
 204};
 205
 206enum ndis_80211_priv_filter {
 207        NDIS_80211_PRIV_ACCEPT_ALL,
 208        NDIS_80211_PRIV_8021X_WEP
 209};
 210
 211enum ndis_80211_status_type {
 212        NDIS_80211_STATUSTYPE_AUTHENTICATION,
 213        NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
 214        NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
 215        NDIS_80211_STATUSTYPE_RADIOSTATE,
 216};
 217
 218enum ndis_80211_media_stream_mode {
 219        NDIS_80211_MEDIA_STREAM_OFF,
 220        NDIS_80211_MEDIA_STREAM_ON
 221};
 222
 223enum ndis_80211_radio_status {
 224        NDIS_80211_RADIO_STATUS_ON,
 225        NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
 226        NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
 227};
 228
 229enum ndis_80211_addkey_bits {
 230        NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
 231        NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
 232        NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
 233        NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
 234};
 235
 236enum ndis_80211_addwep_bits {
 237        NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
 238        NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
 239};
 240
 241enum ndis_80211_power_mode {
 242        NDIS_80211_POWER_MODE_CAM,
 243        NDIS_80211_POWER_MODE_MAX_PSP,
 244        NDIS_80211_POWER_MODE_FAST_PSP,
 245};
 246
 247enum ndis_80211_pmkid_cand_list_flag_bits {
 248        NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
 249};
 250
 251struct ndis_80211_auth_request {
 252        __le32 length;
 253        u8 bssid[6];
 254        u8 padding[2];
 255        __le32 flags;
 256} __packed;
 257
 258struct ndis_80211_pmkid_candidate {
 259        u8 bssid[6];
 260        u8 padding[2];
 261        __le32 flags;
 262} __packed;
 263
 264struct ndis_80211_pmkid_cand_list {
 265        __le32 version;
 266        __le32 num_candidates;
 267        struct ndis_80211_pmkid_candidate candidate_list[0];
 268} __packed;
 269
 270struct ndis_80211_status_indication {
 271        __le32 status_type;
 272        union {
 273                __le32                                  media_stream_mode;
 274                __le32                                  radio_status;
 275                struct ndis_80211_auth_request          auth_request[0];
 276                struct ndis_80211_pmkid_cand_list       cand_list;
 277        } u;
 278} __packed;
 279
 280struct ndis_80211_ssid {
 281        __le32 length;
 282        u8 essid[NDIS_802_11_LENGTH_SSID];
 283} __packed;
 284
 285struct ndis_80211_conf_freq_hop {
 286        __le32 length;
 287        __le32 hop_pattern;
 288        __le32 hop_set;
 289        __le32 dwell_time;
 290} __packed;
 291
 292struct ndis_80211_conf {
 293        __le32 length;
 294        __le32 beacon_period;
 295        __le32 atim_window;
 296        __le32 ds_config;
 297        struct ndis_80211_conf_freq_hop fh_config;
 298} __packed;
 299
 300struct ndis_80211_bssid_ex {
 301        __le32 length;
 302        u8 mac[6];
 303        u8 padding[2];
 304        struct ndis_80211_ssid ssid;
 305        __le32 privacy;
 306        __le32 rssi;
 307        __le32 net_type;
 308        struct ndis_80211_conf config;
 309        __le32 net_infra;
 310        u8 rates[NDIS_802_11_LENGTH_RATES_EX];
 311        __le32 ie_length;
 312        u8 ies[0];
 313} __packed;
 314
 315struct ndis_80211_bssid_list_ex {
 316        __le32 num_items;
 317        struct ndis_80211_bssid_ex bssid[0];
 318} __packed;
 319
 320struct ndis_80211_fixed_ies {
 321        u8 timestamp[8];
 322        __le16 beacon_interval;
 323        __le16 capabilities;
 324} __packed;
 325
 326struct ndis_80211_wep_key {
 327        __le32 size;
 328        __le32 index;
 329        __le32 length;
 330        u8 material[32];
 331} __packed;
 332
 333struct ndis_80211_key {
 334        __le32 size;
 335        __le32 index;
 336        __le32 length;
 337        u8 bssid[6];
 338        u8 padding[6];
 339        u8 rsc[8];
 340        u8 material[32];
 341} __packed;
 342
 343struct ndis_80211_remove_key {
 344        __le32 size;
 345        __le32 index;
 346        u8 bssid[6];
 347        u8 padding[2];
 348} __packed;
 349
 350struct ndis_config_param {
 351        __le32 name_offs;
 352        __le32 name_length;
 353        __le32 type;
 354        __le32 value_offs;
 355        __le32 value_length;
 356} __packed;
 357
 358struct ndis_80211_assoc_info {
 359        __le32 length;
 360        __le16 req_ies;
 361        struct req_ie {
 362                __le16 capa;
 363                __le16 listen_interval;
 364                u8 cur_ap_address[6];
 365        } req_ie;
 366        __le32 req_ie_length;
 367        __le32 offset_req_ies;
 368        __le16 resp_ies;
 369        struct resp_ie {
 370                __le16 capa;
 371                __le16 status_code;
 372                __le16 assoc_id;
 373        } resp_ie;
 374        __le32 resp_ie_length;
 375        __le32 offset_resp_ies;
 376} __packed;
 377
 378struct ndis_80211_auth_encr_pair {
 379        __le32 auth_mode;
 380        __le32 encr_mode;
 381} __packed;
 382
 383struct ndis_80211_capability {
 384        __le32 length;
 385        __le32 version;
 386        __le32 num_pmkids;
 387        __le32 num_auth_encr_pair;
 388        struct ndis_80211_auth_encr_pair auth_encr_pair[0];
 389} __packed;
 390
 391struct ndis_80211_bssid_info {
 392        u8 bssid[6];
 393        u8 pmkid[16];
 394} __packed;
 395
 396struct ndis_80211_pmkid {
 397        __le32 length;
 398        __le32 bssid_info_count;
 399        struct ndis_80211_bssid_info bssid_info[0];
 400} __packed;
 401
 402/*
 403 *  private data
 404 */
 405#define CAP_MODE_80211A         1
 406#define CAP_MODE_80211B         2
 407#define CAP_MODE_80211G         4
 408#define CAP_MODE_MASK           7
 409
 410#define WORK_LINK_UP            (1<<0)
 411#define WORK_LINK_DOWN          (1<<1)
 412#define WORK_SET_MULTICAST_LIST (1<<2)
 413
 414#define RNDIS_WLAN_ALG_NONE     0
 415#define RNDIS_WLAN_ALG_WEP      (1<<0)
 416#define RNDIS_WLAN_ALG_TKIP     (1<<1)
 417#define RNDIS_WLAN_ALG_CCMP     (1<<2)
 418
 419#define RNDIS_WLAN_NUM_KEYS             4
 420#define RNDIS_WLAN_KEY_MGMT_NONE        0
 421#define RNDIS_WLAN_KEY_MGMT_802_1X      (1<<0)
 422#define RNDIS_WLAN_KEY_MGMT_PSK         (1<<1)
 423
 424#define COMMAND_BUFFER_SIZE     (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
 425
 426static const struct ieee80211_channel rndis_channels[] = {
 427        { .center_freq = 2412 },
 428        { .center_freq = 2417 },
 429        { .center_freq = 2422 },
 430        { .center_freq = 2427 },
 431        { .center_freq = 2432 },
 432        { .center_freq = 2437 },
 433        { .center_freq = 2442 },
 434        { .center_freq = 2447 },
 435        { .center_freq = 2452 },
 436        { .center_freq = 2457 },
 437        { .center_freq = 2462 },
 438        { .center_freq = 2467 },
 439        { .center_freq = 2472 },
 440        { .center_freq = 2484 },
 441};
 442
 443static const struct ieee80211_rate rndis_rates[] = {
 444        { .bitrate = 10 },
 445        { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 446        { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 447        { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 448        { .bitrate = 60 },
 449        { .bitrate = 90 },
 450        { .bitrate = 120 },
 451        { .bitrate = 180 },
 452        { .bitrate = 240 },
 453        { .bitrate = 360 },
 454        { .bitrate = 480 },
 455        { .bitrate = 540 }
 456};
 457
 458static const u32 rndis_cipher_suites[] = {
 459        WLAN_CIPHER_SUITE_WEP40,
 460        WLAN_CIPHER_SUITE_WEP104,
 461        WLAN_CIPHER_SUITE_TKIP,
 462        WLAN_CIPHER_SUITE_CCMP,
 463};
 464
 465struct rndis_wlan_encr_key {
 466        int len;
 467        u32 cipher;
 468        u8 material[32];
 469        u8 bssid[ETH_ALEN];
 470        bool pairwise;
 471        bool tx_key;
 472};
 473
 474/* RNDIS device private data */
 475struct rndis_wlan_private {
 476        struct usbnet *usbdev;
 477
 478        struct wireless_dev wdev;
 479
 480        struct cfg80211_scan_request *scan_request;
 481
 482        struct workqueue_struct *workqueue;
 483        struct delayed_work dev_poller_work;
 484        struct delayed_work scan_work;
 485        struct work_struct work;
 486        struct mutex command_lock;
 487        unsigned long work_pending;
 488        int last_qual;
 489        s32 cqm_rssi_thold;
 490        u32 cqm_rssi_hyst;
 491        int last_cqm_event_rssi;
 492
 493        struct ieee80211_supported_band band;
 494        struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
 495        struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
 496        u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
 497
 498        int device_type;
 499        int caps;
 500        int multicast_size;
 501
 502        /* module parameters */
 503        char param_country[4];
 504        int  param_frameburst;
 505        int  param_afterburner;
 506        int  param_power_save;
 507        int  param_power_output;
 508        int  param_roamtrigger;
 509        int  param_roamdelta;
 510        u32  param_workaround_interval;
 511
 512        /* hardware state */
 513        bool radio_on;
 514        int power_mode;
 515        int infra_mode;
 516        bool connected;
 517        u8 bssid[ETH_ALEN];
 518        __le32 current_command_oid;
 519
 520        /* encryption stuff */
 521        u8 encr_tx_key_index;
 522        struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
 523        int  wpa_version;
 524
 525        u8 command_buffer[COMMAND_BUFFER_SIZE];
 526};
 527
 528/*
 529 * cfg80211 ops
 530 */
 531static int rndis_change_virtual_intf(struct wiphy *wiphy,
 532                                        struct net_device *dev,
 533                                        enum nl80211_iftype type, u32 *flags,
 534                                        struct vif_params *params);
 535
 536static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
 537                        struct cfg80211_scan_request *request);
 538
 539static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
 540
 541static int rndis_set_tx_power(struct wiphy *wiphy,
 542                              enum nl80211_tx_power_setting type,
 543                              int mbm);
 544static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
 545
 546static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
 547                                struct cfg80211_connect_params *sme);
 548
 549static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
 550                                u16 reason_code);
 551
 552static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
 553                                        struct cfg80211_ibss_params *params);
 554
 555static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
 556
 557static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
 558        struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
 559
 560static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
 561                         u8 key_index, bool pairwise, const u8 *mac_addr,
 562                         struct key_params *params);
 563
 564static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
 565                         u8 key_index, bool pairwise, const u8 *mac_addr);
 566
 567static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
 568                                 u8 key_index, bool unicast, bool multicast);
 569
 570static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
 571                                        u8 *mac, struct station_info *sinfo);
 572
 573static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
 574                               int idx, u8 *mac, struct station_info *sinfo);
 575
 576static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
 577                                struct cfg80211_pmksa *pmksa);
 578
 579static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
 580                                struct cfg80211_pmksa *pmksa);
 581
 582static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
 583
 584static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
 585                                bool enabled, int timeout);
 586
 587static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
 588                                        struct net_device *dev,
 589                                        s32 rssi_thold, u32 rssi_hyst);
 590
 591static const struct cfg80211_ops rndis_config_ops = {
 592        .change_virtual_intf = rndis_change_virtual_intf,
 593        .scan = rndis_scan,
 594        .set_wiphy_params = rndis_set_wiphy_params,
 595        .set_tx_power = rndis_set_tx_power,
 596        .get_tx_power = rndis_get_tx_power,
 597        .connect = rndis_connect,
 598        .disconnect = rndis_disconnect,
 599        .join_ibss = rndis_join_ibss,
 600        .leave_ibss = rndis_leave_ibss,
 601        .set_channel = rndis_set_channel,
 602        .add_key = rndis_add_key,
 603        .del_key = rndis_del_key,
 604        .set_default_key = rndis_set_default_key,
 605        .get_station = rndis_get_station,
 606        .dump_station = rndis_dump_station,
 607        .set_pmksa = rndis_set_pmksa,
 608        .del_pmksa = rndis_del_pmksa,
 609        .flush_pmksa = rndis_flush_pmksa,
 610        .set_power_mgmt = rndis_set_power_mgmt,
 611        .set_cqm_rssi_config = rndis_set_cqm_rssi_config,
 612};
 613
 614static void *rndis_wiphy_privid = &rndis_wiphy_privid;
 615
 616
 617static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
 618{
 619        return (struct rndis_wlan_private *)dev->driver_priv;
 620}
 621
 622static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
 623{
 624        switch (priv->param_power_output) {
 625        default:
 626        case 3:
 627                return BCM4320_DEFAULT_TXPOWER_DBM_100;
 628        case 2:
 629                return BCM4320_DEFAULT_TXPOWER_DBM_75;
 630        case 1:
 631                return BCM4320_DEFAULT_TXPOWER_DBM_50;
 632        case 0:
 633                return BCM4320_DEFAULT_TXPOWER_DBM_25;
 634        }
 635}
 636
 637static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
 638{
 639        int cipher = priv->encr_keys[idx].cipher;
 640
 641        return (cipher == WLAN_CIPHER_SUITE_CCMP ||
 642                cipher == WLAN_CIPHER_SUITE_TKIP);
 643}
 644
 645static int rndis_cipher_to_alg(u32 cipher)
 646{
 647        switch (cipher) {
 648        default:
 649                return RNDIS_WLAN_ALG_NONE;
 650        case WLAN_CIPHER_SUITE_WEP40:
 651        case WLAN_CIPHER_SUITE_WEP104:
 652                return RNDIS_WLAN_ALG_WEP;
 653        case WLAN_CIPHER_SUITE_TKIP:
 654                return RNDIS_WLAN_ALG_TKIP;
 655        case WLAN_CIPHER_SUITE_CCMP:
 656                return RNDIS_WLAN_ALG_CCMP;
 657        }
 658}
 659
 660static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
 661{
 662        switch (akm_suite) {
 663        default:
 664                return RNDIS_WLAN_KEY_MGMT_NONE;
 665        case WLAN_AKM_SUITE_8021X:
 666                return RNDIS_WLAN_KEY_MGMT_802_1X;
 667        case WLAN_AKM_SUITE_PSK:
 668                return RNDIS_WLAN_KEY_MGMT_PSK;
 669        }
 670}
 671
 672#ifdef DEBUG
 673static const char *oid_to_string(__le32 oid)
 674{
 675        switch (oid) {
 676#define OID_STR(oid) case oid: return(#oid)
 677                /* from rndis_host.h */
 678                OID_STR(OID_802_3_PERMANENT_ADDRESS);
 679                OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
 680                OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
 681                OID_STR(OID_GEN_PHYSICAL_MEDIUM);
 682
 683                /* from rndis_wlan.c */
 684                OID_STR(OID_GEN_LINK_SPEED);
 685                OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
 686
 687                OID_STR(OID_GEN_XMIT_OK);
 688                OID_STR(OID_GEN_RCV_OK);
 689                OID_STR(OID_GEN_XMIT_ERROR);
 690                OID_STR(OID_GEN_RCV_ERROR);
 691                OID_STR(OID_GEN_RCV_NO_BUFFER);
 692
 693                OID_STR(OID_802_3_CURRENT_ADDRESS);
 694                OID_STR(OID_802_3_MULTICAST_LIST);
 695                OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
 696
 697                OID_STR(OID_802_11_BSSID);
 698                OID_STR(OID_802_11_SSID);
 699                OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
 700                OID_STR(OID_802_11_ADD_WEP);
 701                OID_STR(OID_802_11_REMOVE_WEP);
 702                OID_STR(OID_802_11_DISASSOCIATE);
 703                OID_STR(OID_802_11_AUTHENTICATION_MODE);
 704                OID_STR(OID_802_11_PRIVACY_FILTER);
 705                OID_STR(OID_802_11_BSSID_LIST_SCAN);
 706                OID_STR(OID_802_11_ENCRYPTION_STATUS);
 707                OID_STR(OID_802_11_ADD_KEY);
 708                OID_STR(OID_802_11_REMOVE_KEY);
 709                OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
 710                OID_STR(OID_802_11_CAPABILITY);
 711                OID_STR(OID_802_11_PMKID);
 712                OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
 713                OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
 714                OID_STR(OID_802_11_TX_POWER_LEVEL);
 715                OID_STR(OID_802_11_RSSI);
 716                OID_STR(OID_802_11_RSSI_TRIGGER);
 717                OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
 718                OID_STR(OID_802_11_RTS_THRESHOLD);
 719                OID_STR(OID_802_11_SUPPORTED_RATES);
 720                OID_STR(OID_802_11_CONFIGURATION);
 721                OID_STR(OID_802_11_POWER_MODE);
 722                OID_STR(OID_802_11_BSSID_LIST);
 723#undef OID_STR
 724        }
 725
 726        return "?";
 727}
 728#else
 729static const char *oid_to_string(__le32 oid)
 730{
 731        return "?";
 732}
 733#endif
 734
 735/* translate error code */
 736static int rndis_error_status(__le32 rndis_status)
 737{
 738        int ret = -EINVAL;
 739        switch (rndis_status) {
 740        case RNDIS_STATUS_SUCCESS:
 741                ret = 0;
 742                break;
 743        case RNDIS_STATUS_FAILURE:
 744        case RNDIS_STATUS_INVALID_DATA:
 745                ret = -EINVAL;
 746                break;
 747        case RNDIS_STATUS_NOT_SUPPORTED:
 748                ret = -EOPNOTSUPP;
 749                break;
 750        case RNDIS_STATUS_ADAPTER_NOT_READY:
 751        case RNDIS_STATUS_ADAPTER_NOT_OPEN:
 752                ret = -EBUSY;
 753                break;
 754        }
 755        return ret;
 756}
 757
 758static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
 759{
 760        struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
 761        union {
 762                void                    *buf;
 763                struct rndis_msg_hdr    *header;
 764                struct rndis_query      *get;
 765                struct rndis_query_c    *get_c;
 766        } u;
 767        int ret, buflen;
 768        int resplen, respoffs, copylen;
 769
 770        buflen = *len + sizeof(*u.get);
 771        if (buflen < CONTROL_BUFFER_SIZE)
 772                buflen = CONTROL_BUFFER_SIZE;
 773
 774        if (buflen > COMMAND_BUFFER_SIZE) {
 775                u.buf = kmalloc(buflen, GFP_KERNEL);
 776                if (!u.buf)
 777                        return -ENOMEM;
 778        } else {
 779                u.buf = priv->command_buffer;
 780        }
 781
 782        mutex_lock(&priv->command_lock);
 783
 784        memset(u.get, 0, sizeof *u.get);
 785        u.get->msg_type = RNDIS_MSG_QUERY;
 786        u.get->msg_len = cpu_to_le32(sizeof *u.get);
 787        u.get->oid = oid;
 788
 789        priv->current_command_oid = oid;
 790        ret = rndis_command(dev, u.header, buflen);
 791        priv->current_command_oid = 0;
 792        if (ret < 0)
 793                netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
 794                           __func__, oid_to_string(oid), ret,
 795                           le32_to_cpu(u.get_c->status));
 796
 797        if (ret == 0) {
 798                resplen = le32_to_cpu(u.get_c->len);
 799                respoffs = le32_to_cpu(u.get_c->offset) + 8;
 800
 801                if (respoffs > buflen) {
 802                        /* Device returned data offset outside buffer, error. */
 803                        netdev_dbg(dev->net, "%s(%s): received invalid "
 804                                "data offset: %d > %d\n", __func__,
 805                                oid_to_string(oid), respoffs, buflen);
 806
 807                        ret = -EINVAL;
 808                        goto exit_unlock;
 809                }
 810
 811                if ((resplen + respoffs) > buflen) {
 812                        /* Device would have returned more data if buffer would
 813                         * have been big enough. Copy just the bits that we got.
 814                         */
 815                        copylen = buflen - respoffs;
 816                } else {
 817                        copylen = resplen;
 818                }
 819
 820                if (copylen > *len)
 821                        copylen = *len;
 822
 823                memcpy(data, u.buf + respoffs, copylen);
 824
 825                *len = resplen;
 826
 827                ret = rndis_error_status(u.get_c->status);
 828                if (ret < 0)
 829                        netdev_dbg(dev->net, "%s(%s): device returned error,  0x%08x (%d)\n",
 830                                   __func__, oid_to_string(oid),
 831                                   le32_to_cpu(u.get_c->status), ret);
 832        }
 833
 834exit_unlock:
 835        mutex_unlock(&priv->command_lock);
 836
 837        if (u.buf != priv->command_buffer)
 838                kfree(u.buf);
 839        return ret;
 840}
 841
 842static int rndis_set_oid(struct usbnet *dev, __le32 oid, const void *data,
 843                         int len)
 844{
 845        struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
 846        union {
 847                void                    *buf;
 848                struct rndis_msg_hdr    *header;
 849                struct rndis_set        *set;
 850                struct rndis_set_c      *set_c;
 851        } u;
 852        int ret, buflen;
 853
 854        buflen = len + sizeof(*u.set);
 855        if (buflen < CONTROL_BUFFER_SIZE)
 856                buflen = CONTROL_BUFFER_SIZE;
 857
 858        if (buflen > COMMAND_BUFFER_SIZE) {
 859                u.buf = kmalloc(buflen, GFP_KERNEL);
 860                if (!u.buf)
 861                        return -ENOMEM;
 862        } else {
 863                u.buf = priv->command_buffer;
 864        }
 865
 866        mutex_lock(&priv->command_lock);
 867
 868        memset(u.set, 0, sizeof *u.set);
 869        u.set->msg_type = RNDIS_MSG_SET;
 870        u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
 871        u.set->oid = oid;
 872        u.set->len = cpu_to_le32(len);
 873        u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
 874        u.set->handle = cpu_to_le32(0);
 875        memcpy(u.buf + sizeof(*u.set), data, len);
 876
 877        priv->current_command_oid = oid;
 878        ret = rndis_command(dev, u.header, buflen);
 879        priv->current_command_oid = 0;
 880        if (ret < 0)
 881                netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
 882                           __func__, oid_to_string(oid), ret,
 883                           le32_to_cpu(u.set_c->status));
 884
 885        if (ret == 0) {
 886                ret = rndis_error_status(u.set_c->status);
 887
 888                if (ret < 0)
 889                        netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
 890                                   __func__, oid_to_string(oid),
 891                                   le32_to_cpu(u.set_c->status), ret);
 892        }
 893
 894        mutex_unlock(&priv->command_lock);
 895
 896        if (u.buf != priv->command_buffer)
 897                kfree(u.buf);
 898        return ret;
 899}
 900
 901static int rndis_reset(struct usbnet *usbdev)
 902{
 903        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
 904        struct rndis_reset *reset;
 905        int ret;
 906
 907        mutex_lock(&priv->command_lock);
 908
 909        reset = (void *)priv->command_buffer;
 910        memset(reset, 0, sizeof(*reset));
 911        reset->msg_type = RNDIS_MSG_RESET;
 912        reset->msg_len = cpu_to_le32(sizeof(*reset));
 913        priv->current_command_oid = 0;
 914        ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
 915
 916        mutex_unlock(&priv->command_lock);
 917
 918        if (ret < 0)
 919                return ret;
 920        return 0;
 921}
 922
 923/*
 924 * Specs say that we can only set config parameters only soon after device
 925 * initialization.
 926 *   value_type: 0 = u32, 2 = unicode string
 927 */
 928static int rndis_set_config_parameter(struct usbnet *dev, char *param,
 929                                                int value_type, void *value)
 930{
 931        struct ndis_config_param *infobuf;
 932        int value_len, info_len, param_len, ret, i;
 933        __le16 *unibuf;
 934        __le32 *dst_value;
 935
 936        if (value_type == 0)
 937                value_len = sizeof(__le32);
 938        else if (value_type == 2)
 939                value_len = strlen(value) * sizeof(__le16);
 940        else
 941                return -EINVAL;
 942
 943        param_len = strlen(param) * sizeof(__le16);
 944        info_len = sizeof(*infobuf) + param_len + value_len;
 945
 946#ifdef DEBUG
 947        info_len += 12;
 948#endif
 949        infobuf = kmalloc(info_len, GFP_KERNEL);
 950        if (!infobuf)
 951                return -ENOMEM;
 952
 953#ifdef DEBUG
 954        info_len -= 12;
 955        /* extra 12 bytes are for padding (debug output) */
 956        memset(infobuf, 0xCC, info_len + 12);
 957#endif
 958
 959        if (value_type == 2)
 960                netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
 961                           param, (u8 *)value);
 962        else
 963                netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
 964                           param, *(u32 *)value);
 965
 966        infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
 967        infobuf->name_length = cpu_to_le32(param_len);
 968        infobuf->type = cpu_to_le32(value_type);
 969        infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
 970        infobuf->value_length = cpu_to_le32(value_len);
 971
 972        /* simple string to unicode string conversion */
 973        unibuf = (void *)infobuf + sizeof(*infobuf);
 974        for (i = 0; i < param_len / sizeof(__le16); i++)
 975                unibuf[i] = cpu_to_le16(param[i]);
 976
 977        if (value_type == 2) {
 978                unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
 979                for (i = 0; i < value_len / sizeof(__le16); i++)
 980                        unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
 981        } else {
 982                dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
 983                *dst_value = cpu_to_le32(*(u32 *)value);
 984        }
 985
 986#ifdef DEBUG
 987        netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
 988        for (i = 0; i < info_len; i += 12) {
 989                u32 *tmp = (u32 *)((u8 *)infobuf + i);
 990                netdev_dbg(dev->net, "%08X:%08X:%08X\n",
 991                           cpu_to_be32(tmp[0]),
 992                           cpu_to_be32(tmp[1]),
 993                           cpu_to_be32(tmp[2]));
 994        }
 995#endif
 996
 997        ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
 998                                                        infobuf, info_len);
 999        if (ret != 0)
1000                netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
1001                           ret);
1002
1003        kfree(infobuf);
1004        return ret;
1005}
1006
1007static int rndis_set_config_parameter_str(struct usbnet *dev,
1008                                                char *param, char *value)
1009{
1010        return rndis_set_config_parameter(dev, param, 2, value);
1011}
1012
1013/*
1014 * data conversion functions
1015 */
1016static int level_to_qual(int level)
1017{
1018        int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
1019        return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
1020}
1021
1022/*
1023 * common functions
1024 */
1025static int set_infra_mode(struct usbnet *usbdev, int mode);
1026static void restore_keys(struct usbnet *usbdev);
1027static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
1028                                        bool *matched);
1029
1030static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
1031{
1032        __le32 tmp;
1033
1034        /* Note: OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
1035        tmp = cpu_to_le32(1);
1036        return rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1037                                                        sizeof(tmp));
1038}
1039
1040static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
1041{
1042        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1043        int ret;
1044
1045        ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
1046        if (ret < 0) {
1047                netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
1048                return ret;
1049        }
1050        if (ret == 0) {
1051                priv->radio_on = true;
1052                netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
1053        }
1054
1055        return ret;
1056}
1057
1058static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
1059{
1060        int ret;
1061
1062        ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1063        if (ret < 0) {
1064                netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1065                            bssid, ret);
1066                return ret;
1067        }
1068
1069        return ret;
1070}
1071
1072static int clear_bssid(struct usbnet *usbdev)
1073{
1074        static const u8 broadcast_mac[ETH_ALEN] = {
1075                0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1076        };
1077
1078        return set_bssid(usbdev, broadcast_mac);
1079}
1080
1081static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1082{
1083        int ret, len;
1084
1085        len = ETH_ALEN;
1086        ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1087
1088        if (ret != 0)
1089                memset(bssid, 0, ETH_ALEN);
1090
1091        return ret;
1092}
1093
1094static int get_association_info(struct usbnet *usbdev,
1095                        struct ndis_80211_assoc_info *info, int len)
1096{
1097        return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1098                                info, &len);
1099}
1100
1101static bool is_associated(struct usbnet *usbdev)
1102{
1103        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1104        u8 bssid[ETH_ALEN];
1105        int ret;
1106
1107        if (!priv->radio_on)
1108                return false;
1109
1110        ret = get_bssid(usbdev, bssid);
1111
1112        return (ret == 0 && !is_zero_ether_addr(bssid));
1113}
1114
1115static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1116{
1117        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1118        struct ndis_80211_ssid ssid;
1119        int i, ret = 0;
1120
1121        if (priv->radio_on) {
1122                ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1123                if (ret == 0) {
1124                        priv->radio_on = false;
1125                        netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1126                                   __func__);
1127
1128                        if (reset_ssid)
1129                                msleep(100);
1130                }
1131        }
1132
1133        /* disassociate causes radio to be turned off; if reset_ssid
1134         * is given, set random ssid to enable radio */
1135        if (reset_ssid) {
1136                /* Set device to infrastructure mode so we don't get ad-hoc
1137                 * 'media connect' indications with the random ssid.
1138                 */
1139                set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1140
1141                ssid.length = cpu_to_le32(sizeof(ssid.essid));
1142                get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1143                ssid.essid[0] = 0x1;
1144                ssid.essid[1] = 0xff;
1145                for (i = 2; i < sizeof(ssid.essid); i++)
1146                        ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1147                ret = set_essid(usbdev, &ssid);
1148        }
1149        return ret;
1150}
1151
1152static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1153                                enum nl80211_auth_type auth_type, int keymgmt)
1154{
1155        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1156        __le32 tmp;
1157        int auth_mode, ret;
1158
1159        netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1160                   __func__, wpa_version, auth_type, keymgmt);
1161
1162        if (wpa_version & NL80211_WPA_VERSION_2) {
1163                if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1164                        auth_mode = NDIS_80211_AUTH_WPA2;
1165                else
1166                        auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1167        } else if (wpa_version & NL80211_WPA_VERSION_1) {
1168                if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1169                        auth_mode = NDIS_80211_AUTH_WPA;
1170                else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1171                        auth_mode = NDIS_80211_AUTH_WPA_PSK;
1172                else
1173                        auth_mode = NDIS_80211_AUTH_WPA_NONE;
1174        } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1175                auth_mode = NDIS_80211_AUTH_SHARED;
1176        else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1177                auth_mode = NDIS_80211_AUTH_OPEN;
1178        else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1179                auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1180        else
1181                return -ENOTSUPP;
1182
1183        tmp = cpu_to_le32(auth_mode);
1184        ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1185                                                                sizeof(tmp));
1186        if (ret != 0) {
1187                netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1188                            ret);
1189                return ret;
1190        }
1191
1192        priv->wpa_version = wpa_version;
1193
1194        return 0;
1195}
1196
1197static int set_priv_filter(struct usbnet *usbdev)
1198{
1199        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1200        __le32 tmp;
1201
1202        netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1203                   __func__, priv->wpa_version);
1204
1205        if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1206            priv->wpa_version & NL80211_WPA_VERSION_1)
1207                tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1208        else
1209                tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1210
1211        return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1212                                                                sizeof(tmp));
1213}
1214
1215static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1216{
1217        __le32 tmp;
1218        int encr_mode, ret;
1219
1220        netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1221                   __func__, pairwise, groupwise);
1222
1223        if (pairwise & RNDIS_WLAN_ALG_CCMP)
1224                encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1225        else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1226                encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1227        else if (pairwise & RNDIS_WLAN_ALG_WEP)
1228                encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1229        else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1230                encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1231        else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1232                encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1233        else
1234                encr_mode = NDIS_80211_ENCR_DISABLED;
1235
1236        tmp = cpu_to_le32(encr_mode);
1237        ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1238                                                                sizeof(tmp));
1239        if (ret != 0) {
1240                netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1241                            ret);
1242                return ret;
1243        }
1244
1245        return 0;
1246}
1247
1248static int set_infra_mode(struct usbnet *usbdev, int mode)
1249{
1250        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1251        __le32 tmp;
1252        int ret;
1253
1254        netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1255                   __func__, priv->infra_mode);
1256
1257        tmp = cpu_to_le32(mode);
1258        ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1259                                                                sizeof(tmp));
1260        if (ret != 0) {
1261                netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1262                            ret);
1263                return ret;
1264        }
1265
1266        /* NDIS drivers clear keys when infrastructure mode is
1267         * changed. But Linux tools assume otherwise. So set the
1268         * keys */
1269        restore_keys(usbdev);
1270
1271        priv->infra_mode = mode;
1272        return 0;
1273}
1274
1275static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1276{
1277        __le32 tmp;
1278
1279        netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1280
1281        if (rts_threshold < 0 || rts_threshold > 2347)
1282                rts_threshold = 2347;
1283
1284        tmp = cpu_to_le32(rts_threshold);
1285        return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1286                                                                sizeof(tmp));
1287}
1288
1289static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1290{
1291        __le32 tmp;
1292
1293        netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1294
1295        if (frag_threshold < 256 || frag_threshold > 2346)
1296                frag_threshold = 2346;
1297
1298        tmp = cpu_to_le32(frag_threshold);
1299        return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1300                                                                sizeof(tmp));
1301}
1302
1303static void set_default_iw_params(struct usbnet *usbdev)
1304{
1305        set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1306        set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1307                                                RNDIS_WLAN_KEY_MGMT_NONE);
1308        set_priv_filter(usbdev);
1309        set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1310}
1311
1312static int deauthenticate(struct usbnet *usbdev)
1313{
1314        int ret;
1315
1316        ret = disassociate(usbdev, true);
1317        set_default_iw_params(usbdev);
1318        return ret;
1319}
1320
1321static int set_channel(struct usbnet *usbdev, int channel)
1322{
1323        struct ndis_80211_conf config;
1324        unsigned int dsconfig;
1325        int len, ret;
1326
1327        netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1328
1329        /* this OID is valid only when not associated */
1330        if (is_associated(usbdev))
1331                return 0;
1332
1333        dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1334
1335        len = sizeof(config);
1336        ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1337        if (ret < 0) {
1338                netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1339                           __func__);
1340                return ret;
1341        }
1342
1343        config.ds_config = cpu_to_le32(dsconfig);
1344        ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1345                                                                sizeof(config));
1346
1347        netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1348
1349        return ret;
1350}
1351
1352static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
1353                                                     u32 *beacon_period)
1354{
1355        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1356        struct ieee80211_channel *channel;
1357        struct ndis_80211_conf config;
1358        int len, ret;
1359
1360        /* Get channel and beacon interval */
1361        len = sizeof(config);
1362        ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1363        netdev_dbg(usbdev->net, "%s(): OID_802_11_CONFIGURATION -> %d\n",
1364                                __func__, ret);
1365        if (ret < 0)
1366                return NULL;
1367
1368        channel = ieee80211_get_channel(priv->wdev.wiphy,
1369                                KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
1370        if (!channel)
1371                return NULL;
1372
1373        if (beacon_period)
1374                *beacon_period = le32_to_cpu(config.beacon_period);
1375        return channel;
1376}
1377
1378/* index must be 0 - N, as per NDIS  */
1379static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1380                                                                u8 index)
1381{
1382        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1383        struct ndis_80211_wep_key ndis_key;
1384        u32 cipher;
1385        int ret;
1386
1387        netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1388                   __func__, index, key_len);
1389
1390        if (index >= RNDIS_WLAN_NUM_KEYS)
1391                return -EINVAL;
1392
1393        if (key_len == 5)
1394                cipher = WLAN_CIPHER_SUITE_WEP40;
1395        else if (key_len == 13)
1396                cipher = WLAN_CIPHER_SUITE_WEP104;
1397        else
1398                return -EINVAL;
1399
1400        memset(&ndis_key, 0, sizeof(ndis_key));
1401
1402        ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1403        ndis_key.length = cpu_to_le32(key_len);
1404        ndis_key.index = cpu_to_le32(index);
1405        memcpy(&ndis_key.material, key, key_len);
1406
1407        if (index == priv->encr_tx_key_index) {
1408                ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1409                ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1410                                                        RNDIS_WLAN_ALG_NONE);
1411                if (ret)
1412                        netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1413                                    ret);
1414        }
1415
1416        ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1417                                                        sizeof(ndis_key));
1418        if (ret != 0) {
1419                netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1420                            index + 1, ret);
1421                return ret;
1422        }
1423
1424        priv->encr_keys[index].len = key_len;
1425        priv->encr_keys[index].cipher = cipher;
1426        memcpy(&priv->encr_keys[index].material, key, key_len);
1427        memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1428
1429        return 0;
1430}
1431
1432static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1433                        u8 index, const u8 *addr, const u8 *rx_seq,
1434                        int seq_len, u32 cipher, __le32 flags)
1435{
1436        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1437        struct ndis_80211_key ndis_key;
1438        bool is_addr_ok;
1439        int ret;
1440
1441        if (index >= RNDIS_WLAN_NUM_KEYS) {
1442                netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1443                           __func__, index);
1444                return -EINVAL;
1445        }
1446        if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1447                netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1448                           __func__, key_len);
1449                return -EINVAL;
1450        }
1451        if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1452                if (!rx_seq || seq_len <= 0) {
1453                        netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1454                                   __func__);
1455                        return -EINVAL;
1456                }
1457                if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1458                        netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1459                        return -EINVAL;
1460                }
1461        }
1462
1463        is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1464                                        !is_broadcast_ether_addr(addr);
1465        if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1466                netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1467                           __func__, addr);
1468                return -EINVAL;
1469        }
1470
1471        netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1472                   __func__, index,
1473                   !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1474                   !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1475                   !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1476
1477        memset(&ndis_key, 0, sizeof(ndis_key));
1478
1479        ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1480                                sizeof(ndis_key.material) + key_len);
1481        ndis_key.length = cpu_to_le32(key_len);
1482        ndis_key.index = cpu_to_le32(index) | flags;
1483
1484        if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1485                /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1486                 * different order than NDIS spec, so swap the order here. */
1487                memcpy(ndis_key.material, key, 16);
1488                memcpy(ndis_key.material + 16, key + 24, 8);
1489                memcpy(ndis_key.material + 24, key + 16, 8);
1490        } else
1491                memcpy(ndis_key.material, key, key_len);
1492
1493        if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1494                memcpy(ndis_key.rsc, rx_seq, seq_len);
1495
1496        if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1497                /* pairwise key */
1498                memcpy(ndis_key.bssid, addr, ETH_ALEN);
1499        } else {
1500                /* group key */
1501                if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1502                        memset(ndis_key.bssid, 0xff, ETH_ALEN);
1503                else
1504                        get_bssid(usbdev, ndis_key.bssid);
1505        }
1506
1507        ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1508                                        le32_to_cpu(ndis_key.size));
1509        netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1510                   __func__, ret);
1511        if (ret != 0)
1512                return ret;
1513
1514        memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1515        priv->encr_keys[index].len = key_len;
1516        priv->encr_keys[index].cipher = cipher;
1517        memcpy(&priv->encr_keys[index].material, key, key_len);
1518        if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1519                memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1520        else
1521                memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1522
1523        if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1524                priv->encr_tx_key_index = index;
1525
1526        return 0;
1527}
1528
1529static int restore_key(struct usbnet *usbdev, u8 key_idx)
1530{
1531        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1532        struct rndis_wlan_encr_key key;
1533
1534        if (is_wpa_key(priv, key_idx))
1535                return 0;
1536
1537        key = priv->encr_keys[key_idx];
1538
1539        netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1540
1541        if (key.len == 0)
1542                return 0;
1543
1544        return add_wep_key(usbdev, key.material, key.len, key_idx);
1545}
1546
1547static void restore_keys(struct usbnet *usbdev)
1548{
1549        int i;
1550
1551        for (i = 0; i < 4; i++)
1552                restore_key(usbdev, i);
1553}
1554
1555static void clear_key(struct rndis_wlan_private *priv, u8 idx)
1556{
1557        memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1558}
1559
1560/* remove_key is for both wep and wpa */
1561static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
1562{
1563        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1564        struct ndis_80211_remove_key remove_key;
1565        __le32 keyindex;
1566        bool is_wpa;
1567        int ret;
1568
1569        if (index >= RNDIS_WLAN_NUM_KEYS)
1570                return -ENOENT;
1571
1572        if (priv->encr_keys[index].len == 0)
1573                return 0;
1574
1575        is_wpa = is_wpa_key(priv, index);
1576
1577        netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1578                   __func__, index, is_wpa ? "wpa" : "wep",
1579                   priv->encr_keys[index].len);
1580
1581        clear_key(priv, index);
1582
1583        if (is_wpa) {
1584                remove_key.size = cpu_to_le32(sizeof(remove_key));
1585                remove_key.index = cpu_to_le32(index);
1586                if (bssid) {
1587                        /* pairwise key */
1588                        if (!is_broadcast_ether_addr(bssid))
1589                                remove_key.index |=
1590                                        NDIS_80211_ADDKEY_PAIRWISE_KEY;
1591                        memcpy(remove_key.bssid, bssid,
1592                                        sizeof(remove_key.bssid));
1593                } else
1594                        memset(remove_key.bssid, 0xff,
1595                                                sizeof(remove_key.bssid));
1596
1597                ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1598                                                        sizeof(remove_key));
1599                if (ret != 0)
1600                        return ret;
1601        } else {
1602                keyindex = cpu_to_le32(index);
1603                ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1604                                                        sizeof(keyindex));
1605                if (ret != 0) {
1606                        netdev_warn(usbdev->net,
1607                                    "removing encryption key %d failed (%08X)\n",
1608                                    index, ret);
1609                        return ret;
1610                }
1611        }
1612
1613        /* if it is transmit key, disable encryption */
1614        if (index == priv->encr_tx_key_index)
1615                set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1616
1617        return 0;
1618}
1619
1620static void set_multicast_list(struct usbnet *usbdev)
1621{
1622        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1623        struct netdev_hw_addr *ha;
1624        __le32 filter, basefilter;
1625        int ret;
1626        char *mc_addrs = NULL;
1627        int mc_count;
1628
1629        basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1630                              RNDIS_PACKET_TYPE_BROADCAST;
1631
1632        if (usbdev->net->flags & IFF_PROMISC) {
1633                filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1634                        RNDIS_PACKET_TYPE_ALL_LOCAL;
1635        } else if (usbdev->net->flags & IFF_ALLMULTI) {
1636                filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1637        }
1638
1639        if (filter != basefilter)
1640                goto set_filter;
1641
1642        /*
1643         * mc_list should be accessed holding the lock, so copy addresses to
1644         * local buffer first.
1645         */
1646        netif_addr_lock_bh(usbdev->net);
1647        mc_count = netdev_mc_count(usbdev->net);
1648        if (mc_count > priv->multicast_size) {
1649                filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1650        } else if (mc_count) {
1651                int i = 0;
1652
1653                mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1654                if (!mc_addrs) {
1655                        netdev_warn(usbdev->net,
1656                                    "couldn't alloc %d bytes of memory\n",
1657                                    mc_count * ETH_ALEN);
1658                        netif_addr_unlock_bh(usbdev->net);
1659                        return;
1660                }
1661
1662                netdev_for_each_mc_addr(ha, usbdev->net)
1663                        memcpy(mc_addrs + i++ * ETH_ALEN,
1664                               ha->addr, ETH_ALEN);
1665        }
1666        netif_addr_unlock_bh(usbdev->net);
1667
1668        if (filter != basefilter)
1669                goto set_filter;
1670
1671        if (mc_count) {
1672                ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
1673                                    mc_count * ETH_ALEN);
1674                kfree(mc_addrs);
1675                if (ret == 0)
1676                        filter |= RNDIS_PACKET_TYPE_MULTICAST;
1677                else
1678                        filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1679
1680                netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1681                           mc_count, priv->multicast_size, ret);
1682        }
1683
1684set_filter:
1685        ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1686                                                        sizeof(filter));
1687        if (ret < 0) {
1688                netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1689                            le32_to_cpu(filter));
1690        }
1691
1692        netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1693                   le32_to_cpu(filter), ret);
1694}
1695
1696#ifdef DEBUG
1697static void debug_print_pmkids(struct usbnet *usbdev,
1698                                struct ndis_80211_pmkid *pmkids,
1699                                const char *func_str)
1700{
1701        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1702        int i, len, count, max_pmkids, entry_len;
1703
1704        max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1705        len = le32_to_cpu(pmkids->length);
1706        count = le32_to_cpu(pmkids->bssid_info_count);
1707
1708        entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1709
1710        netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1711                                "%d)\n", func_str, count, len, entry_len);
1712
1713        if (count > max_pmkids)
1714                count = max_pmkids;
1715
1716        for (i = 0; i < count; i++) {
1717                u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1718
1719                netdev_dbg(usbdev->net, "%s():  bssid: %pM, "
1720                                "pmkid: %08X:%08X:%08X:%08X\n",
1721                                func_str, pmkids->bssid_info[i].bssid,
1722                                cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1723                                cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1724        }
1725}
1726#else
1727static void debug_print_pmkids(struct usbnet *usbdev,
1728                                struct ndis_80211_pmkid *pmkids,
1729                                const char *func_str)
1730{
1731        return;
1732}
1733#endif
1734
1735static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1736{
1737        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1738        struct ndis_80211_pmkid *pmkids;
1739        int len, ret, max_pmkids;
1740
1741        max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1742        len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
1743
1744        pmkids = kzalloc(len, GFP_KERNEL);
1745        if (!pmkids)
1746                return ERR_PTR(-ENOMEM);
1747
1748        pmkids->length = cpu_to_le32(len);
1749        pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1750
1751        ret = rndis_query_oid(usbdev, OID_802_11_PMKID, pmkids, &len);
1752        if (ret < 0) {
1753                netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d)"
1754                                " -> %d\n", __func__, len, max_pmkids, ret);
1755
1756                kfree(pmkids);
1757                return ERR_PTR(ret);
1758        }
1759
1760        if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1761                pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1762
1763        debug_print_pmkids(usbdev, pmkids, __func__);
1764
1765        return pmkids;
1766}
1767
1768static int set_device_pmkids(struct usbnet *usbdev,
1769                                struct ndis_80211_pmkid *pmkids)
1770{
1771        int ret, len, num_pmkids;
1772
1773        num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1774        len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
1775        pmkids->length = cpu_to_le32(len);
1776
1777        debug_print_pmkids(usbdev, pmkids, __func__);
1778
1779        ret = rndis_set_oid(usbdev, OID_802_11_PMKID, pmkids,
1780                                                le32_to_cpu(pmkids->length));
1781        if (ret < 0) {
1782                netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d) -> %d"
1783                                "\n", __func__, len, num_pmkids, ret);
1784        }
1785
1786        kfree(pmkids);
1787        return ret;
1788}
1789
1790static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1791                                                struct ndis_80211_pmkid *pmkids,
1792                                                struct cfg80211_pmksa *pmksa,
1793                                                int max_pmkids)
1794{
1795        int i, newlen, err;
1796        unsigned int count;
1797
1798        count = le32_to_cpu(pmkids->bssid_info_count);
1799
1800        if (count > max_pmkids)
1801                count = max_pmkids;
1802
1803        for (i = 0; i < count; i++)
1804                if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
1805                                                        pmksa->bssid))
1806                        break;
1807
1808        /* pmkid not found */
1809        if (i == count) {
1810                netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1811                                        __func__, pmksa->bssid);
1812                err = -ENOENT;
1813                goto error;
1814        }
1815
1816        for (; i + 1 < count; i++)
1817                pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1818
1819        count--;
1820        newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
1821
1822        pmkids->length = cpu_to_le32(newlen);
1823        pmkids->bssid_info_count = cpu_to_le32(count);
1824
1825        return pmkids;
1826error:
1827        kfree(pmkids);
1828        return ERR_PTR(err);
1829}
1830
1831static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1832                                                struct ndis_80211_pmkid *pmkids,
1833                                                struct cfg80211_pmksa *pmksa,
1834                                                int max_pmkids)
1835{
1836        int i, err, newlen;
1837        unsigned int count;
1838
1839        count = le32_to_cpu(pmkids->bssid_info_count);
1840
1841        if (count > max_pmkids)
1842                count = max_pmkids;
1843
1844        /* update with new pmkid */
1845        for (i = 0; i < count; i++) {
1846                if (compare_ether_addr(pmkids->bssid_info[i].bssid,
1847                                                        pmksa->bssid))
1848                        continue;
1849
1850                memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1851                                                                WLAN_PMKID_LEN);
1852
1853                return pmkids;
1854        }
1855
1856        /* out of space, return error */
1857        if (i == max_pmkids) {
1858                netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1859                err = -ENOSPC;
1860                goto error;
1861        }
1862
1863        /* add new pmkid */
1864        newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
1865
1866        pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1867        if (!pmkids) {
1868                err = -ENOMEM;
1869                goto error;
1870        }
1871
1872        pmkids->length = cpu_to_le32(newlen);
1873        pmkids->bssid_info_count = cpu_to_le32(count + 1);
1874
1875        memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1876        memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1877
1878        return pmkids;
1879error:
1880        kfree(pmkids);
1881        return ERR_PTR(err);
1882}
1883
1884/*
1885 * cfg80211 ops
1886 */
1887static int rndis_change_virtual_intf(struct wiphy *wiphy,
1888                                        struct net_device *dev,
1889                                        enum nl80211_iftype type, u32 *flags,
1890                                        struct vif_params *params)
1891{
1892        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1893        struct usbnet *usbdev = priv->usbdev;
1894        int mode;
1895
1896        switch (type) {
1897        case NL80211_IFTYPE_ADHOC:
1898                mode = NDIS_80211_INFRA_ADHOC;
1899                break;
1900        case NL80211_IFTYPE_STATION:
1901                mode = NDIS_80211_INFRA_INFRA;
1902                break;
1903        default:
1904                return -EINVAL;
1905        }
1906
1907        priv->wdev.iftype = type;
1908
1909        return set_infra_mode(usbdev, mode);
1910}
1911
1912static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1913{
1914        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1915        struct usbnet *usbdev = priv->usbdev;
1916        int err;
1917
1918        if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1919                err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1920                if (err < 0)
1921                        return err;
1922        }
1923
1924        if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1925                err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1926                if (err < 0)
1927                        return err;
1928        }
1929
1930        return 0;
1931}
1932
1933static int rndis_set_tx_power(struct wiphy *wiphy,
1934                              enum nl80211_tx_power_setting type,
1935                              int mbm)
1936{
1937        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1938        struct usbnet *usbdev = priv->usbdev;
1939
1940        netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1941                   __func__, type, mbm);
1942
1943        if (mbm < 0 || (mbm % 100))
1944                return -ENOTSUPP;
1945
1946        /* Device doesn't support changing txpower after initialization, only
1947         * turn off/on radio. Support 'auto' mode and setting same dBm that is
1948         * currently used.
1949         */
1950        if (type == NL80211_TX_POWER_AUTOMATIC ||
1951            MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1952                if (!priv->radio_on)
1953                        disassociate(usbdev, true); /* turn on radio */
1954
1955                return 0;
1956        }
1957
1958        return -ENOTSUPP;
1959}
1960
1961static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1962{
1963        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1964        struct usbnet *usbdev = priv->usbdev;
1965
1966        *dbm = get_bcm4320_power_dbm(priv);
1967
1968        netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1969
1970        return 0;
1971}
1972
1973#define SCAN_DELAY_JIFFIES (6 * HZ)
1974static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1975                        struct cfg80211_scan_request *request)
1976{
1977        struct usbnet *usbdev = netdev_priv(dev);
1978        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1979        int ret;
1980        int delay = SCAN_DELAY_JIFFIES;
1981
1982        netdev_dbg(usbdev->net, "cfg80211.scan\n");
1983
1984        /* Get current bssid list from device before new scan, as new scan
1985         * clears internal bssid list.
1986         */
1987        rndis_check_bssid_list(usbdev, NULL, NULL);
1988
1989        if (!request)
1990                return -EINVAL;
1991
1992        if (priv->scan_request && priv->scan_request != request)
1993                return -EBUSY;
1994
1995        priv->scan_request = request;
1996
1997        ret = rndis_start_bssid_list_scan(usbdev);
1998        if (ret == 0) {
1999                if (priv->device_type == RNDIS_BCM4320A)
2000                        delay = HZ;
2001
2002                /* Wait before retrieving scan results from device */
2003                queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
2004        }
2005
2006        return ret;
2007}
2008
2009static bool rndis_bss_info_update(struct usbnet *usbdev,
2010                                  struct ndis_80211_bssid_ex *bssid)
2011{
2012        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2013        struct ieee80211_channel *channel;
2014        struct cfg80211_bss *bss;
2015        s32 signal;
2016        u64 timestamp;
2017        u16 capability;
2018        u16 beacon_interval;
2019        struct ndis_80211_fixed_ies *fixed;
2020        int ie_len, bssid_len;
2021        u8 *ie;
2022
2023        netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
2024                   bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
2025
2026        /* parse bssid structure */
2027        bssid_len = le32_to_cpu(bssid->length);
2028
2029        if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
2030                        sizeof(struct ndis_80211_fixed_ies))
2031                return NULL;
2032
2033        fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
2034
2035        ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
2036        ie_len = min(bssid_len - (int)sizeof(*bssid),
2037                                        (int)le32_to_cpu(bssid->ie_length));
2038        ie_len -= sizeof(struct ndis_80211_fixed_ies);
2039        if (ie_len < 0)
2040                return NULL;
2041
2042        /* extract data for cfg80211_inform_bss */
2043        channel = ieee80211_get_channel(priv->wdev.wiphy,
2044                        KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
2045        if (!channel)
2046                return NULL;
2047
2048        signal = level_to_qual(le32_to_cpu(bssid->rssi));
2049        timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
2050        capability = le16_to_cpu(fixed->capabilities);
2051        beacon_interval = le16_to_cpu(fixed->beacon_interval);
2052
2053        bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
2054                timestamp, capability, beacon_interval, ie, ie_len, signal,
2055                GFP_KERNEL);
2056        cfg80211_put_bss(bss);
2057
2058        return (bss != NULL);
2059}
2060
2061static struct ndis_80211_bssid_ex *next_bssid_list_item(
2062                                        struct ndis_80211_bssid_ex *bssid,
2063                                        int *bssid_len, void *buf, int len)
2064{
2065        void *buf_end, *bssid_end;
2066
2067        buf_end = (char *)buf + len;
2068        bssid_end = (char *)bssid + *bssid_len;
2069
2070        if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
2071                *bssid_len = 0;
2072                return NULL;
2073        } else {
2074                bssid = (void *)((char *)bssid + *bssid_len);
2075                *bssid_len = le32_to_cpu(bssid->length);
2076                return bssid;
2077        }
2078}
2079
2080static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
2081                                  int bssid_len, void *buf, int len)
2082{
2083        void *buf_end, *bssid_end;
2084
2085        if (!bssid || bssid_len <= 0 || bssid_len > len)
2086                return false;
2087
2088        buf_end = (char *)buf + len;
2089        bssid_end = (char *)bssid + bssid_len;
2090
2091        return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
2092}
2093
2094static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
2095                                        bool *matched)
2096{
2097        void *buf = NULL;
2098        struct ndis_80211_bssid_list_ex *bssid_list;
2099        struct ndis_80211_bssid_ex *bssid;
2100        int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
2101
2102        netdev_dbg(usbdev->net, "%s()\n", __func__);
2103
2104        len = CONTROL_BUFFER_SIZE;
2105resize_buf:
2106        buf = kzalloc(len, GFP_KERNEL);
2107        if (!buf) {
2108                ret = -ENOMEM;
2109                goto out;
2110        }
2111
2112        /* BSSID-list might have got bigger last time we checked, keep
2113         * resizing until it won't get any bigger.
2114         */
2115        new_len = len;
2116        ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &new_len);
2117        if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
2118                goto out;
2119
2120        if (new_len > len) {
2121                len = new_len;
2122                kfree(buf);
2123                goto resize_buf;
2124        }
2125
2126        len = new_len;
2127
2128        bssid_list = buf;
2129        count = le32_to_cpu(bssid_list->num_items);
2130        real_count = 0;
2131        netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
2132
2133        bssid_len = 0;
2134        bssid = next_bssid_list_item(bssid_list->bssid, &bssid_len, buf, len);
2135
2136        /* Device returns incorrect 'num_items'. Workaround by ignoring the
2137         * received 'num_items' and walking through full bssid buffer instead.
2138         */
2139        while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
2140                if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
2141                    matched) {
2142                        if (compare_ether_addr(bssid->mac, match_bssid))
2143                                *matched = true;
2144                }
2145
2146                real_count++;
2147                bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
2148        }
2149
2150        netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
2151                                " %d\n", __func__, count, real_count);
2152
2153out:
2154        kfree(buf);
2155        return ret;
2156}
2157
2158static void rndis_get_scan_results(struct work_struct *work)
2159{
2160        struct rndis_wlan_private *priv =
2161                container_of(work, struct rndis_wlan_private, scan_work.work);
2162        struct usbnet *usbdev = priv->usbdev;
2163        int ret;
2164
2165        netdev_dbg(usbdev->net, "get_scan_results\n");
2166
2167        if (!priv->scan_request)
2168                return;
2169
2170        ret = rndis_check_bssid_list(usbdev, NULL, NULL);
2171
2172        cfg80211_scan_done(priv->scan_request, ret < 0);
2173
2174        priv->scan_request = NULL;
2175}
2176
2177static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2178                                        struct cfg80211_connect_params *sme)
2179{
2180        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2181        struct usbnet *usbdev = priv->usbdev;
2182        struct ieee80211_channel *channel = sme->channel;
2183        struct ndis_80211_ssid ssid;
2184        int pairwise = RNDIS_WLAN_ALG_NONE;
2185        int groupwise = RNDIS_WLAN_ALG_NONE;
2186        int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2187        int length, i, ret, chan = -1;
2188
2189        if (channel)
2190                chan = ieee80211_frequency_to_channel(channel->center_freq);
2191
2192        groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2193        for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2194                pairwise |=
2195                        rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2196
2197        if (sme->crypto.n_ciphers_pairwise > 0 &&
2198                        pairwise == RNDIS_WLAN_ALG_NONE) {
2199                netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2200                return -ENOTSUPP;
2201        }
2202
2203        for (i = 0; i < sme->crypto.n_akm_suites; i++)
2204                keymgmt |=
2205                        rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2206
2207        if (sme->crypto.n_akm_suites > 0 &&
2208                        keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2209                netdev_err(usbdev->net, "Invalid keymgmt\n");
2210                return -ENOTSUPP;
2211        }
2212
2213        netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2214                   sme->ssid, sme->bssid, chan,
2215                   sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2216                   groupwise, pairwise, keymgmt);
2217
2218        if (is_associated(usbdev))
2219                disassociate(usbdev, false);
2220
2221        ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2222        if (ret < 0) {
2223                netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2224                           ret);
2225                goto err_turn_radio_on;
2226        }
2227
2228        ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2229                                                                keymgmt);
2230        if (ret < 0) {
2231                netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2232                           ret);
2233                goto err_turn_radio_on;
2234        }
2235
2236        set_priv_filter(usbdev);
2237
2238        ret = set_encr_mode(usbdev, pairwise, groupwise);
2239        if (ret < 0) {
2240                netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2241                           ret);
2242                goto err_turn_radio_on;
2243        }
2244
2245        if (channel) {
2246                ret = set_channel(usbdev, chan);
2247                if (ret < 0) {
2248                        netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2249                                   ret);
2250                        goto err_turn_radio_on;
2251                }
2252        }
2253
2254        if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2255                priv->encr_tx_key_index = sme->key_idx;
2256                ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2257                if (ret < 0) {
2258                        netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2259                                   ret, sme->key_len, sme->key_idx);
2260                        goto err_turn_radio_on;
2261                }
2262        }
2263
2264        if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2265                                !is_broadcast_ether_addr(sme->bssid)) {
2266                ret = set_bssid(usbdev, sme->bssid);
2267                if (ret < 0) {
2268                        netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2269                                   ret);
2270                        goto err_turn_radio_on;
2271                }
2272        } else
2273                clear_bssid(usbdev);
2274
2275        length = sme->ssid_len;
2276        if (length > NDIS_802_11_LENGTH_SSID)
2277                length = NDIS_802_11_LENGTH_SSID;
2278
2279        memset(&ssid, 0, sizeof(ssid));
2280        ssid.length = cpu_to_le32(length);
2281        memcpy(ssid.essid, sme->ssid, length);
2282
2283        /* Pause and purge rx queue, so we don't pass packets before
2284         * 'media connect'-indication.
2285         */
2286        usbnet_pause_rx(usbdev);
2287        usbnet_purge_paused_rxq(usbdev);
2288
2289        ret = set_essid(usbdev, &ssid);
2290        if (ret < 0)
2291                netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2292        return ret;
2293
2294err_turn_radio_on:
2295        disassociate(usbdev, true);
2296
2297        return ret;
2298}
2299
2300static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2301                                                                u16 reason_code)
2302{
2303        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2304        struct usbnet *usbdev = priv->usbdev;
2305
2306        netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2307
2308        priv->connected = false;
2309        memset(priv->bssid, 0, ETH_ALEN);
2310
2311        return deauthenticate(usbdev);
2312}
2313
2314static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2315                                        struct cfg80211_ibss_params *params)
2316{
2317        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2318        struct usbnet *usbdev = priv->usbdev;
2319        struct ieee80211_channel *channel = params->channel;
2320        struct ndis_80211_ssid ssid;
2321        enum nl80211_auth_type auth_type;
2322        int ret, alg, length, chan = -1;
2323
2324        if (channel)
2325                chan = ieee80211_frequency_to_channel(channel->center_freq);
2326
2327        /* TODO: How to handle ad-hoc encryption?
2328         * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2329         * pre-shared for encryption (open/shared/wpa), is key set before
2330         * join_ibss? Which auth_type to use (not in params)? What about WPA?
2331         */
2332        if (params->privacy) {
2333                auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2334                alg = RNDIS_WLAN_ALG_WEP;
2335        } else {
2336                auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2337                alg = RNDIS_WLAN_ALG_NONE;
2338        }
2339
2340        netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2341                   params->ssid, params->bssid, chan, params->privacy);
2342
2343        if (is_associated(usbdev))
2344                disassociate(usbdev, false);
2345
2346        ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2347        if (ret < 0) {
2348                netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2349                           ret);
2350                goto err_turn_radio_on;
2351        }
2352
2353        ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2354        if (ret < 0) {
2355                netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2356                           ret);
2357                goto err_turn_radio_on;
2358        }
2359
2360        set_priv_filter(usbdev);
2361
2362        ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2363        if (ret < 0) {
2364                netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2365                           ret);
2366                goto err_turn_radio_on;
2367        }
2368
2369        if (channel) {
2370                ret = set_channel(usbdev, chan);
2371                if (ret < 0) {
2372                        netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2373                                   ret);
2374                        goto err_turn_radio_on;
2375                }
2376        }
2377
2378        if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2379                                !is_broadcast_ether_addr(params->bssid)) {
2380                ret = set_bssid(usbdev, params->bssid);
2381                if (ret < 0) {
2382                        netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2383                                   ret);
2384                        goto err_turn_radio_on;
2385                }
2386        } else
2387                clear_bssid(usbdev);
2388
2389        length = params->ssid_len;
2390        if (length > NDIS_802_11_LENGTH_SSID)
2391                length = NDIS_802_11_LENGTH_SSID;
2392
2393        memset(&ssid, 0, sizeof(ssid));
2394        ssid.length = cpu_to_le32(length);
2395        memcpy(ssid.essid, params->ssid, length);
2396
2397        /* Don't need to pause rx queue for ad-hoc. */
2398        usbnet_purge_paused_rxq(usbdev);
2399        usbnet_resume_rx(usbdev);
2400
2401        ret = set_essid(usbdev, &ssid);
2402        if (ret < 0)
2403                netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2404                           ret);
2405        return ret;
2406
2407err_turn_radio_on:
2408        disassociate(usbdev, true);
2409
2410        return ret;
2411}
2412
2413static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2414{
2415        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2416        struct usbnet *usbdev = priv->usbdev;
2417
2418        netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2419
2420        priv->connected = false;
2421        memset(priv->bssid, 0, ETH_ALEN);
2422
2423        return deauthenticate(usbdev);
2424}
2425
2426static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
2427        struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2428{
2429        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2430        struct usbnet *usbdev = priv->usbdev;
2431
2432        return set_channel(usbdev,
2433                        ieee80211_frequency_to_channel(chan->center_freq));
2434}
2435
2436static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2437                         u8 key_index, bool pairwise, const u8 *mac_addr,
2438                         struct key_params *params)
2439{
2440        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2441        struct usbnet *usbdev = priv->usbdev;
2442        __le32 flags;
2443
2444        netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2445                   __func__, key_index, mac_addr, params->cipher);
2446
2447        switch (params->cipher) {
2448        case WLAN_CIPHER_SUITE_WEP40:
2449        case WLAN_CIPHER_SUITE_WEP104:
2450                return add_wep_key(usbdev, params->key, params->key_len,
2451                                                                key_index);
2452        case WLAN_CIPHER_SUITE_TKIP:
2453        case WLAN_CIPHER_SUITE_CCMP:
2454                flags = 0;
2455
2456                if (params->seq && params->seq_len > 0)
2457                        flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2458                if (mac_addr)
2459                        flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2460                                        NDIS_80211_ADDKEY_TRANSMIT_KEY;
2461
2462                return add_wpa_key(usbdev, params->key, params->key_len,
2463                                key_index, mac_addr, params->seq,
2464                                params->seq_len, params->cipher, flags);
2465        default:
2466                netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2467                           __func__, params->cipher);
2468                return -ENOTSUPP;
2469        }
2470}
2471
2472static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2473                         u8 key_index, bool pairwise, const u8 *mac_addr)
2474{
2475        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2476        struct usbnet *usbdev = priv->usbdev;
2477
2478        netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2479
2480        return remove_key(usbdev, key_index, mac_addr);
2481}
2482
2483static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2484                                 u8 key_index, bool unicast, bool multicast)
2485{
2486        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2487        struct usbnet *usbdev = priv->usbdev;
2488        struct rndis_wlan_encr_key key;
2489
2490        netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2491
2492        if (key_index >= RNDIS_WLAN_NUM_KEYS)
2493                return -ENOENT;
2494
2495        priv->encr_tx_key_index = key_index;
2496
2497        if (is_wpa_key(priv, key_index))
2498                return 0;
2499
2500        key = priv->encr_keys[key_index];
2501
2502        return add_wep_key(usbdev, key.material, key.len, key_index);
2503}
2504
2505static void rndis_fill_station_info(struct usbnet *usbdev,
2506                                                struct station_info *sinfo)
2507{
2508        __le32 linkspeed, rssi;
2509        int ret, len;
2510
2511        memset(sinfo, 0, sizeof(*sinfo));
2512
2513        len = sizeof(linkspeed);
2514        ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2515        if (ret == 0) {
2516                sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2517                sinfo->filled |= STATION_INFO_TX_BITRATE;
2518        }
2519
2520        len = sizeof(rssi);
2521        ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2522        if (ret == 0) {
2523                sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2524                sinfo->filled |= STATION_INFO_SIGNAL;
2525        }
2526}
2527
2528static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2529                                        u8 *mac, struct station_info *sinfo)
2530{
2531        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2532        struct usbnet *usbdev = priv->usbdev;
2533
2534        if (compare_ether_addr(priv->bssid, mac))
2535                return -ENOENT;
2536
2537        rndis_fill_station_info(usbdev, sinfo);
2538
2539        return 0;
2540}
2541
2542static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2543                               int idx, u8 *mac, struct station_info *sinfo)
2544{
2545        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2546        struct usbnet *usbdev = priv->usbdev;
2547
2548        if (idx != 0)
2549                return -ENOENT;
2550
2551        memcpy(mac, priv->bssid, ETH_ALEN);
2552
2553        rndis_fill_station_info(usbdev, sinfo);
2554
2555        return 0;
2556}
2557
2558static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2559                                struct cfg80211_pmksa *pmksa)
2560{
2561        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2562        struct usbnet *usbdev = priv->usbdev;
2563        struct ndis_80211_pmkid *pmkids;
2564        u32 *tmp = (u32 *)pmksa->pmkid;
2565
2566        netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2567                        pmksa->bssid,
2568                        cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2569                        cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2570
2571        pmkids = get_device_pmkids(usbdev);
2572        if (IS_ERR(pmkids)) {
2573                /* couldn't read PMKID cache from device */
2574                return PTR_ERR(pmkids);
2575        }
2576
2577        pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2578        if (IS_ERR(pmkids)) {
2579                /* not found, list full, etc */
2580                return PTR_ERR(pmkids);
2581        }
2582
2583        return set_device_pmkids(usbdev, pmkids);
2584}
2585
2586static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2587                                struct cfg80211_pmksa *pmksa)
2588{
2589        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2590        struct usbnet *usbdev = priv->usbdev;
2591        struct ndis_80211_pmkid *pmkids;
2592        u32 *tmp = (u32 *)pmksa->pmkid;
2593
2594        netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2595                        pmksa->bssid,
2596                        cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2597                        cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2598
2599        pmkids = get_device_pmkids(usbdev);
2600        if (IS_ERR(pmkids)) {
2601                /* Couldn't read PMKID cache from device */
2602                return PTR_ERR(pmkids);
2603        }
2604
2605        pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2606        if (IS_ERR(pmkids)) {
2607                /* not found, etc */
2608                return PTR_ERR(pmkids);
2609        }
2610
2611        return set_device_pmkids(usbdev, pmkids);
2612}
2613
2614static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2615{
2616        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2617        struct usbnet *usbdev = priv->usbdev;
2618        struct ndis_80211_pmkid pmkid;
2619
2620        netdev_dbg(usbdev->net, "%s()\n", __func__);
2621
2622        memset(&pmkid, 0, sizeof(pmkid));
2623
2624        pmkid.length = cpu_to_le32(sizeof(pmkid));
2625        pmkid.bssid_info_count = cpu_to_le32(0);
2626
2627        return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
2628}
2629
2630static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2631                                bool enabled, int timeout)
2632{
2633        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2634        struct usbnet *usbdev = priv->usbdev;
2635        int power_mode;
2636        __le32 mode;
2637        int ret;
2638
2639        if (priv->device_type != RNDIS_BCM4320B)
2640                return -ENOTSUPP;
2641
2642        netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
2643                                enabled ? "enabled" : "disabled",
2644                                timeout);
2645
2646        if (enabled)
2647                power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
2648        else
2649                power_mode = NDIS_80211_POWER_MODE_CAM;
2650
2651        if (power_mode == priv->power_mode)
2652                return 0;
2653
2654        priv->power_mode = power_mode;
2655
2656        mode = cpu_to_le32(power_mode);
2657        ret = rndis_set_oid(usbdev, OID_802_11_POWER_MODE, &mode, sizeof(mode));
2658
2659        netdev_dbg(usbdev->net, "%s(): OID_802_11_POWER_MODE -> %d\n",
2660                                __func__, ret);
2661
2662        return ret;
2663}
2664
2665static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
2666                                        struct net_device *dev,
2667                                        s32 rssi_thold, u32 rssi_hyst)
2668{
2669        struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2670
2671        priv->cqm_rssi_thold = rssi_thold;
2672        priv->cqm_rssi_hyst = rssi_hyst;
2673        priv->last_cqm_event_rssi = 0;
2674
2675        return 0;
2676}
2677
2678static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
2679                                           struct ndis_80211_assoc_info *info)
2680{
2681        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2682        struct ieee80211_channel *channel;
2683        struct ndis_80211_ssid ssid;
2684        struct cfg80211_bss *bss;
2685        s32 signal;
2686        u64 timestamp;
2687        u16 capability;
2688        u32 beacon_period = 0;
2689        __le32 rssi;
2690        u8 ie_buf[34];
2691        int len, ret, ie_len;
2692
2693        /* Get signal quality, in case of error use rssi=0 and ignore error. */
2694        len = sizeof(rssi);
2695        rssi = 0;
2696        ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2697        signal = level_to_qual(le32_to_cpu(rssi));
2698
2699        netdev_dbg(usbdev->net, "%s(): OID_802_11_RSSI -> %d, "
2700                   "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
2701                   level_to_qual(le32_to_cpu(rssi)));
2702
2703        /* Get AP capabilities */
2704        if (info) {
2705                capability = le16_to_cpu(info->resp_ie.capa);
2706        } else {
2707                /* Set atleast ESS/IBSS capability */
2708                capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
2709                                WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
2710        }
2711
2712        /* Get channel and beacon interval */
2713        channel = get_current_channel(usbdev, &beacon_period);
2714        if (!channel) {
2715                netdev_warn(usbdev->net, "%s(): could not get channel.\n",
2716                                        __func__);
2717                return;
2718        }
2719
2720        /* Get SSID, in case of error, use zero length SSID and ignore error. */
2721        len = sizeof(ssid);
2722        memset(&ssid, 0, sizeof(ssid));
2723        ret = rndis_query_oid(usbdev, OID_802_11_SSID, &ssid, &len);
2724        netdev_dbg(usbdev->net, "%s(): OID_802_11_SSID -> %d, len: %d, ssid: "
2725                                "'%.32s'\n", __func__, ret,
2726                                le32_to_cpu(ssid.length), ssid.essid);
2727
2728        if (le32_to_cpu(ssid.length) > 32)
2729                ssid.length = cpu_to_le32(32);
2730
2731        ie_buf[0] = WLAN_EID_SSID;
2732        ie_buf[1] = le32_to_cpu(ssid.length);
2733        memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
2734
2735        ie_len = le32_to_cpu(ssid.length) + 2;
2736
2737        /* no tsf */
2738        timestamp = 0;
2739
2740        netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
2741                "capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
2742                "signal:%d\n", __func__, (channel ? channel->center_freq : -1),
2743                bssid, (u32)timestamp, capability, beacon_period, ie_len,
2744                ssid.essid, signal);
2745
2746        bss = cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid,
2747                timestamp, capability, beacon_period, ie_buf, ie_len,
2748                signal, GFP_KERNEL);
2749        cfg80211_put_bss(bss);
2750}
2751
2752/*
2753 * workers, indication handlers, device poller
2754 */
2755static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2756{
2757        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2758        struct ndis_80211_assoc_info *info = NULL;
2759        u8 bssid[ETH_ALEN];
2760        unsigned int resp_ie_len, req_ie_len;
2761        unsigned int offset;
2762        u8 *req_ie, *resp_ie;
2763        int ret;
2764        bool roamed = false;
2765        bool match_bss;
2766
2767        if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2768                /* received media connect indication while connected, either
2769                 * device reassociated with same AP or roamed to new. */
2770                roamed = true;
2771        }
2772
2773        req_ie_len = 0;
2774        resp_ie_len = 0;
2775        req_ie = NULL;
2776        resp_ie = NULL;
2777
2778        if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2779                info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2780                if (!info) {
2781                        /* No memory? Try resume work later */
2782                        set_bit(WORK_LINK_UP, &priv->work_pending);
2783                        queue_work(priv->workqueue, &priv->work);
2784                        return;
2785                }
2786
2787                /* Get association info IEs from device. */
2788                ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2789                if (!ret) {
2790                        req_ie_len = le32_to_cpu(info->req_ie_length);
2791                        if (req_ie_len > CONTROL_BUFFER_SIZE)
2792                                req_ie_len = CONTROL_BUFFER_SIZE;
2793                        if (req_ie_len != 0) {
2794                                offset = le32_to_cpu(info->offset_req_ies);
2795
2796                                if (offset > CONTROL_BUFFER_SIZE)
2797                                        offset = CONTROL_BUFFER_SIZE;
2798
2799                                req_ie = (u8 *)info + offset;
2800
2801                                if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2802                                        req_ie_len =
2803                                                CONTROL_BUFFER_SIZE - offset;
2804                        }
2805
2806                        resp_ie_len = le32_to_cpu(info->resp_ie_length);
2807                        if (resp_ie_len > CONTROL_BUFFER_SIZE)
2808                                resp_ie_len = CONTROL_BUFFER_SIZE;
2809                        if (resp_ie_len != 0) {
2810                                offset = le32_to_cpu(info->offset_resp_ies);
2811
2812                                if (offset > CONTROL_BUFFER_SIZE)
2813                                        offset = CONTROL_BUFFER_SIZE;
2814
2815                                resp_ie = (u8 *)info + offset;
2816
2817                                if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2818                                        resp_ie_len =
2819                                                CONTROL_BUFFER_SIZE - offset;
2820                        }
2821                } else {
2822                        /* Since rndis_wlan_craft_connected_bss() might use info
2823                         * later and expects info to contain valid data if
2824                         * non-null, free info and set NULL here.
2825                         */
2826                        kfree(info);
2827                        info = NULL;
2828                }
2829        } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2830                return;
2831
2832        ret = get_bssid(usbdev, bssid);
2833        if (ret < 0)
2834                memset(bssid, 0, sizeof(bssid));
2835
2836        netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2837                   bssid, roamed ? " roamed" : "");
2838
2839        /* Internal bss list in device should contain at least the currently
2840         * connected bss and we can get it to cfg80211 with
2841         * rndis_check_bssid_list().
2842         *
2843         * NDIS spec says: "If the device is associated, but the associated
2844         *  BSSID is not in its BSSID scan list, then the driver must add an
2845         *  entry for the BSSID at the end of the data that it returns in
2846         *  response to query of OID_802_11_BSSID_LIST."
2847         *
2848         * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
2849         */
2850        match_bss = false;
2851        rndis_check_bssid_list(usbdev, bssid, &match_bss);
2852
2853        if (!is_zero_ether_addr(bssid) && !match_bss) {
2854                /* Couldn't get bss from device, we need to manually craft bss
2855                 * for cfg80211.
2856                 */
2857                rndis_wlan_craft_connected_bss(usbdev, bssid, info);
2858        }
2859
2860        if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2861                if (!roamed)
2862                        cfg80211_connect_result(usbdev->net, bssid, req_ie,
2863                                                req_ie_len, resp_ie,
2864                                                resp_ie_len, 0, GFP_KERNEL);
2865                else
2866                        cfg80211_roamed(usbdev->net,
2867                                        get_current_channel(usbdev, NULL),
2868                                        bssid, req_ie, req_ie_len,
2869                                        resp_ie, resp_ie_len, GFP_KERNEL);
2870        } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2871                cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2872
2873        if (info != NULL)
2874                kfree(info);
2875
2876        priv->connected = true;
2877        memcpy(priv->bssid, bssid, ETH_ALEN);
2878
2879        usbnet_resume_rx(usbdev);
2880        netif_carrier_on(usbdev->net);
2881}
2882
2883static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2884{
2885        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2886
2887        if (priv->connected) {
2888                priv->connected = false;
2889                memset(priv->bssid, 0, ETH_ALEN);
2890
2891                deauthenticate(usbdev);
2892
2893                cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
2894        }
2895
2896        netif_carrier_off(usbdev->net);
2897}
2898
2899static void rndis_wlan_worker(struct work_struct *work)
2900{
2901        struct rndis_wlan_private *priv =
2902                container_of(work, struct rndis_wlan_private, work);
2903        struct usbnet *usbdev = priv->usbdev;
2904
2905        if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2906                rndis_wlan_do_link_up_work(usbdev);
2907
2908        if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2909                rndis_wlan_do_link_down_work(usbdev);
2910
2911        if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2912                set_multicast_list(usbdev);
2913}
2914
2915static void rndis_wlan_set_multicast_list(struct net_device *dev)
2916{
2917        struct usbnet *usbdev = netdev_priv(dev);
2918        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2919
2920        if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2921                return;
2922
2923        set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2924        queue_work(priv->workqueue, &priv->work);
2925}
2926
2927static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2928                                struct ndis_80211_status_indication *indication,
2929                                int len)
2930{
2931        u8 *buf;
2932        const char *type;
2933        int flags, buflen, key_id;
2934        bool pairwise_error, group_error;
2935        struct ndis_80211_auth_request *auth_req;
2936        enum nl80211_key_type key_type;
2937
2938        /* must have at least one array entry */
2939        if (len < offsetof(struct ndis_80211_status_indication, u) +
2940                                sizeof(struct ndis_80211_auth_request)) {
2941                netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2942                            len);
2943                return;
2944        }
2945
2946        buf = (void *)&indication->u.auth_request[0];
2947        buflen = len - offsetof(struct ndis_80211_status_indication, u);
2948
2949        while (buflen >= sizeof(*auth_req)) {
2950                auth_req = (void *)buf;
2951                type = "unknown";
2952                flags = le32_to_cpu(auth_req->flags);
2953                pairwise_error = false;
2954                group_error = false;
2955
2956                if (flags & 0x1)
2957                        type = "reauth request";
2958                if (flags & 0x2)
2959                        type = "key update request";
2960                if (flags & 0x6) {
2961                        pairwise_error = true;
2962                        type = "pairwise_error";
2963                }
2964                if (flags & 0xe) {
2965                        group_error = true;
2966                        type = "group_error";
2967                }
2968
2969                netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2970                            type, le32_to_cpu(auth_req->flags));
2971
2972                if (pairwise_error) {
2973                        key_type = NL80211_KEYTYPE_PAIRWISE;
2974                        key_id = -1;
2975
2976                        cfg80211_michael_mic_failure(usbdev->net,
2977                                                        auth_req->bssid,
2978                                                        key_type, key_id, NULL,
2979                                                        GFP_KERNEL);
2980                }
2981
2982                if (group_error) {
2983                        key_type = NL80211_KEYTYPE_GROUP;
2984                        key_id = -1;
2985
2986                        cfg80211_michael_mic_failure(usbdev->net,
2987                                                        auth_req->bssid,
2988                                                        key_type, key_id, NULL,
2989                                                        GFP_KERNEL);
2990                }
2991
2992                buflen -= le32_to_cpu(auth_req->length);
2993                buf += le32_to_cpu(auth_req->length);
2994        }
2995}
2996
2997static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2998                                struct ndis_80211_status_indication *indication,
2999                                int len)
3000{
3001        struct ndis_80211_pmkid_cand_list *cand_list;
3002        int list_len, expected_len, i;
3003
3004        if (len < offsetof(struct ndis_80211_status_indication, u) +
3005                                sizeof(struct ndis_80211_pmkid_cand_list)) {
3006                netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
3007                            len);
3008                return;
3009        }
3010
3011        list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
3012                        sizeof(struct ndis_80211_pmkid_candidate);
3013        expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
3014                        offsetof(struct ndis_80211_status_indication, u);
3015
3016        if (len < expected_len) {
3017                netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
3018                            len, expected_len);
3019                return;
3020        }
3021
3022        cand_list = &indication->u.cand_list;
3023
3024        netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
3025                    le32_to_cpu(cand_list->version),
3026                    le32_to_cpu(cand_list->num_candidates));
3027
3028        if (le32_to_cpu(cand_list->version) != 1)
3029                return;
3030
3031        for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
3032                struct ndis_80211_pmkid_candidate *cand =
3033                                                &cand_list->candidate_list[i];
3034                bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
3035
3036                netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
3037                           i, le32_to_cpu(cand->flags), preauth, cand->bssid);
3038
3039                cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
3040                                                preauth, GFP_ATOMIC);
3041        }
3042}
3043
3044static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
3045                        struct rndis_indicate *msg, int buflen)
3046{
3047        struct ndis_80211_status_indication *indication;
3048        unsigned int len, offset;
3049
3050        offset = offsetof(struct rndis_indicate, status) +
3051                        le32_to_cpu(msg->offset);
3052        len = le32_to_cpu(msg->length);
3053
3054        if (len < 8) {
3055                netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
3056                            len);
3057                return;
3058        }
3059
3060        if (len > buflen || offset > buflen || offset + len > buflen) {
3061                netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
3062                            offset + len, buflen);
3063                return;
3064        }
3065
3066        indication = (void *)((u8 *)msg + offset);
3067
3068        switch (le32_to_cpu(indication->status_type)) {
3069        case NDIS_80211_STATUSTYPE_RADIOSTATE:
3070                netdev_info(usbdev->net, "radio state indication: %i\n",
3071                            le32_to_cpu(indication->u.radio_status));
3072                return;
3073
3074        case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
3075                netdev_info(usbdev->net, "media stream mode indication: %i\n",
3076                            le32_to_cpu(indication->u.media_stream_mode));
3077                return;
3078
3079        case NDIS_80211_STATUSTYPE_AUTHENTICATION:
3080                rndis_wlan_auth_indication(usbdev, indication, len);
3081                return;
3082
3083        case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
3084                rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
3085                return;
3086
3087        default:
3088                netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
3089                            le32_to_cpu(indication->status_type));
3090        }
3091}
3092
3093static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
3094{
3095        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3096        struct rndis_indicate *msg = ind;
3097
3098        switch (msg->status) {
3099        case RNDIS_STATUS_MEDIA_CONNECT:
3100                if (priv->current_command_oid == OID_802_11_ADD_KEY) {
3101                        /* OID_802_11_ADD_KEY causes sometimes extra
3102                         * "media connect" indications which confuses driver
3103                         * and userspace to think that device is
3104                         * roaming/reassociating when it isn't.
3105                         */
3106                        netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
3107                        return;
3108                }
3109
3110                usbnet_pause_rx(usbdev);
3111
3112                netdev_info(usbdev->net, "media connect\n");
3113
3114                /* queue work to avoid recursive calls into rndis_command */
3115                set_bit(WORK_LINK_UP, &priv->work_pending);
3116                queue_work(priv->workqueue, &priv->work);
3117                break;
3118
3119        case RNDIS_STATUS_MEDIA_DISCONNECT:
3120                netdev_info(usbdev->net, "media disconnect\n");
3121
3122                /* queue work to avoid recursive calls into rndis_command */
3123                set_bit(WORK_LINK_DOWN, &priv->work_pending);
3124                queue_work(priv->workqueue, &priv->work);
3125                break;
3126
3127        case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
3128                rndis_wlan_media_specific_indication(usbdev, msg, buflen);
3129                break;
3130
3131        default:
3132                netdev_info(usbdev->net, "indication: 0x%08x\n",
3133                            le32_to_cpu(msg->status));
3134                break;
3135        }
3136}
3137
3138static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
3139{
3140        struct {
3141                __le32  num_items;
3142                __le32  items[8];
3143        } networks_supported;
3144        struct ndis_80211_capability *caps;
3145        u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
3146        int len, retval, i, n;
3147        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3148
3149        /* determine supported modes */
3150        len = sizeof(networks_supported);
3151        retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
3152                                                &networks_supported, &len);
3153        if (retval >= 0) {
3154                n = le32_to_cpu(networks_supported.num_items);
3155                if (n > 8)
3156                        n = 8;
3157                for (i = 0; i < n; i++) {
3158                        switch (le32_to_cpu(networks_supported.items[i])) {
3159                        case NDIS_80211_TYPE_FREQ_HOP:
3160                        case NDIS_80211_TYPE_DIRECT_SEQ:
3161                                priv->caps |= CAP_MODE_80211B;
3162                                break;
3163                        case NDIS_80211_TYPE_OFDM_A:
3164                                priv->caps |= CAP_MODE_80211A;
3165                                break;
3166                        case NDIS_80211_TYPE_OFDM_G:
3167                                priv->caps |= CAP_MODE_80211G;
3168                                break;
3169                        }
3170                }
3171        }
3172
3173        /* get device 802.11 capabilities, number of PMKIDs */
3174        caps = (struct ndis_80211_capability *)caps_buf;
3175        len = sizeof(caps_buf);
3176        retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
3177        if (retval >= 0) {
3178                netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
3179                                "ver %d, pmkids %d, auth-encr-pairs %d\n",
3180                                le32_to_cpu(caps->length),
3181                                le32_to_cpu(caps->version),
3182                                le32_to_cpu(caps->num_pmkids),
3183                                le32_to_cpu(caps->num_auth_encr_pair));
3184                wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
3185        } else
3186                wiphy->max_num_pmkids = 0;
3187
3188        return retval;
3189}
3190
3191static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
3192{
3193        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3194        enum nl80211_cqm_rssi_threshold_event event;
3195        int thold, hyst, last_event;
3196
3197        if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
3198                return;
3199        if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
3200                return;
3201
3202        last_event = priv->last_cqm_event_rssi;
3203        thold = priv->cqm_rssi_thold;
3204        hyst = priv->cqm_rssi_hyst;
3205
3206        if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
3207                event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
3208        else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
3209                event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
3210        else
3211                return;
3212
3213        priv->last_cqm_event_rssi = rssi;
3214        cfg80211_cqm_rssi_notify(usbdev->net, event, GFP_KERNEL);
3215}
3216
3217#define DEVICE_POLLER_JIFFIES (HZ)
3218static void rndis_device_poller(struct work_struct *work)
3219{
3220        struct rndis_wlan_private *priv =
3221                container_of(work, struct rndis_wlan_private,
3222                                                        dev_poller_work.work);
3223        struct usbnet *usbdev = priv->usbdev;
3224        __le32 rssi, tmp;
3225        int len, ret, j;
3226        int update_jiffies = DEVICE_POLLER_JIFFIES;
3227        void *buf;
3228
3229        /* Only check/do workaround when connected. Calling is_associated()
3230         * also polls device with rndis_command() and catches for media link
3231         * indications.
3232         */
3233        if (!is_associated(usbdev)) {
3234                /* Workaround bad scanning in BCM4320a devices with active
3235                 * background scanning when not associated.
3236                 */
3237                if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
3238                    !priv->scan_request) {
3239                        /* Get previous scan results */
3240                        rndis_check_bssid_list(usbdev, NULL, NULL);
3241
3242                        /* Initiate new scan */
3243                        rndis_start_bssid_list_scan(usbdev);
3244                }
3245
3246                goto end;
3247        }
3248
3249        len = sizeof(rssi);
3250        ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
3251        if (ret == 0) {
3252                priv->last_qual = level_to_qual(le32_to_cpu(rssi));
3253                rndis_do_cqm(usbdev, le32_to_cpu(rssi));
3254        }
3255
3256        netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
3257                   ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
3258
3259        /* Workaround transfer stalls on poor quality links.
3260         * TODO: find right way to fix these stalls (as stalls do not happen
3261         * with ndiswrapper/windows driver). */
3262        if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
3263                /* Decrease stats worker interval to catch stalls.
3264                 * faster. Faster than 400-500ms causes packet loss,
3265                 * Slower doesn't catch stalls fast enough.
3266                 */
3267                j = msecs_to_jiffies(priv->param_workaround_interval);
3268                if (j > DEVICE_POLLER_JIFFIES)
3269                        j = DEVICE_POLLER_JIFFIES;
3270                else if (j <= 0)
3271                        j = 1;
3272                update_jiffies = j;
3273
3274                /* Send scan OID. Use of both OIDs is required to get device
3275                 * working.
3276                 */
3277                tmp = cpu_to_le32(1);
3278                rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
3279                                                                sizeof(tmp));
3280
3281                len = CONTROL_BUFFER_SIZE;
3282                buf = kmalloc(len, GFP_KERNEL);
3283                if (!buf)
3284                        goto end;
3285
3286                rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
3287                kfree(buf);
3288        }
3289
3290end:
3291        if (update_jiffies >= HZ)
3292                update_jiffies = round_jiffies_relative(update_jiffies);
3293        else {
3294                j = round_jiffies_relative(update_jiffies);
3295                if (abs(j - update_jiffies) <= 10)
3296                        update_jiffies = j;
3297        }
3298
3299        queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3300                                                                update_jiffies);
3301}
3302
3303/*
3304 * driver/device initialization
3305 */
3306static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
3307{
3308        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3309
3310        priv->device_type = device_type;
3311
3312        priv->param_country[0] = modparam_country[0];
3313        priv->param_country[1] = modparam_country[1];
3314        priv->param_country[2] = 0;
3315        priv->param_frameburst   = modparam_frameburst;
3316        priv->param_afterburner  = modparam_afterburner;
3317        priv->param_power_save   = modparam_power_save;
3318        priv->param_power_output = modparam_power_output;
3319        priv->param_roamtrigger  = modparam_roamtrigger;
3320        priv->param_roamdelta    = modparam_roamdelta;
3321
3322        priv->param_country[0] = toupper(priv->param_country[0]);
3323        priv->param_country[1] = toupper(priv->param_country[1]);
3324        /* doesn't support EU as country code, use FI instead */
3325        if (!strcmp(priv->param_country, "EU"))
3326                strcpy(priv->param_country, "FI");
3327
3328        if (priv->param_power_save < 0)
3329                priv->param_power_save = 0;
3330        else if (priv->param_power_save > 2)
3331                priv->param_power_save = 2;
3332
3333        if (priv->param_power_output < 0)
3334                priv->param_power_output = 0;
3335        else if (priv->param_power_output > 3)
3336                priv->param_power_output = 3;
3337
3338        if (priv->param_roamtrigger < -80)
3339                priv->param_roamtrigger = -80;
3340        else if (priv->param_roamtrigger > -60)
3341                priv->param_roamtrigger = -60;
3342
3343        if (priv->param_roamdelta < 0)
3344                priv->param_roamdelta = 0;
3345        else if (priv->param_roamdelta > 2)
3346                priv->param_roamdelta = 2;
3347
3348        if (modparam_workaround_interval < 0)
3349                priv->param_workaround_interval = 500;
3350        else
3351                priv->param_workaround_interval = modparam_workaround_interval;
3352}
3353
3354static int unknown_early_init(struct usbnet *usbdev)
3355{
3356        /* copy module parameters for unknown so that iwconfig reports txpower
3357         * and workaround parameter is copied to private structure correctly.
3358         */
3359        rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
3360
3361        /* This is unknown device, so do not try set configuration parameters.
3362         */
3363
3364        return 0;
3365}
3366
3367static int bcm4320a_early_init(struct usbnet *usbdev)
3368{
3369        /* copy module parameters for bcm4320a so that iwconfig reports txpower
3370         * and workaround parameter is copied to private structure correctly.
3371         */
3372        rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
3373
3374        /* bcm4320a doesn't handle configuration parameters well. Try
3375         * set any and you get partially zeroed mac and broken device.
3376         */
3377
3378        return 0;
3379}
3380
3381static int bcm4320b_early_init(struct usbnet *usbdev)
3382{
3383        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3384        char buf[8];
3385
3386        rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
3387
3388        /* Early initialization settings, setting these won't have effect
3389         * if called after generic_rndis_bind().
3390         */
3391
3392        rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3393        rndis_set_config_parameter_str(usbdev, "FrameBursting",
3394                                        priv->param_frameburst ? "1" : "0");
3395        rndis_set_config_parameter_str(usbdev, "Afterburner",
3396                                        priv->param_afterburner ? "1" : "0");
3397        sprintf(buf, "%d", priv->param_power_save);
3398        rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3399        sprintf(buf, "%d", priv->param_power_output);
3400        rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3401        sprintf(buf, "%d", priv->param_roamtrigger);
3402        rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3403        sprintf(buf, "%d", priv->param_roamdelta);
3404        rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3405
3406        return 0;
3407}
3408
3409/* same as rndis_netdev_ops but with local multicast handler */
3410static const struct net_device_ops rndis_wlan_netdev_ops = {
3411        .ndo_open               = usbnet_open,
3412        .ndo_stop               = usbnet_stop,
3413        .ndo_start_xmit         = usbnet_start_xmit,
3414        .ndo_tx_timeout         = usbnet_tx_timeout,
3415        .ndo_set_mac_address    = eth_mac_addr,
3416        .ndo_validate_addr      = eth_validate_addr,
3417        .ndo_set_rx_mode        = rndis_wlan_set_multicast_list,
3418};
3419
3420static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3421{
3422        struct wiphy *wiphy;
3423        struct rndis_wlan_private *priv;
3424        int retval, len;
3425        __le32 tmp;
3426
3427        /* allocate wiphy and rndis private data
3428         * NOTE: We only support a single virtual interface, so wiphy
3429         * and wireless_dev are somewhat synonymous for this device.
3430         */
3431        wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3432        if (!wiphy)
3433                return -ENOMEM;
3434
3435        priv = wiphy_priv(wiphy);
3436        usbdev->net->ieee80211_ptr = &priv->wdev;
3437        priv->wdev.wiphy = wiphy;
3438        priv->wdev.iftype = NL80211_IFTYPE_STATION;
3439
3440        /* These have to be initialized before calling generic_rndis_bind().
3441         * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3442         */
3443        usbdev->driver_priv = priv;
3444        priv->usbdev = usbdev;
3445
3446        mutex_init(&priv->command_lock);
3447
3448        /* because rndis_command() sleeps we need to use workqueue */
3449        priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3450        INIT_WORK(&priv->work, rndis_wlan_worker);
3451        INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3452        INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3453
3454        /* try bind rndis_host */
3455        retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3456        if (retval < 0)
3457                goto fail;
3458
3459        /* generic_rndis_bind set packet filter to multicast_all+
3460         * promisc mode which doesn't work well for our devices (device
3461         * picks up rssi to closest station instead of to access point).
3462         *
3463         * rndis_host wants to avoid all OID as much as possible
3464         * so do promisc/multicast handling in rndis_wlan.
3465         */
3466        usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3467
3468        tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3469        retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3470                                                                sizeof(tmp));
3471
3472        len = sizeof(tmp);
3473        retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3474                                                                &len);
3475        priv->multicast_size = le32_to_cpu(tmp);
3476        if (retval < 0 || priv->multicast_size < 0)
3477                priv->multicast_size = 0;
3478        if (priv->multicast_size > 0)
3479                usbdev->net->flags |= IFF_MULTICAST;
3480        else
3481                usbdev->net->flags &= ~IFF_MULTICAST;
3482
3483        /* fill-out wiphy structure and register w/ cfg80211 */
3484        memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3485        wiphy->privid = rndis_wiphy_privid;
3486        wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3487                                        | BIT(NL80211_IFTYPE_ADHOC);
3488        wiphy->max_scan_ssids = 1;
3489
3490        /* TODO: fill-out band/encr information based on priv->caps */
3491        rndis_wlan_get_caps(usbdev, wiphy);
3492
3493        memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3494        memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3495        priv->band.channels = priv->channels;
3496        priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3497        priv->band.bitrates = priv->rates;
3498        priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3499        wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3500        wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3501
3502        memcpy(priv->cipher_suites, rndis_cipher_suites,
3503                                                sizeof(rndis_cipher_suites));
3504        wiphy->cipher_suites = priv->cipher_suites;
3505        wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3506
3507        set_wiphy_dev(wiphy, &usbdev->udev->dev);
3508
3509        if (wiphy_register(wiphy)) {
3510                retval = -ENODEV;
3511                goto fail;
3512        }
3513
3514        set_default_iw_params(usbdev);
3515
3516        priv->power_mode = -1;
3517
3518        /* set default rts/frag */
3519        rndis_set_wiphy_params(wiphy,
3520                        WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3521
3522        /* turn radio off on init */
3523        priv->radio_on = false;
3524        disassociate(usbdev, false);
3525        netif_carrier_off(usbdev->net);
3526
3527        return 0;
3528
3529fail:
3530        cancel_delayed_work_sync(&priv->dev_poller_work);
3531        cancel_delayed_work_sync(&priv->scan_work);
3532        cancel_work_sync(&priv->work);
3533        flush_workqueue(priv->workqueue);
3534        destroy_workqueue(priv->workqueue);
3535
3536        wiphy_free(wiphy);
3537        return retval;
3538}
3539
3540static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3541{
3542        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3543
3544        /* turn radio off */
3545        disassociate(usbdev, false);
3546
3547        cancel_delayed_work_sync(&priv->dev_poller_work);
3548        cancel_delayed_work_sync(&priv->scan_work);
3549        cancel_work_sync(&priv->work);
3550        flush_workqueue(priv->workqueue);
3551        destroy_workqueue(priv->workqueue);
3552
3553        rndis_unbind(usbdev, intf);
3554
3555        wiphy_unregister(priv->wdev.wiphy);
3556        wiphy_free(priv->wdev.wiphy);
3557}
3558
3559static int rndis_wlan_reset(struct usbnet *usbdev)
3560{
3561        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3562        int retval;
3563
3564        netdev_dbg(usbdev->net, "%s()\n", __func__);
3565
3566        retval = rndis_reset(usbdev);
3567        if (retval)
3568                netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3569
3570        /* rndis_reset cleared multicast list, so restore here.
3571           (set_multicast_list() also turns on current packet filter) */
3572        set_multicast_list(usbdev);
3573
3574        queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3575                round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3576
3577        return deauthenticate(usbdev);
3578}
3579
3580static int rndis_wlan_stop(struct usbnet *usbdev)
3581{
3582        struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3583        int retval;
3584        __le32 filter;
3585
3586        netdev_dbg(usbdev->net, "%s()\n", __func__);
3587
3588        retval = disassociate(usbdev, false);
3589
3590        priv->work_pending = 0;
3591        cancel_delayed_work_sync(&priv->dev_poller_work);
3592        cancel_delayed_work_sync(&priv->scan_work);
3593        cancel_work_sync(&priv->work);
3594        flush_workqueue(priv->workqueue);
3595
3596        if (priv->scan_request) {
3597                cfg80211_scan_done(priv->scan_request, true);
3598                priv->scan_request = NULL;
3599        }
3600
3601        /* Set current packet filter zero to block receiving data packets from
3602           device. */
3603        filter = 0;
3604        rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3605                                                                sizeof(filter));
3606
3607        return retval;
3608}
3609
3610static const struct driver_info bcm4320b_info = {
3611        .description =  "Wireless RNDIS device, BCM4320b based",
3612        .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3613                                FLAG_AVOID_UNLINK_URBS,
3614        .bind =         rndis_wlan_bind,
3615        .unbind =       rndis_wlan_unbind,
3616        .status =       rndis_status,
3617        .rx_fixup =     rndis_rx_fixup,
3618        .tx_fixup =     rndis_tx_fixup,
3619        .reset =        rndis_wlan_reset,
3620        .stop =         rndis_wlan_stop,
3621        .early_init =   bcm4320b_early_init,
3622        .indication =   rndis_wlan_indication,
3623};
3624
3625static const struct driver_info bcm4320a_info = {
3626        .description =  "Wireless RNDIS device, BCM4320a based",
3627        .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3628                                FLAG_AVOID_UNLINK_URBS,
3629        .bind =         rndis_wlan_bind,
3630        .unbind =       rndis_wlan_unbind,
3631        .status =       rndis_status,
3632        .rx_fixup =     rndis_rx_fixup,
3633        .tx_fixup =     rndis_tx_fixup,
3634        .reset =        rndis_wlan_reset,
3635        .stop =         rndis_wlan_stop,
3636        .early_init =   bcm4320a_early_init,
3637        .indication =   rndis_wlan_indication,
3638};
3639
3640static const struct driver_info rndis_wlan_info = {
3641        .description =  "Wireless RNDIS device",
3642        .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3643                                FLAG_AVOID_UNLINK_URBS,
3644        .bind =         rndis_wlan_bind,
3645        .unbind =       rndis_wlan_unbind,
3646        .status =       rndis_status,
3647        .rx_fixup =     rndis_rx_fixup,
3648        .tx_fixup =     rndis_tx_fixup,
3649        .reset =        rndis_wlan_reset,
3650        .stop =         rndis_wlan_stop,
3651        .early_init =   unknown_early_init,
3652        .indication =   rndis_wlan_indication,
3653};
3654
3655/*-------------------------------------------------------------------------*/
3656
3657static const struct usb_device_id products [] = {
3658#define RNDIS_MASTER_INTERFACE \
3659        .bInterfaceClass        = USB_CLASS_COMM, \
3660        .bInterfaceSubClass     = 2 /* ACM */, \
3661        .bInterfaceProtocol     = 0x0ff
3662
3663/* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3664 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3665 */
3666{
3667        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3668                          | USB_DEVICE_ID_MATCH_DEVICE,
3669        .idVendor               = 0x0411,
3670        .idProduct              = 0x00bc,       /* Buffalo WLI-U2-KG125S */
3671        RNDIS_MASTER_INTERFACE,
3672        .driver_info            = (unsigned long) &bcm4320b_info,
3673}, {
3674        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3675                          | USB_DEVICE_ID_MATCH_DEVICE,
3676        .idVendor               = 0x0baf,
3677        .idProduct              = 0x011b,       /* U.S. Robotics USR5421 */
3678        RNDIS_MASTER_INTERFACE,
3679        .driver_info            = (unsigned long) &bcm4320b_info,
3680}, {
3681        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3682                          | USB_DEVICE_ID_MATCH_DEVICE,
3683        .idVendor               = 0x050d,
3684        .idProduct              = 0x011b,       /* Belkin F5D7051 */
3685        RNDIS_MASTER_INTERFACE,
3686        .driver_info            = (unsigned long) &bcm4320b_info,
3687}, {
3688        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3689                          | USB_DEVICE_ID_MATCH_DEVICE,
3690        .idVendor               = 0x1799,       /* Belkin has two vendor ids */
3691        .idProduct              = 0x011b,       /* Belkin F5D7051 */
3692        RNDIS_MASTER_INTERFACE,
3693        .driver_info            = (unsigned long) &bcm4320b_info,
3694}, {
3695        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3696                          | USB_DEVICE_ID_MATCH_DEVICE,
3697        .idVendor               = 0x13b1,
3698        .idProduct              = 0x0014,       /* Linksys WUSB54GSv2 */
3699        RNDIS_MASTER_INTERFACE,
3700        .driver_info            = (unsigned long) &bcm4320b_info,
3701}, {
3702        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3703                          | USB_DEVICE_ID_MATCH_DEVICE,
3704        .idVendor               = 0x13b1,
3705        .idProduct              = 0x0026,       /* Linksys WUSB54GSC */
3706        RNDIS_MASTER_INTERFACE,
3707        .driver_info            = (unsigned long) &bcm4320b_info,
3708}, {
3709        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3710                          | USB_DEVICE_ID_MATCH_DEVICE,
3711        .idVendor               = 0x0b05,
3712        .idProduct              = 0x1717,       /* Asus WL169gE */
3713        RNDIS_MASTER_INTERFACE,
3714        .driver_info            = (unsigned long) &bcm4320b_info,
3715}, {
3716        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3717                          | USB_DEVICE_ID_MATCH_DEVICE,
3718        .idVendor               = 0x0a5c,
3719        .idProduct              = 0xd11b,       /* Eminent EM4045 */
3720        RNDIS_MASTER_INTERFACE,
3721        .driver_info            = (unsigned long) &bcm4320b_info,
3722}, {
3723        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3724                          | USB_DEVICE_ID_MATCH_DEVICE,
3725        .idVendor               = 0x1690,
3726        .idProduct              = 0x0715,       /* BT Voyager 1055 */
3727        RNDIS_MASTER_INTERFACE,
3728        .driver_info            = (unsigned long) &bcm4320b_info,
3729},
3730/* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3731 * parameters available, hardware probably contain older firmware version with
3732 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3733 */
3734{
3735        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3736                          | USB_DEVICE_ID_MATCH_DEVICE,
3737        .idVendor               = 0x13b1,
3738        .idProduct              = 0x000e,       /* Linksys WUSB54GSv1 */
3739        RNDIS_MASTER_INTERFACE,
3740        .driver_info            = (unsigned long) &bcm4320a_info,
3741}, {
3742        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3743                          | USB_DEVICE_ID_MATCH_DEVICE,
3744        .idVendor               = 0x0baf,
3745        .idProduct              = 0x0111,       /* U.S. Robotics USR5420 */
3746        RNDIS_MASTER_INTERFACE,
3747        .driver_info            = (unsigned long) &bcm4320a_info,
3748}, {
3749        .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3750                          | USB_DEVICE_ID_MATCH_DEVICE,
3751        .idVendor               = 0x0411,
3752        .idProduct              = 0x004b,       /* BUFFALO WLI-USB-G54 */
3753        RNDIS_MASTER_INTERFACE,
3754        .driver_info            = (unsigned long) &bcm4320a_info,
3755},
3756/* Generic Wireless RNDIS devices that we don't have exact
3757 * idVendor/idProduct/chip yet.
3758 */
3759{
3760        /* RNDIS is MSFT's un-official variant of CDC ACM */
3761        USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3762        .driver_info = (unsigned long) &rndis_wlan_info,
3763}, {
3764        /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3765        USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3766        .driver_info = (unsigned long) &rndis_wlan_info,
3767},
3768        { },            // END
3769};
3770MODULE_DEVICE_TABLE(usb, products);
3771
3772static struct usb_driver rndis_wlan_driver = {
3773        .name =         "rndis_wlan",
3774        .id_table =     products,
3775        .probe =        usbnet_probe,
3776        .disconnect =   usbnet_disconnect,
3777        .suspend =      usbnet_suspend,
3778        .resume =       usbnet_resume,
3779};
3780
3781module_usb_driver(rndis_wlan_driver);
3782
3783MODULE_AUTHOR("Bjorge Dijkstra");
3784MODULE_AUTHOR("Jussi Kivilinna");
3785MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3786MODULE_LICENSE("GPL");
3787
3788