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