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