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