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