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