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