root/drivers/net/wireless/rndis_wlan.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_rndis_wlan_priv
  2. get_bcm4320_power_dbm
  3. is_wpa_key
  4. rndis_cipher_to_alg
  5. rndis_akm_suite_to_key_mgmt
  6. oid_to_string
  7. oid_to_string
  8. rndis_error_status
  9. rndis_query_oid
  10. rndis_set_oid
  11. rndis_reset
  12. rndis_set_config_parameter
  13. rndis_set_config_parameter_str
  14. level_to_qual
  15. rndis_start_bssid_list_scan
  16. set_essid
  17. set_bssid
  18. clear_bssid
  19. get_bssid
  20. get_association_info
  21. is_associated
  22. disassociate
  23. set_auth_mode
  24. set_priv_filter
  25. set_encr_mode
  26. set_infra_mode
  27. set_rts_threshold
  28. set_frag_threshold
  29. set_default_iw_params
  30. deauthenticate
  31. set_channel
  32. get_current_channel
  33. add_wep_key
  34. add_wpa_key
  35. restore_key
  36. restore_keys
  37. clear_key
  38. remove_key
  39. set_multicast_list
  40. debug_print_pmkids
  41. debug_print_pmkids
  42. get_device_pmkids
  43. set_device_pmkids
  44. remove_pmkid
  45. update_pmkid
  46. rndis_change_virtual_intf
  47. rndis_set_wiphy_params
  48. rndis_set_tx_power
  49. rndis_get_tx_power
  50. rndis_scan
  51. rndis_bss_info_update
  52. next_bssid_list_item
  53. check_bssid_list_item
  54. rndis_check_bssid_list
  55. rndis_get_scan_results
  56. rndis_connect
  57. rndis_disconnect
  58. rndis_join_ibss
  59. rndis_leave_ibss
  60. rndis_add_key
  61. rndis_del_key
  62. rndis_set_default_key
  63. rndis_fill_station_info
  64. rndis_get_station
  65. rndis_dump_station
  66. rndis_set_pmksa
  67. rndis_del_pmksa
  68. rndis_flush_pmksa
  69. rndis_set_power_mgmt
  70. rndis_set_cqm_rssi_config
  71. rndis_wlan_craft_connected_bss
  72. rndis_wlan_do_link_up_work
  73. rndis_wlan_do_link_down_work
  74. rndis_wlan_worker
  75. rndis_wlan_set_multicast_list
  76. rndis_wlan_auth_indication
  77. rndis_wlan_pmkid_cand_list_indication
  78. rndis_wlan_media_specific_indication
  79. rndis_wlan_indication
  80. rndis_wlan_get_caps
  81. rndis_do_cqm
  82. rndis_device_poller
  83. rndis_copy_module_params
  84. unknown_early_init
  85. bcm4320a_early_init
  86. bcm4320b_early_init
  87. rndis_wlan_bind
  88. rndis_wlan_unbind
  89. rndis_wlan_reset
  90. rndis_wlan_stop

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

/* [<][>][^][v][top][bottom][index][help] */