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