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