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