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