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