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