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