Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / wireless / rndis_wlan.c
1 /*
2  * Driver for RNDIS based wireless USB devices.
3  *
4  * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5  * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  Portions of this file are based on NDISwrapper project,
22  *  Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
23  *  http://ndiswrapper.sourceforge.net/
24  */
25
26 // #define      DEBUG                   // error path messages, extra info
27 // #define      VERBOSE                 // more; success messages
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/ethtool.h>
34 #include <linux/workqueue.h>
35 #include <linux/mutex.h>
36 #include <linux/mii.h>
37 #include <linux/usb.h>
38 #include <linux/usb/cdc.h>
39 #include <linux/wireless.h>
40 #include <linux/ieee80211.h>
41 #include <linux/if_arp.h>
42 #include <linux/ctype.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <net/iw_handler.h>
46 #include <net/cfg80211.h>
47 #include <linux/usb/usbnet.h>
48 #include <linux/usb/rndis_host.h>
49
50
51 /* NOTE: All these are settings for Broadcom chipset */
52 static char modparam_country[4] = "EU";
53 module_param_string(country, modparam_country, 4, 0444);
54 MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
55
56 static int modparam_frameburst = 1;
57 module_param_named(frameburst, modparam_frameburst, int, 0444);
58 MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
59
60 static int modparam_afterburner = 0;
61 module_param_named(afterburner, modparam_afterburner, int, 0444);
62 MODULE_PARM_DESC(afterburner,
63         "enable afterburner aka '125 High Speed Mode' (default: off)");
64
65 static int modparam_power_save = 0;
66 module_param_named(power_save, modparam_power_save, int, 0444);
67 MODULE_PARM_DESC(power_save,
68         "set power save mode: 0=off, 1=on, 2=fast (default: off)");
69
70 static int modparam_power_output = 3;
71 module_param_named(power_output, modparam_power_output, int, 0444);
72 MODULE_PARM_DESC(power_output,
73         "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
74
75 static int modparam_roamtrigger = -70;
76 module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
77 MODULE_PARM_DESC(roamtrigger,
78         "set roaming dBm trigger: -80=optimize for distance, "
79                                 "-60=bandwidth (default: -70)");
80
81 static int modparam_roamdelta = 1;
82 module_param_named(roamdelta, modparam_roamdelta, int, 0444);
83 MODULE_PARM_DESC(roamdelta,
84         "set roaming tendency: 0=aggressive, 1=moderate, "
85                                 "2=conservative (default: moderate)");
86
87 static int modparam_workaround_interval;
88 module_param_named(workaround_interval, modparam_workaround_interval,
89                                                         int, 0444);
90 MODULE_PARM_DESC(workaround_interval,
91         "set stall workaround interval in msecs (0=disabled) (default: 0)");
92
93
94 /* various RNDIS OID defs */
95 #define OID_GEN_LINK_SPEED                      cpu_to_le32(0x00010107)
96 #define OID_GEN_RNDIS_CONFIG_PARAMETER          cpu_to_le32(0x0001021b)
97
98 #define OID_GEN_XMIT_OK                         cpu_to_le32(0x00020101)
99 #define OID_GEN_RCV_OK                          cpu_to_le32(0x00020102)
100 #define OID_GEN_XMIT_ERROR                      cpu_to_le32(0x00020103)
101 #define OID_GEN_RCV_ERROR                       cpu_to_le32(0x00020104)
102 #define OID_GEN_RCV_NO_BUFFER                   cpu_to_le32(0x00020105)
103
104 #define OID_802_3_CURRENT_ADDRESS               cpu_to_le32(0x01010102)
105 #define OID_802_3_MULTICAST_LIST                cpu_to_le32(0x01010103)
106 #define OID_802_3_MAXIMUM_LIST_SIZE             cpu_to_le32(0x01010104)
107
108 #define OID_802_11_BSSID                        cpu_to_le32(0x0d010101)
109 #define OID_802_11_SSID                         cpu_to_le32(0x0d010102)
110 #define OID_802_11_INFRASTRUCTURE_MODE          cpu_to_le32(0x0d010108)
111 #define OID_802_11_ADD_WEP                      cpu_to_le32(0x0d010113)
112 #define OID_802_11_REMOVE_WEP                   cpu_to_le32(0x0d010114)
113 #define OID_802_11_DISASSOCIATE                 cpu_to_le32(0x0d010115)
114 #define OID_802_11_AUTHENTICATION_MODE          cpu_to_le32(0x0d010118)
115 #define OID_802_11_PRIVACY_FILTER               cpu_to_le32(0x0d010119)
116 #define OID_802_11_BSSID_LIST_SCAN              cpu_to_le32(0x0d01011a)
117 #define OID_802_11_ENCRYPTION_STATUS            cpu_to_le32(0x0d01011b)
118 #define OID_802_11_ADD_KEY                      cpu_to_le32(0x0d01011d)
119 #define OID_802_11_REMOVE_KEY                   cpu_to_le32(0x0d01011e)
120 #define OID_802_11_ASSOCIATION_INFORMATION      cpu_to_le32(0x0d01011f)
121 #define OID_802_11_CAPABILITY                   cpu_to_le32(0x0d010122)
122 #define OID_802_11_PMKID                        cpu_to_le32(0x0d010123)
123 #define OID_802_11_NETWORK_TYPES_SUPPORTED      cpu_to_le32(0x0d010203)
124 #define OID_802_11_NETWORK_TYPE_IN_USE          cpu_to_le32(0x0d010204)
125 #define OID_802_11_TX_POWER_LEVEL               cpu_to_le32(0x0d010205)
126 #define OID_802_11_RSSI                         cpu_to_le32(0x0d010206)
127 #define OID_802_11_RSSI_TRIGGER                 cpu_to_le32(0x0d010207)
128 #define OID_802_11_FRAGMENTATION_THRESHOLD      cpu_to_le32(0x0d010209)
129 #define OID_802_11_RTS_THRESHOLD                cpu_to_le32(0x0d01020a)
130 #define OID_802_11_SUPPORTED_RATES              cpu_to_le32(0x0d01020e)
131 #define OID_802_11_CONFIGURATION                cpu_to_le32(0x0d010211)
132 #define OID_802_11_BSSID_LIST                   cpu_to_le32(0x0d010217)
133
134
135 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
136 #define WL_NOISE        -96     /* typical noise level in dBm */
137 #define WL_SIGMAX       -32     /* typical maximum signal level in dBm */
138
139
140 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
141  * based on wireless rndis) has default txpower of 13dBm.
142  * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
143  *  100% : 20 mW ~ 13dBm
144  *   75% : 15 mW ~ 12dBm
145  *   50% : 10 mW ~ 10dBm
146  *   25% :  5 mW ~  7dBm
147  */
148 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
149 #define BCM4320_DEFAULT_TXPOWER_DBM_75  12
150 #define BCM4320_DEFAULT_TXPOWER_DBM_50  10
151 #define BCM4320_DEFAULT_TXPOWER_DBM_25  7
152
153
154 /* codes for "status" field of completion messages */
155 #define RNDIS_STATUS_ADAPTER_NOT_READY          cpu_to_le32(0xc0010011)
156 #define RNDIS_STATUS_ADAPTER_NOT_OPEN           cpu_to_le32(0xc0010012)
157
158
159 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
160  * slightly modified for datatype endianess, etc
161  */
162 #define NDIS_802_11_LENGTH_SSID 32
163 #define NDIS_802_11_LENGTH_RATES 8
164 #define NDIS_802_11_LENGTH_RATES_EX 16
165
166 enum ndis_80211_net_type {
167         NDIS_80211_TYPE_FREQ_HOP,
168         NDIS_80211_TYPE_DIRECT_SEQ,
169         NDIS_80211_TYPE_OFDM_A,
170         NDIS_80211_TYPE_OFDM_G
171 };
172
173 enum ndis_80211_net_infra {
174         NDIS_80211_INFRA_ADHOC,
175         NDIS_80211_INFRA_INFRA,
176         NDIS_80211_INFRA_AUTO_UNKNOWN
177 };
178
179 enum ndis_80211_auth_mode {
180         NDIS_80211_AUTH_OPEN,
181         NDIS_80211_AUTH_SHARED,
182         NDIS_80211_AUTH_AUTO_SWITCH,
183         NDIS_80211_AUTH_WPA,
184         NDIS_80211_AUTH_WPA_PSK,
185         NDIS_80211_AUTH_WPA_NONE,
186         NDIS_80211_AUTH_WPA2,
187         NDIS_80211_AUTH_WPA2_PSK
188 };
189
190 enum ndis_80211_encr_status {
191         NDIS_80211_ENCR_WEP_ENABLED,
192         NDIS_80211_ENCR_DISABLED,
193         NDIS_80211_ENCR_WEP_KEY_ABSENT,
194         NDIS_80211_ENCR_NOT_SUPPORTED,
195         NDIS_80211_ENCR_TKIP_ENABLED,
196         NDIS_80211_ENCR_TKIP_KEY_ABSENT,
197         NDIS_80211_ENCR_CCMP_ENABLED,
198         NDIS_80211_ENCR_CCMP_KEY_ABSENT
199 };
200
201 enum ndis_80211_priv_filter {
202         NDIS_80211_PRIV_ACCEPT_ALL,
203         NDIS_80211_PRIV_8021X_WEP
204 };
205
206 enum ndis_80211_status_type {
207         NDIS_80211_STATUSTYPE_AUTHENTICATION,
208         NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
209         NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
210         NDIS_80211_STATUSTYPE_RADIOSTATE,
211 };
212
213 enum ndis_80211_media_stream_mode {
214         NDIS_80211_MEDIA_STREAM_OFF,
215         NDIS_80211_MEDIA_STREAM_ON
216 };
217
218 enum ndis_80211_radio_status {
219         NDIS_80211_RADIO_STATUS_ON,
220         NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
221         NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
222 };
223
224 enum ndis_80211_addkey_bits {
225         NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
226         NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
227         NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
228         NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
229 };
230
231 enum ndis_80211_addwep_bits {
232         NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
233         NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
234 };
235
236 struct ndis_80211_auth_request {
237         __le32 length;
238         u8 bssid[6];
239         u8 padding[2];
240         __le32 flags;
241 } __packed;
242
243 struct ndis_80211_pmkid_candidate {
244         u8 bssid[6];
245         u8 padding[2];
246         __le32 flags;
247 } __packed;
248
249 struct ndis_80211_pmkid_cand_list {
250         __le32 version;
251         __le32 num_candidates;
252         struct ndis_80211_pmkid_candidate candidate_list[0];
253 } __packed;
254
255 struct ndis_80211_status_indication {
256         __le32 status_type;
257         union {
258                 __le32                                  media_stream_mode;
259                 __le32                                  radio_status;
260                 struct ndis_80211_auth_request          auth_request[0];
261                 struct ndis_80211_pmkid_cand_list       cand_list;
262         } u;
263 } __packed;
264
265 struct ndis_80211_ssid {
266         __le32 length;
267         u8 essid[NDIS_802_11_LENGTH_SSID];
268 } __packed;
269
270 struct ndis_80211_conf_freq_hop {
271         __le32 length;
272         __le32 hop_pattern;
273         __le32 hop_set;
274         __le32 dwell_time;
275 } __packed;
276
277 struct ndis_80211_conf {
278         __le32 length;
279         __le32 beacon_period;
280         __le32 atim_window;
281         __le32 ds_config;
282         struct ndis_80211_conf_freq_hop fh_config;
283 } __packed;
284
285 struct ndis_80211_bssid_ex {
286         __le32 length;
287         u8 mac[6];
288         u8 padding[2];
289         struct ndis_80211_ssid ssid;
290         __le32 privacy;
291         __le32 rssi;
292         __le32 net_type;
293         struct ndis_80211_conf config;
294         __le32 net_infra;
295         u8 rates[NDIS_802_11_LENGTH_RATES_EX];
296         __le32 ie_length;
297         u8 ies[0];
298 } __packed;
299
300 struct ndis_80211_bssid_list_ex {
301         __le32 num_items;
302         struct ndis_80211_bssid_ex bssid[0];
303 } __packed;
304
305 struct ndis_80211_fixed_ies {
306         u8 timestamp[8];
307         __le16 beacon_interval;
308         __le16 capabilities;
309 } __packed;
310
311 struct ndis_80211_wep_key {
312         __le32 size;
313         __le32 index;
314         __le32 length;
315         u8 material[32];
316 } __packed;
317
318 struct ndis_80211_key {
319         __le32 size;
320         __le32 index;
321         __le32 length;
322         u8 bssid[6];
323         u8 padding[6];
324         u8 rsc[8];
325         u8 material[32];
326 } __packed;
327
328 struct ndis_80211_remove_key {
329         __le32 size;
330         __le32 index;
331         u8 bssid[6];
332         u8 padding[2];
333 } __packed;
334
335 struct ndis_config_param {
336         __le32 name_offs;
337         __le32 name_length;
338         __le32 type;
339         __le32 value_offs;
340         __le32 value_length;
341 } __packed;
342
343 struct ndis_80211_assoc_info {
344         __le32 length;
345         __le16 req_ies;
346         struct req_ie {
347                 __le16 capa;
348                 __le16 listen_interval;
349                 u8 cur_ap_address[6];
350         } req_ie;
351         __le32 req_ie_length;
352         __le32 offset_req_ies;
353         __le16 resp_ies;
354         struct resp_ie {
355                 __le16 capa;
356                 __le16 status_code;
357                 __le16 assoc_id;
358         } resp_ie;
359         __le32 resp_ie_length;
360         __le32 offset_resp_ies;
361 } __packed;
362
363 struct ndis_80211_auth_encr_pair {
364         __le32 auth_mode;
365         __le32 encr_mode;
366 } __packed;
367
368 struct ndis_80211_capability {
369         __le32 length;
370         __le32 version;
371         __le32 num_pmkids;
372         __le32 num_auth_encr_pair;
373         struct ndis_80211_auth_encr_pair auth_encr_pair[0];
374 } __packed;
375
376 struct ndis_80211_bssid_info {
377         u8 bssid[6];
378         u8 pmkid[16];
379 };
380
381 struct ndis_80211_pmkid {
382         __le32 length;
383         __le32 bssid_info_count;
384         struct ndis_80211_bssid_info bssid_info[0];
385 };
386
387 /*
388  *  private data
389  */
390 #define NET_TYPE_11FB   0
391
392 #define CAP_MODE_80211A         1
393 #define CAP_MODE_80211B         2
394 #define CAP_MODE_80211G         4
395 #define CAP_MODE_MASK           7
396
397 #define WORK_LINK_UP            (1<<0)
398 #define WORK_LINK_DOWN          (1<<1)
399 #define WORK_SET_MULTICAST_LIST (1<<2)
400
401 #define RNDIS_WLAN_ALG_NONE     0
402 #define RNDIS_WLAN_ALG_WEP      (1<<0)
403 #define RNDIS_WLAN_ALG_TKIP     (1<<1)
404 #define RNDIS_WLAN_ALG_CCMP     (1<<2)
405
406 #define RNDIS_WLAN_KEY_MGMT_NONE        0
407 #define RNDIS_WLAN_KEY_MGMT_802_1X      (1<<0)
408 #define RNDIS_WLAN_KEY_MGMT_PSK         (1<<1)
409
410 #define COMMAND_BUFFER_SIZE     (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
411
412 static const struct ieee80211_channel rndis_channels[] = {
413         { .center_freq = 2412 },
414         { .center_freq = 2417 },
415         { .center_freq = 2422 },
416         { .center_freq = 2427 },
417         { .center_freq = 2432 },
418         { .center_freq = 2437 },
419         { .center_freq = 2442 },
420         { .center_freq = 2447 },
421         { .center_freq = 2452 },
422         { .center_freq = 2457 },
423         { .center_freq = 2462 },
424         { .center_freq = 2467 },
425         { .center_freq = 2472 },
426         { .center_freq = 2484 },
427 };
428
429 static const struct ieee80211_rate rndis_rates[] = {
430         { .bitrate = 10 },
431         { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
432         { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
433         { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
434         { .bitrate = 60 },
435         { .bitrate = 90 },
436         { .bitrate = 120 },
437         { .bitrate = 180 },
438         { .bitrate = 240 },
439         { .bitrate = 360 },
440         { .bitrate = 480 },
441         { .bitrate = 540 }
442 };
443
444 static const u32 rndis_cipher_suites[] = {
445         WLAN_CIPHER_SUITE_WEP40,
446         WLAN_CIPHER_SUITE_WEP104,
447         WLAN_CIPHER_SUITE_TKIP,
448         WLAN_CIPHER_SUITE_CCMP,
449 };
450
451 struct rndis_wlan_encr_key {
452         int len;
453         u32 cipher;
454         u8 material[32];
455         u8 bssid[ETH_ALEN];
456         bool pairwise;
457         bool tx_key;
458 };
459
460 /* RNDIS device private data */
461 struct rndis_wlan_private {
462         struct usbnet *usbdev;
463
464         struct wireless_dev wdev;
465
466         struct cfg80211_scan_request *scan_request;
467
468         struct workqueue_struct *workqueue;
469         struct delayed_work dev_poller_work;
470         struct delayed_work scan_work;
471         struct work_struct work;
472         struct mutex command_lock;
473         unsigned long work_pending;
474         int last_qual;
475
476         struct ieee80211_supported_band band;
477         struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
478         struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
479         u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
480
481         int caps;
482         int multicast_size;
483
484         /* module parameters */
485         char param_country[4];
486         int  param_frameburst;
487         int  param_afterburner;
488         int  param_power_save;
489         int  param_power_output;
490         int  param_roamtrigger;
491         int  param_roamdelta;
492         u32  param_workaround_interval;
493
494         /* hardware state */
495         bool radio_on;
496         int infra_mode;
497         bool connected;
498         u8 bssid[ETH_ALEN];
499         struct ndis_80211_ssid essid;
500         __le32 current_command_oid;
501
502         /* encryption stuff */
503         int  encr_tx_key_index;
504         struct rndis_wlan_encr_key encr_keys[4];
505         int  wpa_version;
506
507         u8 command_buffer[COMMAND_BUFFER_SIZE];
508 };
509
510 /*
511  * cfg80211 ops
512  */
513 static int rndis_change_virtual_intf(struct wiphy *wiphy,
514                                         struct net_device *dev,
515                                         enum nl80211_iftype type, u32 *flags,
516                                         struct vif_params *params);
517
518 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
519                         struct cfg80211_scan_request *request);
520
521 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
522
523 static int rndis_set_tx_power(struct wiphy *wiphy, 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 = NULL;
2499         u8 bssid[ETH_ALEN];
2500         int resp_ie_len, req_ie_len;
2501         u8 *req_ie, *resp_ie;
2502         int ret, offset;
2503         bool roamed = false;
2504
2505         if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2506                 /* received media connect indication while connected, either
2507                  * device reassociated with same AP or roamed to new. */
2508                 roamed = true;
2509         }
2510
2511         req_ie_len = 0;
2512         resp_ie_len = 0;
2513         req_ie = NULL;
2514         resp_ie = NULL;
2515
2516         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2517                 info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2518                 if (!info) {
2519                         /* No memory? Try resume work later */
2520                         set_bit(WORK_LINK_UP, &priv->work_pending);
2521                         queue_work(priv->workqueue, &priv->work);
2522                         return;
2523                 }
2524
2525                 /* Get association info IEs from device. */
2526                 ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2527                 if (!ret) {
2528                         req_ie_len = le32_to_cpu(info->req_ie_length);
2529                         if (req_ie_len > 0) {
2530                                 offset = le32_to_cpu(info->offset_req_ies);
2531
2532                                 if (offset > CONTROL_BUFFER_SIZE)
2533                                         offset = CONTROL_BUFFER_SIZE;
2534
2535                                 req_ie = (u8 *)info + offset;
2536
2537                                 if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2538                                         req_ie_len =
2539                                                 CONTROL_BUFFER_SIZE - offset;
2540                         }
2541
2542                         resp_ie_len = le32_to_cpu(info->resp_ie_length);
2543                         if (resp_ie_len > 0) {
2544                                 offset = le32_to_cpu(info->offset_resp_ies);
2545
2546                                 if (offset > CONTROL_BUFFER_SIZE)
2547                                         offset = CONTROL_BUFFER_SIZE;
2548
2549                                 resp_ie = (u8 *)info + offset;
2550
2551                                 if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2552                                         resp_ie_len =
2553                                                 CONTROL_BUFFER_SIZE - offset;
2554                         }
2555                 }
2556         } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2557                 return;
2558
2559         ret = get_bssid(usbdev, bssid);
2560         if (ret < 0)
2561                 memset(bssid, 0, sizeof(bssid));
2562
2563         netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2564                    bssid, roamed ? " roamed" : "");
2565
2566         /* Internal bss list in device always contains at least the currently
2567          * connected bss and we can get it to cfg80211 with
2568          * rndis_check_bssid_list().
2569          * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2570          * spec.
2571          */
2572         rndis_check_bssid_list(usbdev);
2573
2574         if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2575                 if (!roamed)
2576                         cfg80211_connect_result(usbdev->net, bssid, req_ie,
2577                                                 req_ie_len, resp_ie,
2578                                                 resp_ie_len, 0, GFP_KERNEL);
2579                 else
2580                         cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2581                                         resp_ie, resp_ie_len, GFP_KERNEL);
2582         } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2583                 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2584
2585         if (info != NULL)
2586                 kfree(info);
2587
2588         priv->connected = true;
2589         memcpy(priv->bssid, bssid, ETH_ALEN);
2590
2591         usbnet_resume_rx(usbdev);
2592         netif_carrier_on(usbdev->net);
2593 }
2594
2595 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2596 {
2597         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2598
2599         if (priv->connected) {
2600                 priv->connected = false;
2601                 memset(priv->bssid, 0, ETH_ALEN);
2602
2603                 deauthenticate(usbdev);
2604
2605                 cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
2606         }
2607
2608         netif_carrier_off(usbdev->net);
2609 }
2610
2611 static void rndis_wlan_worker(struct work_struct *work)
2612 {
2613         struct rndis_wlan_private *priv =
2614                 container_of(work, struct rndis_wlan_private, work);
2615         struct usbnet *usbdev = priv->usbdev;
2616
2617         if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2618                 rndis_wlan_do_link_up_work(usbdev);
2619
2620         if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2621                 rndis_wlan_do_link_down_work(usbdev);
2622
2623         if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2624                 set_multicast_list(usbdev);
2625 }
2626
2627 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2628 {
2629         struct usbnet *usbdev = netdev_priv(dev);
2630         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2631
2632         if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2633                 return;
2634
2635         set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2636         queue_work(priv->workqueue, &priv->work);
2637 }
2638
2639 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2640                                 struct ndis_80211_status_indication *indication,
2641                                 int len)
2642 {
2643         u8 *buf;
2644         const char *type;
2645         int flags, buflen, key_id;
2646         bool pairwise_error, group_error;
2647         struct ndis_80211_auth_request *auth_req;
2648         enum nl80211_key_type key_type;
2649
2650         /* must have at least one array entry */
2651         if (len < offsetof(struct ndis_80211_status_indication, u) +
2652                                 sizeof(struct ndis_80211_auth_request)) {
2653                 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2654                             len);
2655                 return;
2656         }
2657
2658         buf = (void *)&indication->u.auth_request[0];
2659         buflen = len - offsetof(struct ndis_80211_status_indication, u);
2660
2661         while (buflen >= sizeof(*auth_req)) {
2662                 auth_req = (void *)buf;
2663                 type = "unknown";
2664                 flags = le32_to_cpu(auth_req->flags);
2665                 pairwise_error = false;
2666                 group_error = false;
2667
2668                 if (flags & 0x1)
2669                         type = "reauth request";
2670                 if (flags & 0x2)
2671                         type = "key update request";
2672                 if (flags & 0x6) {
2673                         pairwise_error = true;
2674                         type = "pairwise_error";
2675                 }
2676                 if (flags & 0xe) {
2677                         group_error = true;
2678                         type = "group_error";
2679                 }
2680
2681                 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2682                             type, le32_to_cpu(auth_req->flags));
2683
2684                 if (pairwise_error) {
2685                         key_type = NL80211_KEYTYPE_PAIRWISE;
2686                         key_id = -1;
2687
2688                         cfg80211_michael_mic_failure(usbdev->net,
2689                                                         auth_req->bssid,
2690                                                         key_type, key_id, NULL,
2691                                                         GFP_KERNEL);
2692                 }
2693
2694                 if (group_error) {
2695                         key_type = NL80211_KEYTYPE_GROUP;
2696                         key_id = -1;
2697
2698                         cfg80211_michael_mic_failure(usbdev->net,
2699                                                         auth_req->bssid,
2700                                                         key_type, key_id, NULL,
2701                                                         GFP_KERNEL);
2702                 }
2703
2704                 buflen -= le32_to_cpu(auth_req->length);
2705                 buf += le32_to_cpu(auth_req->length);
2706         }
2707 }
2708
2709 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2710                                 struct ndis_80211_status_indication *indication,
2711                                 int len)
2712 {
2713         struct ndis_80211_pmkid_cand_list *cand_list;
2714         int list_len, expected_len, i;
2715
2716         if (len < offsetof(struct ndis_80211_status_indication, u) +
2717                                 sizeof(struct ndis_80211_pmkid_cand_list)) {
2718                 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2719                             len);
2720                 return;
2721         }
2722
2723         list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2724                         sizeof(struct ndis_80211_pmkid_candidate);
2725         expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2726                         offsetof(struct ndis_80211_status_indication, u);
2727
2728         if (len < expected_len) {
2729                 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2730                             len, expected_len);
2731                 return;
2732         }
2733
2734         cand_list = &indication->u.cand_list;
2735
2736         netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2737                     le32_to_cpu(cand_list->version),
2738                     le32_to_cpu(cand_list->num_candidates));
2739
2740         if (le32_to_cpu(cand_list->version) != 1)
2741                 return;
2742
2743         for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2744                 struct ndis_80211_pmkid_candidate *cand =
2745                                                 &cand_list->candidate_list[i];
2746
2747                 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2748                            i, le32_to_cpu(cand->flags), cand->bssid);
2749
2750 #if 0
2751                 struct iw_pmkid_cand pcand;
2752                 union iwreq_data wrqu;
2753
2754                 memset(&pcand, 0, sizeof(pcand));
2755                 if (le32_to_cpu(cand->flags) & 0x01)
2756                         pcand.flags |= IW_PMKID_CAND_PREAUTH;
2757                 pcand.index = i;
2758                 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2759
2760                 memset(&wrqu, 0, sizeof(wrqu));
2761                 wrqu.data.length = sizeof(pcand);
2762                 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2763                                                                 (u8 *)&pcand);
2764 #endif
2765         }
2766 }
2767
2768 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2769                         struct rndis_indicate *msg, int buflen)
2770 {
2771         struct ndis_80211_status_indication *indication;
2772         int len, offset;
2773
2774         offset = offsetof(struct rndis_indicate, status) +
2775                         le32_to_cpu(msg->offset);
2776         len = le32_to_cpu(msg->length);
2777
2778         if (len < 8) {
2779                 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2780                             len);
2781                 return;
2782         }
2783
2784         if (offset + len > buflen) {
2785                 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2786                             offset + len, buflen);
2787                 return;
2788         }
2789
2790         indication = (void *)((u8 *)msg + offset);
2791
2792         switch (le32_to_cpu(indication->status_type)) {
2793         case NDIS_80211_STATUSTYPE_RADIOSTATE:
2794                 netdev_info(usbdev->net, "radio state indication: %i\n",
2795                             le32_to_cpu(indication->u.radio_status));
2796                 return;
2797
2798         case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2799                 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2800                             le32_to_cpu(indication->u.media_stream_mode));
2801                 return;
2802
2803         case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2804                 rndis_wlan_auth_indication(usbdev, indication, len);
2805                 return;
2806
2807         case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2808                 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2809                 return;
2810
2811         default:
2812                 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2813                             le32_to_cpu(indication->status_type));
2814         }
2815 }
2816
2817 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2818 {
2819         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2820         struct rndis_indicate *msg = ind;
2821
2822         switch (msg->status) {
2823         case RNDIS_STATUS_MEDIA_CONNECT:
2824                 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2825                         /* OID_802_11_ADD_KEY causes sometimes extra
2826                          * "media connect" indications which confuses driver
2827                          * and userspace to think that device is
2828                          * roaming/reassociating when it isn't.
2829                          */
2830                         netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
2831                         return;
2832                 }
2833
2834                 usbnet_pause_rx(usbdev);
2835
2836                 netdev_info(usbdev->net, "media connect\n");
2837
2838                 /* queue work to avoid recursive calls into rndis_command */
2839                 set_bit(WORK_LINK_UP, &priv->work_pending);
2840                 queue_work(priv->workqueue, &priv->work);
2841                 break;
2842
2843         case RNDIS_STATUS_MEDIA_DISCONNECT:
2844                 netdev_info(usbdev->net, "media disconnect\n");
2845
2846                 /* queue work to avoid recursive calls into rndis_command */
2847                 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2848                 queue_work(priv->workqueue, &priv->work);
2849                 break;
2850
2851         case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2852                 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2853                 break;
2854
2855         default:
2856                 netdev_info(usbdev->net, "indication: 0x%08x\n",
2857                             le32_to_cpu(msg->status));
2858                 break;
2859         }
2860 }
2861
2862 static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
2863 {
2864         struct {
2865                 __le32  num_items;
2866                 __le32  items[8];
2867         } networks_supported;
2868         struct ndis_80211_capability *caps;
2869         u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
2870         int len, retval, i, n;
2871         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2872
2873         /* determine supported modes */
2874         len = sizeof(networks_supported);
2875         retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2876                                                 &networks_supported, &len);
2877         if (retval >= 0) {
2878                 n = le32_to_cpu(networks_supported.num_items);
2879                 if (n > 8)
2880                         n = 8;
2881                 for (i = 0; i < n; i++) {
2882                         switch (le32_to_cpu(networks_supported.items[i])) {
2883                         case NDIS_80211_TYPE_FREQ_HOP:
2884                         case NDIS_80211_TYPE_DIRECT_SEQ:
2885                                 priv->caps |= CAP_MODE_80211B;
2886                                 break;
2887                         case NDIS_80211_TYPE_OFDM_A:
2888                                 priv->caps |= CAP_MODE_80211A;
2889                                 break;
2890                         case NDIS_80211_TYPE_OFDM_G:
2891                                 priv->caps |= CAP_MODE_80211G;
2892                                 break;
2893                         }
2894                 }
2895         }
2896
2897         /* get device 802.11 capabilities, number of PMKIDs */
2898         caps = (struct ndis_80211_capability *)caps_buf;
2899         len = sizeof(caps_buf);
2900         retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
2901         if (retval >= 0) {
2902                 netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
2903                                 "ver %d, pmkids %d, auth-encr-pairs %d\n",
2904                                 le32_to_cpu(caps->length),
2905                                 le32_to_cpu(caps->version),
2906                                 le32_to_cpu(caps->num_pmkids),
2907                                 le32_to_cpu(caps->num_auth_encr_pair));
2908                 wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
2909         } else
2910                 wiphy->max_num_pmkids = 0;
2911
2912         return retval;
2913 }
2914
2915 #define DEVICE_POLLER_JIFFIES (HZ)
2916 static void rndis_device_poller(struct work_struct *work)
2917 {
2918         struct rndis_wlan_private *priv =
2919                 container_of(work, struct rndis_wlan_private,
2920                                                         dev_poller_work.work);
2921         struct usbnet *usbdev = priv->usbdev;
2922         __le32 rssi, tmp;
2923         int len, ret, j;
2924         int update_jiffies = DEVICE_POLLER_JIFFIES;
2925         void *buf;
2926
2927         /* Only check/do workaround when connected. Calling is_associated()
2928          * also polls device with rndis_command() and catches for media link
2929          * indications.
2930          */
2931         if (!is_associated(usbdev))
2932                 goto end;
2933
2934         len = sizeof(rssi);
2935         ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2936         if (ret == 0)
2937                 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
2938
2939         netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
2940                    ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
2941
2942         /* Workaround transfer stalls on poor quality links.
2943          * TODO: find right way to fix these stalls (as stalls do not happen
2944          * with ndiswrapper/windows driver). */
2945         if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
2946                 /* Decrease stats worker interval to catch stalls.
2947                  * faster. Faster than 400-500ms causes packet loss,
2948                  * Slower doesn't catch stalls fast enough.
2949                  */
2950                 j = msecs_to_jiffies(priv->param_workaround_interval);
2951                 if (j > DEVICE_POLLER_JIFFIES)
2952                         j = DEVICE_POLLER_JIFFIES;
2953                 else if (j <= 0)
2954                         j = 1;
2955                 update_jiffies = j;
2956
2957                 /* Send scan OID. Use of both OIDs is required to get device
2958                  * working.
2959                  */
2960                 tmp = cpu_to_le32(1);
2961                 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2962                                                                 sizeof(tmp));
2963
2964                 len = CONTROL_BUFFER_SIZE;
2965                 buf = kmalloc(len, GFP_KERNEL);
2966                 if (!buf)
2967                         goto end;
2968
2969                 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2970                 kfree(buf);
2971         }
2972
2973 end:
2974         if (update_jiffies >= HZ)
2975                 update_jiffies = round_jiffies_relative(update_jiffies);
2976         else {
2977                 j = round_jiffies_relative(update_jiffies);
2978                 if (abs(j - update_jiffies) <= 10)
2979                         update_jiffies = j;
2980         }
2981
2982         queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2983                                                                 update_jiffies);
2984 }
2985
2986 /*
2987  * driver/device initialization
2988  */
2989 static void rndis_copy_module_params(struct usbnet *usbdev)
2990 {
2991         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2992
2993         priv->param_country[0] = modparam_country[0];
2994         priv->param_country[1] = modparam_country[1];
2995         priv->param_country[2] = 0;
2996         priv->param_frameburst   = modparam_frameburst;
2997         priv->param_afterburner  = modparam_afterburner;
2998         priv->param_power_save   = modparam_power_save;
2999         priv->param_power_output = modparam_power_output;
3000         priv->param_roamtrigger  = modparam_roamtrigger;
3001         priv->param_roamdelta    = modparam_roamdelta;
3002
3003         priv->param_country[0] = toupper(priv->param_country[0]);
3004         priv->param_country[1] = toupper(priv->param_country[1]);
3005         /* doesn't support EU as country code, use FI instead */
3006         if (!strcmp(priv->param_country, "EU"))
3007                 strcpy(priv->param_country, "FI");
3008
3009         if (priv->param_power_save < 0)
3010                 priv->param_power_save = 0;
3011         else if (priv->param_power_save > 2)
3012                 priv->param_power_save = 2;
3013
3014         if (priv->param_power_output < 0)
3015                 priv->param_power_output = 0;
3016         else if (priv->param_power_output > 3)
3017                 priv->param_power_output = 3;
3018
3019         if (priv->param_roamtrigger < -80)
3020                 priv->param_roamtrigger = -80;
3021         else if (priv->param_roamtrigger > -60)
3022                 priv->param_roamtrigger = -60;
3023
3024         if (priv->param_roamdelta < 0)
3025                 priv->param_roamdelta = 0;
3026         else if (priv->param_roamdelta > 2)
3027                 priv->param_roamdelta = 2;
3028
3029         if (modparam_workaround_interval < 0)
3030                 priv->param_workaround_interval = 500;
3031         else
3032                 priv->param_workaround_interval = modparam_workaround_interval;
3033 }
3034
3035 static int bcm4320a_early_init(struct usbnet *usbdev)
3036 {
3037         /* copy module parameters for bcm4320a so that iwconfig reports txpower
3038          * and workaround parameter is copied to private structure correctly.
3039          */
3040         rndis_copy_module_params(usbdev);
3041
3042         /* bcm4320a doesn't handle configuration parameters well. Try
3043          * set any and you get partially zeroed mac and broken device.
3044          */
3045
3046         return 0;
3047 }
3048
3049 static int bcm4320b_early_init(struct usbnet *usbdev)
3050 {
3051         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3052         char buf[8];
3053
3054         rndis_copy_module_params(usbdev);
3055
3056         /* Early initialization settings, setting these won't have effect
3057          * if called after generic_rndis_bind().
3058          */
3059
3060         rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3061         rndis_set_config_parameter_str(usbdev, "FrameBursting",
3062                                         priv->param_frameburst ? "1" : "0");
3063         rndis_set_config_parameter_str(usbdev, "Afterburner",
3064                                         priv->param_afterburner ? "1" : "0");
3065         sprintf(buf, "%d", priv->param_power_save);
3066         rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3067         sprintf(buf, "%d", priv->param_power_output);
3068         rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3069         sprintf(buf, "%d", priv->param_roamtrigger);
3070         rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3071         sprintf(buf, "%d", priv->param_roamdelta);
3072         rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3073
3074         return 0;
3075 }
3076
3077 /* same as rndis_netdev_ops but with local multicast handler */
3078 static const struct net_device_ops rndis_wlan_netdev_ops = {
3079         .ndo_open               = usbnet_open,
3080         .ndo_stop               = usbnet_stop,
3081         .ndo_start_xmit         = usbnet_start_xmit,
3082         .ndo_tx_timeout         = usbnet_tx_timeout,
3083         .ndo_set_mac_address    = eth_mac_addr,
3084         .ndo_validate_addr      = eth_validate_addr,
3085         .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3086 };
3087
3088 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3089 {
3090         struct wiphy *wiphy;
3091         struct rndis_wlan_private *priv;
3092         int retval, len;
3093         __le32 tmp;
3094
3095         /* allocate wiphy and rndis private data
3096          * NOTE: We only support a single virtual interface, so wiphy
3097          * and wireless_dev are somewhat synonymous for this device.
3098          */
3099         wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3100         if (!wiphy)
3101                 return -ENOMEM;
3102
3103         priv = wiphy_priv(wiphy);
3104         usbdev->net->ieee80211_ptr = &priv->wdev;
3105         priv->wdev.wiphy = wiphy;
3106         priv->wdev.iftype = NL80211_IFTYPE_STATION;
3107
3108         /* These have to be initialized before calling generic_rndis_bind().
3109          * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3110          */
3111         usbdev->driver_priv = priv;
3112         priv->usbdev = usbdev;
3113
3114         mutex_init(&priv->command_lock);
3115
3116         /* because rndis_command() sleeps we need to use workqueue */
3117         priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3118         INIT_WORK(&priv->work, rndis_wlan_worker);
3119         INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3120         INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3121
3122         /* try bind rndis_host */
3123         retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3124         if (retval < 0)
3125                 goto fail;
3126
3127         /* generic_rndis_bind set packet filter to multicast_all+
3128          * promisc mode which doesn't work well for our devices (device
3129          * picks up rssi to closest station instead of to access point).
3130          *
3131          * rndis_host wants to avoid all OID as much as possible
3132          * so do promisc/multicast handling in rndis_wlan.
3133          */
3134         usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3135
3136         tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3137         retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3138                                                                 sizeof(tmp));
3139
3140         len = sizeof(tmp);
3141         retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3142                                                                 &len);
3143         priv->multicast_size = le32_to_cpu(tmp);
3144         if (retval < 0 || priv->multicast_size < 0)
3145                 priv->multicast_size = 0;
3146         if (priv->multicast_size > 0)
3147                 usbdev->net->flags |= IFF_MULTICAST;
3148         else
3149                 usbdev->net->flags &= ~IFF_MULTICAST;
3150
3151         /* fill-out wiphy structure and register w/ cfg80211 */
3152         memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3153         wiphy->privid = rndis_wiphy_privid;
3154         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3155                                         | BIT(NL80211_IFTYPE_ADHOC);
3156         wiphy->max_scan_ssids = 1;
3157
3158         /* TODO: fill-out band/encr information based on priv->caps */
3159         rndis_wlan_get_caps(usbdev, wiphy);
3160
3161         memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3162         memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3163         priv->band.channels = priv->channels;
3164         priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3165         priv->band.bitrates = priv->rates;
3166         priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3167         wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3168         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3169
3170         memcpy(priv->cipher_suites, rndis_cipher_suites,
3171                                                 sizeof(rndis_cipher_suites));
3172         wiphy->cipher_suites = priv->cipher_suites;
3173         wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3174
3175         set_wiphy_dev(wiphy, &usbdev->udev->dev);
3176
3177         if (wiphy_register(wiphy)) {
3178                 retval = -ENODEV;
3179                 goto fail;
3180         }
3181
3182         set_default_iw_params(usbdev);
3183
3184         /* set default rts/frag */
3185         rndis_set_wiphy_params(wiphy,
3186                         WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3187
3188         /* turn radio on */
3189         priv->radio_on = true;
3190         disassociate(usbdev, true);
3191         netif_carrier_off(usbdev->net);
3192
3193         return 0;
3194
3195 fail:
3196         cancel_delayed_work_sync(&priv->dev_poller_work);
3197         cancel_delayed_work_sync(&priv->scan_work);
3198         cancel_work_sync(&priv->work);
3199         flush_workqueue(priv->workqueue);
3200         destroy_workqueue(priv->workqueue);
3201
3202         wiphy_free(wiphy);
3203         return retval;
3204 }
3205
3206 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3207 {
3208         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3209
3210         /* turn radio off */
3211         disassociate(usbdev, false);
3212
3213         cancel_delayed_work_sync(&priv->dev_poller_work);
3214         cancel_delayed_work_sync(&priv->scan_work);
3215         cancel_work_sync(&priv->work);
3216         flush_workqueue(priv->workqueue);
3217         destroy_workqueue(priv->workqueue);
3218
3219         rndis_unbind(usbdev, intf);
3220
3221         wiphy_unregister(priv->wdev.wiphy);
3222         wiphy_free(priv->wdev.wiphy);
3223 }
3224
3225 static int rndis_wlan_reset(struct usbnet *usbdev)
3226 {
3227         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3228         int retval;
3229
3230         netdev_dbg(usbdev->net, "%s()\n", __func__);
3231
3232         retval = rndis_reset(usbdev);
3233         if (retval)
3234                 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3235
3236         /* rndis_reset cleared multicast list, so restore here.
3237            (set_multicast_list() also turns on current packet filter) */
3238         set_multicast_list(usbdev);
3239
3240         queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3241                 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3242
3243         return deauthenticate(usbdev);
3244 }
3245
3246 static int rndis_wlan_stop(struct usbnet *usbdev)
3247 {
3248         struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3249         int retval;
3250         __le32 filter;
3251
3252         netdev_dbg(usbdev->net, "%s()\n", __func__);
3253
3254         retval = disassociate(usbdev, false);
3255
3256         priv->work_pending = 0;
3257         cancel_delayed_work_sync(&priv->dev_poller_work);
3258         cancel_delayed_work_sync(&priv->scan_work);
3259         cancel_work_sync(&priv->work);
3260         flush_workqueue(priv->workqueue);
3261
3262         if (priv->scan_request) {
3263                 cfg80211_scan_done(priv->scan_request, true);
3264                 priv->scan_request = NULL;
3265         }
3266
3267         /* Set current packet filter zero to block receiving data packets from
3268            device. */
3269         filter = 0;
3270         rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3271                                                                 sizeof(filter));
3272
3273         return retval;
3274 }
3275
3276 static const struct driver_info bcm4320b_info = {
3277         .description =  "Wireless RNDIS device, BCM4320b based",
3278         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3279                                 FLAG_AVOID_UNLINK_URBS,
3280         .bind =         rndis_wlan_bind,
3281         .unbind =       rndis_wlan_unbind,
3282         .status =       rndis_status,
3283         .rx_fixup =     rndis_rx_fixup,
3284         .tx_fixup =     rndis_tx_fixup,
3285         .reset =        rndis_wlan_reset,
3286         .stop =         rndis_wlan_stop,
3287         .early_init =   bcm4320b_early_init,
3288         .indication =   rndis_wlan_indication,
3289 };
3290
3291 static const struct driver_info bcm4320a_info = {
3292         .description =  "Wireless RNDIS device, BCM4320a based",
3293         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3294                                 FLAG_AVOID_UNLINK_URBS,
3295         .bind =         rndis_wlan_bind,
3296         .unbind =       rndis_wlan_unbind,
3297         .status =       rndis_status,
3298         .rx_fixup =     rndis_rx_fixup,
3299         .tx_fixup =     rndis_tx_fixup,
3300         .reset =        rndis_wlan_reset,
3301         .stop =         rndis_wlan_stop,
3302         .early_init =   bcm4320a_early_init,
3303         .indication =   rndis_wlan_indication,
3304 };
3305
3306 static const struct driver_info rndis_wlan_info = {
3307         .description =  "Wireless RNDIS device",
3308         .flags =        FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3309                                 FLAG_AVOID_UNLINK_URBS,
3310         .bind =         rndis_wlan_bind,
3311         .unbind =       rndis_wlan_unbind,
3312         .status =       rndis_status,
3313         .rx_fixup =     rndis_rx_fixup,
3314         .tx_fixup =     rndis_tx_fixup,
3315         .reset =        rndis_wlan_reset,
3316         .stop =         rndis_wlan_stop,
3317         .early_init =   bcm4320a_early_init,
3318         .indication =   rndis_wlan_indication,
3319 };
3320
3321 /*-------------------------------------------------------------------------*/
3322
3323 static const struct usb_device_id products [] = {
3324 #define RNDIS_MASTER_INTERFACE \
3325         .bInterfaceClass        = USB_CLASS_COMM, \
3326         .bInterfaceSubClass     = 2 /* ACM */, \
3327         .bInterfaceProtocol     = 0x0ff
3328
3329 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3330  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3331  */
3332 {
3333         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3334                           | USB_DEVICE_ID_MATCH_DEVICE,
3335         .idVendor               = 0x0411,
3336         .idProduct              = 0x00bc,       /* Buffalo WLI-U2-KG125S */
3337         RNDIS_MASTER_INTERFACE,
3338         .driver_info            = (unsigned long) &bcm4320b_info,
3339 }, {
3340         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3341                           | USB_DEVICE_ID_MATCH_DEVICE,
3342         .idVendor               = 0x0baf,
3343         .idProduct              = 0x011b,       /* U.S. Robotics USR5421 */
3344         RNDIS_MASTER_INTERFACE,
3345         .driver_info            = (unsigned long) &bcm4320b_info,
3346 }, {
3347         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3348                           | USB_DEVICE_ID_MATCH_DEVICE,
3349         .idVendor               = 0x050d,
3350         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3351         RNDIS_MASTER_INTERFACE,
3352         .driver_info            = (unsigned long) &bcm4320b_info,
3353 }, {
3354         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3355                           | USB_DEVICE_ID_MATCH_DEVICE,
3356         .idVendor               = 0x1799,       /* Belkin has two vendor ids */
3357         .idProduct              = 0x011b,       /* Belkin F5D7051 */
3358         RNDIS_MASTER_INTERFACE,
3359         .driver_info            = (unsigned long) &bcm4320b_info,
3360 }, {
3361         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3362                           | USB_DEVICE_ID_MATCH_DEVICE,
3363         .idVendor               = 0x13b1,
3364         .idProduct              = 0x0014,       /* Linksys WUSB54GSv2 */
3365         RNDIS_MASTER_INTERFACE,
3366         .driver_info            = (unsigned long) &bcm4320b_info,
3367 }, {
3368         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3369                           | USB_DEVICE_ID_MATCH_DEVICE,
3370         .idVendor               = 0x13b1,
3371         .idProduct              = 0x0026,       /* Linksys WUSB54GSC */
3372         RNDIS_MASTER_INTERFACE,
3373         .driver_info            = (unsigned long) &bcm4320b_info,
3374 }, {
3375         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3376                           | USB_DEVICE_ID_MATCH_DEVICE,
3377         .idVendor               = 0x0b05,
3378         .idProduct              = 0x1717,       /* Asus WL169gE */
3379         RNDIS_MASTER_INTERFACE,
3380         .driver_info            = (unsigned long) &bcm4320b_info,
3381 }, {
3382         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3383                           | USB_DEVICE_ID_MATCH_DEVICE,
3384         .idVendor               = 0x0a5c,
3385         .idProduct              = 0xd11b,       /* Eminent EM4045 */
3386         RNDIS_MASTER_INTERFACE,
3387         .driver_info            = (unsigned long) &bcm4320b_info,
3388 }, {
3389         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3390                           | USB_DEVICE_ID_MATCH_DEVICE,
3391         .idVendor               = 0x1690,
3392         .idProduct              = 0x0715,       /* BT Voyager 1055 */
3393         RNDIS_MASTER_INTERFACE,
3394         .driver_info            = (unsigned long) &bcm4320b_info,
3395 },
3396 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3397  * parameters available, hardware probably contain older firmware version with
3398  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3399  */
3400 {
3401         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3402                           | USB_DEVICE_ID_MATCH_DEVICE,
3403         .idVendor               = 0x13b1,
3404         .idProduct              = 0x000e,       /* Linksys WUSB54GSv1 */
3405         RNDIS_MASTER_INTERFACE,
3406         .driver_info            = (unsigned long) &bcm4320a_info,
3407 }, {
3408         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3409                           | USB_DEVICE_ID_MATCH_DEVICE,
3410         .idVendor               = 0x0baf,
3411         .idProduct              = 0x0111,       /* U.S. Robotics USR5420 */
3412         RNDIS_MASTER_INTERFACE,
3413         .driver_info            = (unsigned long) &bcm4320a_info,
3414 }, {
3415         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3416                           | USB_DEVICE_ID_MATCH_DEVICE,
3417         .idVendor               = 0x0411,
3418         .idProduct              = 0x004b,       /* BUFFALO WLI-USB-G54 */
3419         RNDIS_MASTER_INTERFACE,
3420         .driver_info            = (unsigned long) &bcm4320a_info,
3421 },
3422 /* Generic Wireless RNDIS devices that we don't have exact
3423  * idVendor/idProduct/chip yet.
3424  */
3425 {
3426         /* RNDIS is MSFT's un-official variant of CDC ACM */
3427         USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3428         .driver_info = (unsigned long) &rndis_wlan_info,
3429 }, {
3430         /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3431         USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3432         .driver_info = (unsigned long) &rndis_wlan_info,
3433 },
3434         { },            // END
3435 };
3436 MODULE_DEVICE_TABLE(usb, products);
3437
3438 static struct usb_driver rndis_wlan_driver = {
3439         .name =         "rndis_wlan",
3440         .id_table =     products,
3441         .probe =        usbnet_probe,
3442         .disconnect =   usbnet_disconnect,
3443         .suspend =      usbnet_suspend,
3444         .resume =       usbnet_resume,
3445 };
3446
3447 static int __init rndis_wlan_init(void)
3448 {
3449         return usb_register(&rndis_wlan_driver);
3450 }
3451 module_init(rndis_wlan_init);
3452
3453 static void __exit rndis_wlan_exit(void)
3454 {
3455         usb_deregister(&rndis_wlan_driver);
3456 }
3457 module_exit(rndis_wlan_exit);
3458
3459 MODULE_AUTHOR("Bjorge Dijkstra");
3460 MODULE_AUTHOR("Jussi Kivilinna");
3461 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3462 MODULE_LICENSE("GPL");
3463