2 * Driver for RNDIS based wireless USB devices.
4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5 * Copyright (C) 2008 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
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.
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.
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
21 * Portions of this file are based on NDISwrapper project,
22 * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
23 * http://ndiswrapper.sourceforge.net/
26 // #define DEBUG // error path messages, extra info
27 // #define VERBOSE // more; success messages
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/ethtool.h>
34 #include <linux/workqueue.h>
35 #include <linux/mutex.h>
36 #include <linux/mii.h>
37 #include <linux/usb.h>
38 #include <linux/usb/cdc.h>
39 #include <linux/wireless.h>
40 #include <linux/if_arp.h>
41 #include <linux/ctype.h>
42 #include <linux/spinlock.h>
43 #include <net/iw_handler.h>
44 #include <net/ieee80211.h>
45 #include <linux/usb/usbnet.h>
46 #include <linux/usb/rndis_host.h>
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");
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)");
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)");
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)");
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%)");
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)");
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)");
85 static int modparam_workaround_interval = 500;
86 module_param_named(workaround_interval, modparam_workaround_interval,
88 MODULE_PARM_DESC(workaround_interval,
89 "set stall workaround interval in msecs (default: 500)");
92 /* various RNDIS OID defs */
93 #define OID_GEN_LINK_SPEED ccpu2(0x00010107)
94 #define OID_GEN_RNDIS_CONFIG_PARAMETER ccpu2(0x0001021b)
96 #define OID_GEN_XMIT_OK ccpu2(0x00020101)
97 #define OID_GEN_RCV_OK ccpu2(0x00020102)
98 #define OID_GEN_XMIT_ERROR ccpu2(0x00020103)
99 #define OID_GEN_RCV_ERROR ccpu2(0x00020104)
100 #define OID_GEN_RCV_NO_BUFFER ccpu2(0x00020105)
102 #define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
103 #define OID_802_3_CURRENT_ADDRESS ccpu2(0x01010102)
104 #define OID_802_3_MULTICAST_LIST ccpu2(0x01010103)
105 #define OID_802_3_MAXIMUM_LIST_SIZE ccpu2(0x01010104)
107 #define OID_802_11_BSSID ccpu2(0x0d010101)
108 #define OID_802_11_SSID ccpu2(0x0d010102)
109 #define OID_802_11_INFRASTRUCTURE_MODE ccpu2(0x0d010108)
110 #define OID_802_11_ADD_WEP ccpu2(0x0d010113)
111 #define OID_802_11_REMOVE_WEP ccpu2(0x0d010114)
112 #define OID_802_11_DISASSOCIATE ccpu2(0x0d010115)
113 #define OID_802_11_AUTHENTICATION_MODE ccpu2(0x0d010118)
114 #define OID_802_11_PRIVACY_FILTER ccpu2(0x0d010119)
115 #define OID_802_11_BSSID_LIST_SCAN ccpu2(0x0d01011a)
116 #define OID_802_11_ENCRYPTION_STATUS ccpu2(0x0d01011b)
117 #define OID_802_11_ADD_KEY ccpu2(0x0d01011d)
118 #define OID_802_11_REMOVE_KEY ccpu2(0x0d01011e)
119 #define OID_802_11_PMKID ccpu2(0x0d010123)
120 #define OID_802_11_NETWORK_TYPES_SUPPORTED ccpu2(0x0d010203)
121 #define OID_802_11_NETWORK_TYPE_IN_USE ccpu2(0x0d010204)
122 #define OID_802_11_TX_POWER_LEVEL ccpu2(0x0d010205)
123 #define OID_802_11_RSSI ccpu2(0x0d010206)
124 #define OID_802_11_RSSI_TRIGGER ccpu2(0x0d010207)
125 #define OID_802_11_FRAGMENTATION_THRESHOLD ccpu2(0x0d010209)
126 #define OID_802_11_RTS_THRESHOLD ccpu2(0x0d01020a)
127 #define OID_802_11_SUPPORTED_RATES ccpu2(0x0d01020e)
128 #define OID_802_11_CONFIGURATION ccpu2(0x0d010211)
129 #define OID_802_11_BSSID_LIST ccpu2(0x0d010217)
132 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
133 #define WL_NOISE -96 /* typical noise level in dBm */
134 #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
137 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
138 * based on wireless rndis) has default txpower of 13dBm.
139 * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
142 #define BCM4320_DEFAULT_TXPOWER 20
145 /* codes for "status" field of completion messages */
146 #define RNDIS_STATUS_ADAPTER_NOT_READY ccpu2(0xc0010011)
147 #define RNDIS_STATUS_ADAPTER_NOT_OPEN ccpu2(0xc0010012)
150 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
151 * slightly modified for datatype endianess, etc
153 #define NDIS_802_11_LENGTH_SSID 32
154 #define NDIS_802_11_LENGTH_RATES 8
155 #define NDIS_802_11_LENGTH_RATES_EX 16
157 struct NDIS_802_11_SSID {
159 u8 Ssid[NDIS_802_11_LENGTH_SSID];
160 } __attribute__((packed));
162 enum NDIS_802_11_NETWORK_TYPE {
167 Ndis802_11NetworkTypeMax
170 struct NDIS_802_11_CONFIGURATION_FH {
175 } __attribute__((packed));
177 struct NDIS_802_11_CONFIGURATION {
182 struct NDIS_802_11_CONFIGURATION_FH FHConfig;
183 } __attribute__((packed));
185 enum NDIS_802_11_NETWORK_INFRASTRUCTURE {
187 Ndis802_11Infrastructure,
188 Ndis802_11AutoUnknown,
189 Ndis802_11InfrastructureMax
192 enum NDIS_802_11_AUTHENTICATION_MODE {
193 Ndis802_11AuthModeOpen,
194 Ndis802_11AuthModeShared,
195 Ndis802_11AuthModeAutoSwitch,
196 Ndis802_11AuthModeWPA,
197 Ndis802_11AuthModeWPAPSK,
198 Ndis802_11AuthModeWPANone,
199 Ndis802_11AuthModeWPA2,
200 Ndis802_11AuthModeWPA2PSK,
201 Ndis802_11AuthModeMax
204 enum NDIS_802_11_ENCRYPTION_STATUS {
205 Ndis802_11WEPEnabled,
206 Ndis802_11Encryption1Enabled = Ndis802_11WEPEnabled,
207 Ndis802_11WEPDisabled,
208 Ndis802_11EncryptionDisabled = Ndis802_11WEPDisabled,
209 Ndis802_11WEPKeyAbsent,
210 Ndis802_11Encryption1KeyAbsent = Ndis802_11WEPKeyAbsent,
211 Ndis802_11WEPNotSupported,
212 Ndis802_11EncryptionNotSupported = Ndis802_11WEPNotSupported,
213 Ndis802_11Encryption2Enabled,
214 Ndis802_11Encryption2KeyAbsent,
215 Ndis802_11Encryption3Enabled,
216 Ndis802_11Encryption3KeyAbsent
219 enum NDIS_802_11_PRIVACY_FILTER {
220 Ndis802_11PrivFilterAcceptAll,
221 Ndis802_11PrivFilter8021xWEP
224 struct NDIS_WLAN_BSSID_EX {
228 struct NDIS_802_11_SSID Ssid;
231 __le32 NetworkTypeInUse;
232 struct NDIS_802_11_CONFIGURATION Configuration;
233 __le32 InfrastructureMode;
234 u8 SupportedRates[NDIS_802_11_LENGTH_RATES_EX];
237 } __attribute__((packed));
239 struct NDIS_802_11_BSSID_LIST_EX {
240 __le32 NumberOfItems;
241 struct NDIS_WLAN_BSSID_EX Bssid[0];
242 } __attribute__((packed));
244 struct NDIS_802_11_FIXED_IEs {
246 __le16 BeaconInterval;
248 } __attribute__((packed));
250 struct NDIS_802_11_WEP {
255 } __attribute__((packed));
257 struct NDIS_802_11_KEY {
265 } __attribute__((packed));
267 struct NDIS_802_11_REMOVE_KEY {
271 } __attribute__((packed));
273 struct RNDIS_CONFIG_PARAMETER_INFOBUFFER {
274 __le32 ParameterNameOffset;
275 __le32 ParameterNameLength;
276 __le32 ParameterType;
277 __le32 ParameterValueOffset;
278 __le32 ParameterValueLength;
279 } __attribute__((packed));
281 /* these have to match what is in wpa_supplicant */
282 enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
283 enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
285 enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
286 KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
291 #define NET_TYPE_11FB 0
293 #define CAP_MODE_80211A 1
294 #define CAP_MODE_80211B 2
295 #define CAP_MODE_80211G 4
296 #define CAP_MODE_MASK 7
297 #define CAP_SUPPORT_TXPOWER 8
299 #define WORK_CONNECTION_EVENT (1<<0)
300 #define WORK_SET_MULTICAST_LIST (1<<1)
302 /* RNDIS device private data */
303 struct rndis_wext_private {
306 struct usbnet *usbdev;
308 struct workqueue_struct *workqueue;
309 struct delayed_work stats_work;
310 struct work_struct work;
311 struct mutex command_lock;
312 spinlock_t stats_lock;
313 unsigned long work_pending;
315 struct iw_statistics iwstats;
316 struct iw_statistics privstats;
324 /* module parameters */
325 char param_country[4];
326 int param_frameburst;
327 int param_afterburner;
328 int param_power_save;
329 int param_power_output;
330 int param_roamtrigger;
332 u32 param_workaround_interval;
337 struct NDIS_802_11_SSID essid;
339 /* encryption stuff */
340 int encr_tx_key_index;
341 char encr_keys[4][32];
349 int wpa_cipher_group;
353 static const int freq_chan[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
354 2447, 2452, 2457, 2462, 2467, 2472, 2484 };
356 static const int rates_80211g[8] = { 6, 9, 12, 18, 24, 36, 48, 54 };
358 static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
360 static const unsigned char zero_bssid[ETH_ALEN] = {0,};
361 static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
365 static struct rndis_wext_private *get_rndis_wext_priv(struct usbnet *dev)
367 return (struct rndis_wext_private *)dev->driver_priv;
371 static u32 get_bcm4320_power(struct rndis_wext_private *priv)
373 return BCM4320_DEFAULT_TXPOWER *
374 bcm4320_power_output[priv->param_power_output] / 100;
378 /* translate error code */
379 static int rndis_error_status(__le32 rndis_status)
382 switch (rndis_status) {
383 case RNDIS_STATUS_SUCCESS:
386 case RNDIS_STATUS_FAILURE:
387 case RNDIS_STATUS_INVALID_DATA:
390 case RNDIS_STATUS_NOT_SUPPORTED:
393 case RNDIS_STATUS_ADAPTER_NOT_READY:
394 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
402 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
404 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
407 struct rndis_msg_hdr *header;
408 struct rndis_query *get;
409 struct rndis_query_c *get_c;
413 buflen = *len + sizeof(*u.get);
414 if (buflen < CONTROL_BUFFER_SIZE)
415 buflen = CONTROL_BUFFER_SIZE;
416 u.buf = kmalloc(buflen, GFP_KERNEL);
419 memset(u.get, 0, sizeof *u.get);
420 u.get->msg_type = RNDIS_MSG_QUERY;
421 u.get->msg_len = ccpu2(sizeof *u.get);
424 mutex_lock(&priv->command_lock);
425 ret = rndis_command(dev, u.header);
426 mutex_unlock(&priv->command_lock);
429 ret = le32_to_cpu(u.get_c->len);
430 *len = (*len > ret) ? ret : *len;
431 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
432 ret = rndis_error_status(u.get_c->status);
440 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
442 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
445 struct rndis_msg_hdr *header;
446 struct rndis_set *set;
447 struct rndis_set_c *set_c;
451 buflen = len + sizeof(*u.set);
452 if (buflen < CONTROL_BUFFER_SIZE)
453 buflen = CONTROL_BUFFER_SIZE;
454 u.buf = kmalloc(buflen, GFP_KERNEL);
458 memset(u.set, 0, sizeof *u.set);
459 u.set->msg_type = RNDIS_MSG_SET;
460 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
462 u.set->len = cpu_to_le32(len);
463 u.set->offset = ccpu2(sizeof(*u.set) - 8);
464 u.set->handle = ccpu2(0);
465 memcpy(u.buf + sizeof(*u.set), data, len);
467 mutex_lock(&priv->command_lock);
468 ret = rndis_command(dev, u.header);
469 mutex_unlock(&priv->command_lock);
472 ret = rndis_error_status(u.set_c->status);
480 * Specs say that we can only set config parameters only soon after device
482 * value_type: 0 = u32, 2 = unicode string
484 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
485 int value_type, void *value)
487 struct RNDIS_CONFIG_PARAMETER_INFOBUFFER *infobuf;
488 int value_len, info_len, param_len, ret, i;
493 value_len = sizeof(__le32);
494 else if (value_type == 2)
495 value_len = strlen(value) * sizeof(__le16);
499 param_len = strlen(param) * sizeof(__le16);
500 info_len = sizeof(*infobuf) + param_len + value_len;
505 infobuf = kmalloc(info_len, GFP_KERNEL);
511 /* extra 12 bytes are for padding (debug output) */
512 memset(infobuf, 0xCC, info_len + 12);
516 devdbg(dev, "setting config parameter: %s, value: %s",
519 devdbg(dev, "setting config parameter: %s, value: %d",
520 param, *(u32 *)value);
522 infobuf->ParameterNameOffset = cpu_to_le32(sizeof(*infobuf));
523 infobuf->ParameterNameLength = cpu_to_le32(param_len);
524 infobuf->ParameterType = cpu_to_le32(value_type);
525 infobuf->ParameterValueOffset = cpu_to_le32(sizeof(*infobuf) +
527 infobuf->ParameterValueLength = cpu_to_le32(value_len);
529 /* simple string to unicode string conversion */
530 unibuf = (void *)infobuf + sizeof(*infobuf);
531 for (i = 0; i < param_len / sizeof(__le16); i++)
532 unibuf[i] = cpu_to_le16(param[i]);
534 if (value_type == 2) {
535 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
536 for (i = 0; i < value_len / sizeof(__le16); i++)
537 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
539 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
540 *dst_value = cpu_to_le32(*(u32 *)value);
544 devdbg(dev, "info buffer (len: %d):", info_len);
545 for (i = 0; i < info_len; i += 12) {
546 u32 *tmp = (u32 *)((u8 *)infobuf + i);
547 devdbg(dev, "%08X:%08X:%08X",
550 cpu_to_be32(tmp[2]));
554 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
557 devdbg(dev, "setting rndis config paramater failed, %d.", ret);
563 static int rndis_set_config_parameter_str(struct usbnet *dev,
564 char *param, char *value)
566 return(rndis_set_config_parameter(dev, param, 2, value));
569 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
570 char *param, u32 value)
572 return(rndis_set_config_parameter(dev, param, 0, &value));
577 * data conversion functions
579 static int level_to_qual(int level)
581 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
582 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
586 static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
592 /* see comment in wireless.h above the "struct iw_freq"
593 * definition for an explanation of this if
594 * NOTE: 1000000 is due to the kHz
596 if (dsconfig > 1000000) {
597 freq->m = dsconfig / 10;
602 /* convert from kHz to Hz */
607 static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
609 if (freq->m < 1000 && freq->e == 0) {
611 freq->m <= (sizeof(freq_chan) / sizeof(freq_chan[0])))
612 *dsconfig = freq_chan[freq->m - 1] * 1000;
618 for (i = freq->e; i > 0; i--)
631 add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
633 static int get_essid(struct usbnet *usbdev, struct NDIS_802_11_SSID *ssid)
638 ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
641 ssid->SsidLength = 0;
645 unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
647 memcpy(tmp, ssid->Ssid, le32_to_cpu(ssid->SsidLength));
648 tmp[le32_to_cpu(ssid->SsidLength)] = 0;
649 devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
656 static int set_essid(struct usbnet *usbdev, struct NDIS_802_11_SSID *ssid)
658 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
661 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
663 memcpy(&priv->essid, ssid, sizeof(priv->essid));
665 devdbg(usbdev, "set_essid: radio_on = 1");
672 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
677 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
680 memset(bssid, 0, ETH_ALEN);
686 static int is_associated(struct usbnet *usbdev)
691 ret = get_bssid(usbdev, bssid);
693 return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
697 static int disassociate(struct usbnet *usbdev, int reset_ssid)
699 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
700 struct NDIS_802_11_SSID ssid;
703 if (priv->radio_on) {
704 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
707 devdbg(usbdev, "disassociate: radio_on = 0");
714 /* disassociate causes radio to be turned off; if reset_ssid
715 * is given, set random ssid to enable radio */
717 ssid.SsidLength = cpu_to_le32(sizeof(ssid.Ssid));
718 get_random_bytes(&ssid.Ssid[2], sizeof(ssid.Ssid)-2);
721 for (i = 2; i < sizeof(ssid.Ssid); i++)
722 ssid.Ssid[i] = 0x1 + (ssid.Ssid[i] * 0xfe / 0xff);
723 ret = set_essid(usbdev, &ssid);
729 static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
731 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
735 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
736 "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
738 if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
739 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
740 auth_mode = Ndis802_11AuthModeWPA2;
742 auth_mode = Ndis802_11AuthModeWPA2PSK;
743 } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
744 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
745 auth_mode = Ndis802_11AuthModeWPA;
746 else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
747 auth_mode = Ndis802_11AuthModeWPAPSK;
749 auth_mode = Ndis802_11AuthModeWPANone;
750 } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
751 if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
752 auth_mode = Ndis802_11AuthModeAutoSwitch;
754 auth_mode = Ndis802_11AuthModeShared;
756 auth_mode = Ndis802_11AuthModeOpen;
758 tmp = cpu_to_le32(auth_mode);
759 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
762 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
766 priv->wpa_version = wpa_version;
767 priv->wpa_authalg = authalg;
772 static int set_priv_filter(struct usbnet *usbdev)
774 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
777 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
779 if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
780 priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
781 tmp = cpu_to_le32(Ndis802_11PrivFilter8021xWEP);
783 tmp = cpu_to_le32(Ndis802_11PrivFilterAcceptAll);
785 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
790 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
792 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
796 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
800 if (pairwise & IW_AUTH_CIPHER_CCMP)
801 encr_mode = Ndis802_11Encryption3Enabled;
802 else if (pairwise & IW_AUTH_CIPHER_TKIP)
803 encr_mode = Ndis802_11Encryption2Enabled;
805 (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
806 encr_mode = Ndis802_11Encryption1Enabled;
807 else if (groupwise & IW_AUTH_CIPHER_CCMP)
808 encr_mode = Ndis802_11Encryption3Enabled;
809 else if (groupwise & IW_AUTH_CIPHER_TKIP)
810 encr_mode = Ndis802_11Encryption2Enabled;
812 encr_mode = Ndis802_11EncryptionDisabled;
814 tmp = cpu_to_le32(encr_mode);
815 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
818 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
822 priv->wpa_cipher_pair = pairwise;
823 priv->wpa_cipher_group = groupwise;
828 static int set_assoc_params(struct usbnet *usbdev)
830 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
832 set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
833 set_priv_filter(usbdev);
834 set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
840 static int set_infra_mode(struct usbnet *usbdev, int mode)
842 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
846 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
848 tmp = cpu_to_le32(mode);
849 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
852 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
856 /* NDIS drivers clear keys when infrastructure mode is
857 * changed. But Linux tools assume otherwise. So set the
859 if (priv->wpa_keymgmt == 0 ||
860 priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
861 for (i = 0; i < 4; i++) {
862 if (priv->encr_key_len[i] > 0)
863 add_wep_key(usbdev, priv->encr_keys[i],
864 priv->encr_key_len[i], i);
868 priv->infra_mode = mode;
873 static void set_default_iw_params(struct usbnet *usbdev)
875 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
877 priv->wpa_keymgmt = 0;
878 priv->wpa_version = 0;
880 set_infra_mode(usbdev, Ndis802_11Infrastructure);
881 set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
882 IW_AUTH_ALG_OPEN_SYSTEM);
883 set_priv_filter(usbdev);
884 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
888 static int deauthenticate(struct usbnet *usbdev)
892 ret = disassociate(usbdev, 1);
893 set_default_iw_params(usbdev);
898 /* index must be 0 - N, as per NDIS */
899 static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
901 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
902 struct NDIS_802_11_WEP ndis_key;
905 if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
908 memset(&ndis_key, 0, sizeof(ndis_key));
910 ndis_key.Length = cpu_to_le32(sizeof(ndis_key));
911 ndis_key.KeyLength = cpu_to_le32(key_len);
912 ndis_key.KeyIndex = cpu_to_le32(index);
913 memcpy(&ndis_key.KeyMaterial, key, key_len);
915 if (index == priv->encr_tx_key_index) {
916 ndis_key.KeyIndex |= cpu_to_le32(1 << 31);
917 ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
918 IW_AUTH_CIPHER_NONE);
920 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
924 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
927 devwarn(usbdev, "adding encryption key %d failed (%08X)",
932 priv->encr_key_len[index] = key_len;
933 memcpy(&priv->encr_keys[index], key, key_len);
939 /* remove_key is for both wep and wpa */
940 static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
942 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
943 struct NDIS_802_11_REMOVE_KEY remove_key;
947 if (priv->encr_key_len[index] == 0)
950 priv->encr_key_len[index] = 0;
951 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
953 if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
954 priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
955 priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
956 priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
957 remove_key.Length = cpu_to_le32(sizeof(remove_key));
958 remove_key.KeyIndex = cpu_to_le32(index);
961 if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
962 remove_key.KeyIndex |= cpu_to_le32(1 << 30);
963 memcpy(remove_key.Bssid, bssid,
964 sizeof(remove_key.Bssid));
966 memset(remove_key.Bssid, 0xff,
967 sizeof(remove_key.Bssid));
969 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
974 keyindex = cpu_to_le32(index);
975 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
979 "removing encryption key %d failed (%08X)",
985 /* if it is transmit key, disable encryption */
986 if (index == priv->encr_tx_key_index)
987 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
993 static void set_multicast_list(struct usbnet *usbdev)
995 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
996 struct dev_mc_list *mclist;
1001 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1003 if (usbdev->net->flags & IFF_PROMISC) {
1004 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1005 RNDIS_PACKET_TYPE_ALL_LOCAL;
1006 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1007 usbdev->net->mc_count > priv->multicast_size) {
1008 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1009 } else if (usbdev->net->mc_count > 0) {
1010 size = min(priv->multicast_size, usbdev->net->mc_count);
1011 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1014 "couldn't alloc %d bytes of memory",
1019 mclist = usbdev->net->mc_list;
1020 for (i = 0; i < size && mclist; mclist = mclist->next) {
1021 if (mclist->dmi_addrlen != ETH_ALEN)
1024 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1028 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1030 if (ret == 0 && i > 0)
1031 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1033 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1035 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1036 i, priv->multicast_size, ret);
1041 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1044 devwarn(usbdev, "couldn't set packet filter: %08x",
1045 le32_to_cpu(filter));
1048 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1049 le32_to_cpu(filter), ret);
1054 * wireless extension handlers
1057 static int rndis_iw_commit(struct net_device *dev,
1058 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1065 static int rndis_iw_get_range(struct net_device *dev,
1066 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1068 struct iw_range *range = (struct iw_range *)extra;
1069 struct usbnet *usbdev = dev->priv;
1070 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1071 int len, ret, i, j, num, has_80211g_rates;
1075 devdbg(usbdev, "SIOCGIWRANGE");
1077 /* clear iw_range struct */
1078 memset(range, 0, sizeof(*range));
1079 wrqu->data.length = sizeof(*range);
1081 range->txpower_capa = IW_TXPOW_MWATT;
1082 range->num_txpower = 1;
1083 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1084 len = sizeof(tx_power);
1085 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1087 if (ret == 0 && le32_to_cpu(tx_power) != 0xFF)
1088 range->txpower[0] = le32_to_cpu(tx_power);
1090 range->txpower[0] = get_bcm4320_power(priv);
1092 range->txpower[0] = get_bcm4320_power(priv);
1094 len = sizeof(rates);
1095 ret = rndis_query_oid(usbdev, OID_802_11_SUPPORTED_RATES, &rates,
1097 has_80211g_rates = 0;
1100 for (i = 0; i < len; i++) {
1103 range->bitrate[j] = (rates[i] & 0x7f) * 500000;
1104 /* check for non 802.11b rates */
1105 if (range->bitrate[j] == 6000000 ||
1106 range->bitrate[j] == 9000000 ||
1107 (range->bitrate[j] >= 12000000 &&
1108 range->bitrate[j] != 22000000))
1109 has_80211g_rates = 1;
1112 range->num_bitrates = j;
1114 range->num_bitrates = 0;
1116 /* fill in 802.11g rates */
1117 if (has_80211g_rates) {
1118 num = range->num_bitrates;
1119 for (i = 0; i < sizeof(rates_80211g); i++) {
1120 for (j = 0; j < num; j++) {
1121 if (range->bitrate[j] ==
1122 rates_80211g[i] * 1000000)
1126 range->bitrate[range->num_bitrates++] =
1127 rates_80211g[i] * 1000000;
1128 if (range->num_bitrates == IW_MAX_BITRATES)
1132 /* estimated max real througput in bps */
1133 range->throughput = 54 * 1000 * 1000 / 2;
1135 /* ~35% more with afterburner */
1136 if (priv->param_afterburner)
1137 range->throughput = range->throughput / 100 * 135;
1139 /* estimated max real througput in bps */
1140 range->throughput = 11 * 1000 * 1000 / 2;
1143 range->num_channels = (sizeof(freq_chan)/sizeof(freq_chan[0]));
1145 for (i = 0; i < (sizeof(freq_chan)/sizeof(freq_chan[0])) &&
1146 i < IW_MAX_FREQUENCIES; i++) {
1147 range->freq[i].i = i + 1;
1148 range->freq[i].m = freq_chan[i] * 100000;
1149 range->freq[i].e = 1;
1151 range->num_frequency = i;
1154 range->max_rts = 2347;
1155 range->min_frag = 256;
1156 range->max_frag = 2346;
1158 range->max_qual.qual = 100;
1159 range->max_qual.level = 154;
1160 range->max_qual.updated = IW_QUAL_QUAL_UPDATED
1161 | IW_QUAL_LEVEL_UPDATED
1162 | IW_QUAL_NOISE_INVALID;
1164 range->we_version_compiled = WIRELESS_EXT;
1165 range->we_version_source = WIRELESS_EXT;
1167 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
1168 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
1173 static int rndis_iw_get_name(struct net_device *dev,
1174 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1176 struct usbnet *usbdev = dev->priv;
1177 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1179 strcpy(wrqu->name, priv->name);
1184 static int rndis_iw_set_essid(struct net_device *dev,
1185 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1187 struct NDIS_802_11_SSID ssid;
1188 int length = wrqu->essid.length;
1189 struct usbnet *usbdev = dev->priv;
1191 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1192 wrqu->essid.flags, wrqu->essid.length, essid);
1194 if (length > NDIS_802_11_LENGTH_SSID)
1195 length = NDIS_802_11_LENGTH_SSID;
1197 ssid.SsidLength = cpu_to_le32(length);
1199 memcpy(ssid.Ssid, essid, length);
1201 memset(ssid.Ssid, 0, NDIS_802_11_LENGTH_SSID);
1203 set_assoc_params(usbdev);
1205 if (!wrqu->essid.flags || length == 0)
1206 return disassociate(usbdev, 1);
1208 return set_essid(usbdev, &ssid);
1212 static int rndis_iw_get_essid(struct net_device *dev,
1213 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1215 struct NDIS_802_11_SSID ssid;
1216 struct usbnet *usbdev = dev->priv;
1219 ret = get_essid(usbdev, &ssid);
1221 if (ret == 0 && le32_to_cpu(ssid.SsidLength) > 0) {
1222 wrqu->essid.flags = 1;
1223 wrqu->essid.length = le32_to_cpu(ssid.SsidLength);
1224 memcpy(essid, ssid.Ssid, wrqu->essid.length);
1225 essid[wrqu->essid.length] = 0;
1227 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1228 wrqu->essid.flags = 0;
1229 wrqu->essid.length = 0;
1231 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1236 static int rndis_iw_get_bssid(struct net_device *dev,
1237 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1239 struct usbnet *usbdev = dev->priv;
1240 unsigned char bssid[ETH_ALEN];
1242 DECLARE_MAC_BUF(mac);
1244 ret = get_bssid(usbdev, bssid);
1247 devdbg(usbdev, "SIOCGIWAP: %s", print_mac(mac, bssid));
1249 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1251 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1252 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1258 static int rndis_iw_set_bssid(struct net_device *dev,
1259 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1261 struct usbnet *usbdev = dev->priv;
1262 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1263 DECLARE_MAC_BUF(mac);
1266 devdbg(usbdev, "SIOCSIWAP: %s", print_mac(mac, bssid));
1268 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1270 /* user apps may set ap's mac address, which is not required;
1271 * they may fail to work if this function fails, so return
1274 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1280 static int rndis_iw_set_auth(struct net_device *dev,
1281 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1283 struct iw_param *p = &wrqu->param;
1284 struct usbnet *usbdev = dev->priv;
1285 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1286 int ret = -ENOTSUPP;
1288 switch (p->flags & IW_AUTH_INDEX) {
1289 case IW_AUTH_WPA_VERSION:
1290 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1291 priv->wpa_version = p->value;
1295 case IW_AUTH_CIPHER_PAIRWISE:
1296 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1297 priv->wpa_cipher_pair = p->value;
1301 case IW_AUTH_CIPHER_GROUP:
1302 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1303 priv->wpa_cipher_group = p->value;
1307 case IW_AUTH_KEY_MGMT:
1308 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1309 priv->wpa_keymgmt = p->value;
1313 case IW_AUTH_TKIP_COUNTERMEASURES:
1314 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1319 case IW_AUTH_DROP_UNENCRYPTED:
1320 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1324 case IW_AUTH_80211_AUTH_ALG:
1325 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1326 priv->wpa_authalg = p->value;
1330 case IW_AUTH_WPA_ENABLED:
1331 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1332 if (wrqu->param.value)
1333 deauthenticate(usbdev);
1337 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1338 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1343 case IW_AUTH_ROAMING_CONTROL:
1344 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1348 case IW_AUTH_PRIVACY_INVOKED:
1349 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1350 wrqu->param.flags & IW_AUTH_INDEX);
1354 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
1355 p->flags & IW_AUTH_INDEX, p->value);
1361 static int rndis_iw_get_auth(struct net_device *dev,
1362 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1364 struct iw_param *p = &wrqu->param;
1365 struct usbnet *usbdev = dev->priv;
1366 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1368 switch (p->flags & IW_AUTH_INDEX) {
1369 case IW_AUTH_WPA_VERSION:
1370 p->value = priv->wpa_version;
1372 case IW_AUTH_CIPHER_PAIRWISE:
1373 p->value = priv->wpa_cipher_pair;
1375 case IW_AUTH_CIPHER_GROUP:
1376 p->value = priv->wpa_cipher_group;
1378 case IW_AUTH_KEY_MGMT:
1379 p->value = priv->wpa_keymgmt;
1381 case IW_AUTH_80211_AUTH_ALG:
1382 p->value = priv->wpa_authalg;
1385 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1386 wrqu->param.flags & IW_AUTH_INDEX);
1393 static int rndis_iw_get_mode(struct net_device *dev,
1394 struct iw_request_info *info,
1395 union iwreq_data *wrqu, char *extra)
1397 struct usbnet *usbdev = dev->priv;
1398 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1400 switch (priv->infra_mode) {
1401 case Ndis802_11IBSS:
1402 wrqu->mode = IW_MODE_ADHOC;
1404 case Ndis802_11Infrastructure:
1405 wrqu->mode = IW_MODE_INFRA;
1407 /*case Ndis802_11AutoUnknown:*/
1409 wrqu->mode = IW_MODE_AUTO;
1412 devdbg(usbdev, "SIOCGIWMODE: %08x", wrqu->mode);
1417 static int rndis_iw_set_mode(struct net_device *dev,
1418 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1420 struct usbnet *usbdev = dev->priv;
1423 devdbg(usbdev, "SIOCSIWMODE: %08x", wrqu->mode);
1425 switch (wrqu->mode) {
1427 mode = Ndis802_11IBSS;
1430 mode = Ndis802_11Infrastructure;
1432 /*case IW_MODE_AUTO:*/
1434 mode = Ndis802_11AutoUnknown;
1438 return set_infra_mode(usbdev, mode);
1442 static int rndis_iw_set_encode(struct net_device *dev,
1443 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1445 struct usbnet *usbdev = dev->priv;
1446 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1447 int ret, index, key_len;
1450 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1452 /* iwconfig gives index as 1 - N */
1456 index = priv->encr_tx_key_index;
1458 if (index < 0 || index >= 4) {
1459 devwarn(usbdev, "encryption index out of range (%u)", index);
1463 /* remove key if disabled */
1464 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1465 if (remove_key(usbdev, index, NULL))
1471 /* global encryption state (for all keys) */
1472 if (wrqu->data.flags & IW_ENCODE_OPEN)
1473 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1474 IW_AUTH_ALG_OPEN_SYSTEM);
1475 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1476 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1477 IW_AUTH_ALG_SHARED_KEY);
1481 if (wrqu->data.length > 0) {
1482 key_len = wrqu->data.length;
1485 /* must be set as tx key */
1486 if (priv->encr_key_len[index] == 0)
1488 key_len = priv->encr_key_len[index];
1489 key = priv->encr_keys[index];
1490 priv->encr_tx_key_index = index;
1493 if (add_wep_key(usbdev, key, key_len, index) != 0)
1496 if (index == priv->encr_tx_key_index)
1497 /* ndis drivers want essid to be set after setting encr */
1498 set_essid(usbdev, &priv->essid);
1504 static int rndis_iw_set_encode_ext(struct net_device *dev,
1505 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1507 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1508 struct usbnet *usbdev = dev->priv;
1509 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1510 struct NDIS_802_11_KEY ndis_key;
1514 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1516 /* iwconfig gives index as 1 - N */
1520 keyidx = priv->encr_tx_key_index;
1522 if (keyidx < 0 || keyidx >= 4)
1525 if (ext->alg == WPA_ALG_WEP) {
1526 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1527 priv->encr_tx_key_index = keyidx;
1528 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1531 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1532 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1533 return remove_key(usbdev, keyidx, NULL);
1535 if (ext->key_len > sizeof(ndis_key.KeyMaterial))
1538 memset(&ndis_key, 0, sizeof(ndis_key));
1540 ndis_key.Length = cpu_to_le32(sizeof(ndis_key) -
1541 sizeof(ndis_key.KeyMaterial) + ext->key_len);
1542 ndis_key.KeyLength = cpu_to_le32(ext->key_len);
1543 ndis_key.KeyIndex = cpu_to_le32(keyidx);
1545 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1546 memcpy(ndis_key.KeyRSC, ext->rx_seq, 6);
1547 ndis_key.KeyIndex |= cpu_to_le32(1 << 29);
1550 addr = ext->addr.sa_data;
1551 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1553 if (priv->infra_mode == Ndis802_11IBSS)
1554 memset(ndis_key.Bssid, 0xff, ETH_ALEN);
1556 get_bssid(usbdev, ndis_key.Bssid);
1559 ndis_key.KeyIndex |= cpu_to_le32(1 << 30);
1560 memcpy(ndis_key.Bssid, addr, ETH_ALEN);
1563 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1564 ndis_key.KeyIndex |= cpu_to_le32(1 << 31);
1566 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1567 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1568 * different order than NDIS spec, so swap the order here. */
1569 memcpy(ndis_key.KeyMaterial, ext->key, 16);
1570 memcpy(ndis_key.KeyMaterial + 16, ext->key + 24, 8);
1571 memcpy(ndis_key.KeyMaterial + 24, ext->key + 16, 8);
1573 memcpy(ndis_key.KeyMaterial, ext->key, ext->key_len);
1575 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1576 le32_to_cpu(ndis_key.Length));
1577 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1581 priv->encr_key_len[keyidx] = ext->key_len;
1582 memcpy(&priv->encr_keys[keyidx], ndis_key.KeyMaterial, ext->key_len);
1583 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1584 priv->encr_tx_key_index = keyidx;
1590 static int rndis_iw_set_scan(struct net_device *dev,
1591 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1593 struct iw_param *param = &wrqu->param;
1594 struct usbnet *usbdev = dev->priv;
1595 union iwreq_data evt;
1599 devdbg(usbdev, "SIOCSIWSCAN");
1601 if (param->flags == 0) {
1603 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1606 evt.data.length = 0;
1607 wireless_send_event(dev, SIOCGIWSCAN, &evt, NULL);
1613 static char *rndis_translate_scan(struct net_device *dev,
1614 char *cev, char *end_buf, struct NDIS_WLAN_BSSID_EX *bssid)
1617 struct usbnet *usbdev = dev->priv;
1619 struct ieee80211_info_element *ie;
1621 int bssid_len, ie_len, i;
1623 struct iw_event iwe;
1624 unsigned char sbuf[32];
1625 DECLARE_MAC_BUF(mac);
1627 bssid_len = le32_to_cpu(bssid->Length);
1629 devdbg(usbdev, "BSSID %s", print_mac(mac, bssid->MacAddress));
1630 iwe.cmd = SIOCGIWAP;
1631 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1632 memcpy(iwe.u.ap_addr.sa_data, bssid->MacAddress, ETH_ALEN);
1633 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_ADDR_LEN);
1635 devdbg(usbdev, "SSID(%d) %s",
1636 le32_to_cpu(bssid->Ssid.SsidLength),
1638 iwe.cmd = SIOCGIWESSID;
1639 iwe.u.essid.length = le32_to_cpu(bssid->Ssid.SsidLength);
1640 iwe.u.essid.flags = 1;
1641 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1644 devdbg(usbdev, "MODE %d",
1645 le32_to_cpu(bssid->InfrastructureMode));
1646 iwe.cmd = SIOCGIWMODE;
1647 switch (le32_to_cpu(bssid->InfrastructureMode)) {
1648 case Ndis802_11IBSS:
1649 iwe.u.mode = IW_MODE_ADHOC;
1651 case Ndis802_11Infrastructure:
1652 iwe.u.mode = IW_MODE_INFRA;
1654 /*case Ndis802_11AutoUnknown:*/
1656 iwe.u.mode = IW_MODE_AUTO;
1659 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_UINT_LEN);
1661 devdbg(usbdev, "FREQ %d kHz",
1662 le32_to_cpu(bssid->Configuration.DSConfig));
1663 iwe.cmd = SIOCGIWFREQ;
1664 dsconfig_to_freq(le32_to_cpu(bssid->Configuration.DSConfig),
1666 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_FREQ_LEN);
1668 devdbg(usbdev, "QUAL %d", le32_to_cpu(bssid->Rssi));
1670 iwe.u.qual.qual = level_to_qual(le32_to_cpu(bssid->Rssi));
1671 iwe.u.qual.level = le32_to_cpu(bssid->Rssi);
1672 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
1673 | IW_QUAL_LEVEL_UPDATED
1674 | IW_QUAL_NOISE_INVALID;
1675 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_QUAL_LEN);
1677 devdbg(usbdev, "ENCODE %d", le32_to_cpu(bssid->Privacy));
1678 iwe.cmd = SIOCGIWENCODE;
1679 iwe.u.data.length = 0;
1680 if (le32_to_cpu(bssid->Privacy) == Ndis802_11PrivFilterAcceptAll)
1681 iwe.u.data.flags = IW_ENCODE_DISABLED;
1683 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1685 cev = iwe_stream_add_point(cev, end_buf, &iwe, NULL);
1687 devdbg(usbdev, "RATES:");
1688 current_val = cev + IW_EV_LCP_LEN;
1689 iwe.cmd = SIOCGIWRATE;
1690 for (i = 0; i < sizeof(bssid->SupportedRates); i++) {
1691 if (bssid->SupportedRates[i] & 0x7f) {
1692 iwe.u.bitrate.value =
1693 ((bssid->SupportedRates[i] & 0x7f) *
1695 devdbg(usbdev, " %d", iwe.u.bitrate.value);
1696 current_val = iwe_stream_add_value(cev,
1697 current_val, end_buf, &iwe,
1702 if ((current_val - cev) > IW_EV_LCP_LEN)
1705 beacon = le32_to_cpu(bssid->Configuration.BeaconPeriod);
1706 devdbg(usbdev, "BCN_INT %d", beacon);
1707 iwe.cmd = IWEVCUSTOM;
1708 snprintf(sbuf, sizeof(sbuf), "bcn_int=%d", beacon);
1709 iwe.u.data.length = strlen(sbuf);
1710 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1712 atim = le32_to_cpu(bssid->Configuration.ATIMWindow);
1713 devdbg(usbdev, "ATIM %d", atim);
1714 iwe.cmd = IWEVCUSTOM;
1715 snprintf(sbuf, sizeof(sbuf), "atim=%u", atim);
1716 iwe.u.data.length = strlen(sbuf);
1717 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1719 ie = (void *)(bssid->IEs + sizeof(struct NDIS_802_11_FIXED_IEs));
1720 ie_len = min(bssid_len - (int)sizeof(*bssid),
1721 (int)le32_to_cpu(bssid->IELength));
1722 ie_len -= sizeof(struct NDIS_802_11_FIXED_IEs);
1723 while (ie_len >= sizeof(*ie) && sizeof(*ie) + ie->len <= ie_len) {
1724 if ((ie->id == MFIE_TYPE_GENERIC && ie->len >= 4 &&
1725 memcmp(ie->data, "\x00\x50\xf2\x01", 4) == 0) ||
1726 ie->id == MFIE_TYPE_RSN) {
1727 devdbg(usbdev, "IE: WPA%d",
1728 (ie->id == MFIE_TYPE_RSN) ? 2 : 1);
1729 iwe.cmd = IWEVGENIE;
1730 iwe.u.data.length = min(ie->len + 2, MAX_WPA_IE_LEN);
1731 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1735 ie_len -= sizeof(*ie) + ie->len;
1736 ie = (struct ieee80211_info_element *)&ie->data[ie->len];
1743 static int rndis_iw_get_scan(struct net_device *dev,
1744 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1746 struct usbnet *usbdev = dev->priv;
1749 struct NDIS_802_11_BSSID_LIST_EX *bssid_list;
1750 struct NDIS_WLAN_BSSID_EX *bssid;
1751 int ret = -EINVAL, len, count, bssid_len;
1753 devdbg(usbdev, "SIOCGIWSCAN");
1755 len = CONTROL_BUFFER_SIZE;
1756 buf = kmalloc(len, GFP_KERNEL);
1762 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1768 bssid = bssid_list->Bssid;
1769 bssid_len = le32_to_cpu(bssid->Length);
1770 count = le32_to_cpu(bssid_list->NumberOfItems);
1771 devdbg(usbdev, "SIOCGIWSCAN: %d BSSIDs found", count);
1773 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1774 cev = rndis_translate_scan(dev, cev, extra + IW_SCAN_MAX_DATA,
1776 bssid = (void *)bssid + bssid_len;
1777 bssid_len = le32_to_cpu(bssid->Length);
1782 wrqu->data.length = cev - extra;
1783 wrqu->data.flags = 0;
1789 static int rndis_iw_set_genie(struct net_device *dev,
1790 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1792 struct usbnet *usbdev = dev->priv;
1793 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1799 for (j = 0; j < wrqu->data.length; j += 8)
1801 "SIOCSIWGENIE %04x - "
1802 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1803 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1804 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1806 /* clear existing IEs */
1807 if (priv->wpa_ie_len) {
1808 kfree(priv->wpa_ie);
1809 priv->wpa_ie_len = 0;
1813 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1815 priv->wpa_ie_len = wrqu->data.length;
1816 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1823 static int rndis_iw_get_genie(struct net_device *dev,
1824 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1826 struct usbnet *usbdev = dev->priv;
1827 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1829 devdbg(usbdev, "SIOCGIWGENIE");
1831 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1832 wrqu->data.length = 0;
1836 if (wrqu->data.length < priv->wpa_ie_len)
1839 wrqu->data.length = priv->wpa_ie_len;
1840 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1846 static int rndis_iw_set_rts(struct net_device *dev,
1847 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1849 struct usbnet *usbdev = dev->priv;
1851 devdbg(usbdev, "SIOCSIWRTS");
1853 tmp = cpu_to_le32(wrqu->rts.value);
1854 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1859 static int rndis_iw_get_rts(struct net_device *dev,
1860 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1862 struct usbnet *usbdev = dev->priv;
1867 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1869 wrqu->rts.value = le32_to_cpu(tmp);
1870 wrqu->rts.flags = 1;
1871 wrqu->rts.disabled = 0;
1874 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1880 static int rndis_iw_set_frag(struct net_device *dev,
1881 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1883 struct usbnet *usbdev = dev->priv;
1886 devdbg(usbdev, "SIOCSIWFRAG");
1888 tmp = cpu_to_le32(wrqu->frag.value);
1889 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1894 static int rndis_iw_get_frag(struct net_device *dev,
1895 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1897 struct usbnet *usbdev = dev->priv;
1902 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1905 wrqu->frag.value = le32_to_cpu(tmp);
1906 wrqu->frag.flags = 1;
1907 wrqu->frag.disabled = 0;
1909 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1914 static int rndis_iw_set_nick(struct net_device *dev,
1915 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1917 struct usbnet *usbdev = dev->priv;
1918 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1920 devdbg(usbdev, "SIOCSIWNICK");
1922 priv->nick_len = wrqu->data.length;
1923 if (priv->nick_len > 32)
1924 priv->nick_len = 32;
1926 memcpy(priv->nick, extra, priv->nick_len);
1931 static int rndis_iw_get_nick(struct net_device *dev,
1932 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1934 struct usbnet *usbdev = dev->priv;
1935 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1937 wrqu->data.flags = 1;
1938 wrqu->data.length = priv->nick_len;
1939 memcpy(extra, priv->nick, priv->nick_len);
1941 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1947 static int rndis_iw_set_freq(struct net_device *dev,
1948 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1950 struct usbnet *usbdev = dev->priv;
1951 struct NDIS_802_11_CONFIGURATION config;
1952 unsigned int dsconfig;
1955 /* this OID is valid only when not associated */
1956 if (is_associated(usbdev))
1960 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1963 len = sizeof(config);
1964 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1966 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1970 config.DSConfig = cpu_to_le32(dsconfig);
1972 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1973 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1978 static int rndis_iw_get_freq(struct net_device *dev,
1979 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1981 struct usbnet *usbdev = dev->priv;
1982 struct NDIS_802_11_CONFIGURATION config;
1985 len = sizeof(config);
1986 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1988 dsconfig_to_freq(le32_to_cpu(config.DSConfig), &wrqu->freq);
1990 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
1995 static int rndis_iw_get_txpower(struct net_device *dev,
1996 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1998 struct usbnet *usbdev = dev->priv;
1999 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2003 if (priv->radio_on) {
2004 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2005 len = sizeof(tx_power);
2006 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2011 /* fake incase not supported */
2012 tx_power = cpu_to_le32(get_bcm4320_power(priv));
2014 wrqu->txpower.flags = IW_TXPOW_MWATT;
2015 wrqu->txpower.value = le32_to_cpu(tx_power);
2016 wrqu->txpower.disabled = 0;
2018 wrqu->txpower.flags = IW_TXPOW_MWATT;
2019 wrqu->txpower.value = 0;
2020 wrqu->txpower.disabled = 1;
2023 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
2029 static int rndis_iw_set_txpower(struct net_device *dev,
2030 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2032 struct usbnet *usbdev = dev->priv;
2033 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2034 __le32 tx_power = 0;
2037 if (!wrqu->txpower.disabled) {
2038 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
2039 tx_power = cpu_to_le32(wrqu->txpower.value);
2040 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
2041 if (wrqu->txpower.value > 20)
2042 tx_power = cpu_to_le32(128);
2043 else if (wrqu->txpower.value < -43)
2044 tx_power = cpu_to_le32(127);
2047 tmp = wrqu->txpower.value;
2050 tx_power = cpu_to_le32((unsigned char)tmp);
2055 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
2057 if (le32_to_cpu(tx_power) != 0) {
2058 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2059 /* turn radio on first */
2060 if (!priv->radio_on)
2061 disassociate(usbdev, 1);
2063 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2064 &tx_power, sizeof(tx_power));
2069 /* txpower unsupported, just turn radio on */
2070 if (!priv->radio_on)
2071 return disassociate(usbdev, 1);
2072 return 0; /* all ready on */
2076 /* tx_power == 0, turn off radio */
2077 return disassociate(usbdev, 0);
2081 static int rndis_iw_get_rate(struct net_device *dev,
2082 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2084 struct usbnet *usbdev = dev->priv;
2089 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2091 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2092 wrqu->bitrate.disabled = 0;
2093 wrqu->bitrate.flags = 1;
2099 static int rndis_iw_set_mlme(struct net_device *dev,
2100 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2102 struct usbnet *usbdev = dev->priv;
2103 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2104 struct iw_mlme *mlme = (struct iw_mlme *)extra;
2105 unsigned char bssid[ETH_ALEN];
2107 get_bssid(usbdev, bssid);
2109 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2112 switch (mlme->cmd) {
2113 case IW_MLME_DEAUTH:
2114 return deauthenticate(usbdev);
2115 case IW_MLME_DISASSOC:
2116 return disassociate(usbdev, priv->radio_on);
2125 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2127 struct usbnet *usbdev = dev->priv;
2128 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2129 unsigned long flags;
2131 spin_lock_irqsave(&priv->stats_lock, flags);
2132 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2133 spin_unlock_irqrestore(&priv->stats_lock, flags);
2135 return &priv->iwstats;
2139 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2140 static const iw_handler rndis_iw_handler[] =
2142 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2143 IW_IOCTL(SIOCGIWNAME) = rndis_iw_get_name,
2144 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2145 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2146 IW_IOCTL(SIOCSIWMODE) = rndis_iw_set_mode,
2147 IW_IOCTL(SIOCGIWMODE) = rndis_iw_get_mode,
2148 IW_IOCTL(SIOCGIWRANGE) = rndis_iw_get_range,
2149 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2150 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2151 IW_IOCTL(SIOCSIWSCAN) = rndis_iw_set_scan,
2152 IW_IOCTL(SIOCGIWSCAN) = rndis_iw_get_scan,
2153 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2154 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2155 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2156 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2157 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2158 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2159 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2160 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2161 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2162 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2163 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2164 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2165 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2166 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2167 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2168 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2169 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2170 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2173 static const iw_handler rndis_wext_private_handler[] = {
2176 static const struct iw_priv_args rndis_wext_private_args[] = {
2180 static const struct iw_handler_def rndis_iw_handlers = {
2181 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2182 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2183 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2184 .standard = (iw_handler *)rndis_iw_handler,
2185 .private = (iw_handler *)rndis_wext_private_handler,
2186 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2187 .get_wireless_stats = rndis_get_wireless_stats,
2191 static void rndis_wext_worker(struct work_struct *work)
2193 struct rndis_wext_private *priv =
2194 container_of(work, struct rndis_wext_private, work);
2195 struct usbnet *usbdev = priv->usbdev;
2196 union iwreq_data evt;
2197 unsigned char bssid[ETH_ALEN];
2200 if (test_and_clear_bit(WORK_CONNECTION_EVENT, &priv->work_pending)) {
2201 ret = get_bssid(usbdev, bssid);
2205 evt.data.length = 0;
2206 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2207 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2211 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2212 set_multicast_list(usbdev);
2215 static void rndis_wext_set_multicast_list(struct net_device *dev)
2217 struct usbnet *usbdev = dev->priv;
2218 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2220 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2221 queue_work(priv->workqueue, &priv->work);
2224 static void rndis_wext_link_change(struct usbnet *dev, int state)
2226 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2227 union iwreq_data evt;
2230 /* queue work to avoid recursive calls into rndis_command */
2231 set_bit(WORK_CONNECTION_EVENT, &priv->work_pending);
2232 queue_work(priv->workqueue, &priv->work);
2235 evt.data.length = 0;
2236 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2237 wireless_send_event(dev->net, SIOCGIWAP, &evt, NULL);
2242 static int rndis_wext_get_caps(struct usbnet *dev)
2247 } networks_supported;
2248 int len, retval, i, n;
2250 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2252 /* determine if supports setting txpower */
2253 len = sizeof(tx_power);
2254 retval = rndis_query_oid(dev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2256 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2257 priv->caps |= CAP_SUPPORT_TXPOWER;
2259 /* determine supported modes */
2260 len = sizeof(networks_supported);
2261 retval = rndis_query_oid(dev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2262 &networks_supported, &len);
2264 n = le32_to_cpu(networks_supported.num_items);
2267 for (i = 0; i < n; i++) {
2268 switch (le32_to_cpu(networks_supported.items[i])) {
2271 priv->caps |= CAP_MODE_80211B;
2273 case Ndis802_11OFDM5:
2274 priv->caps |= CAP_MODE_80211A;
2276 case Ndis802_11OFDM24:
2277 priv->caps |= CAP_MODE_80211G;
2281 if (priv->caps & CAP_MODE_80211A)
2282 strcat(priv->name, "a");
2283 if (priv->caps & CAP_MODE_80211B)
2284 strcat(priv->name, "b");
2285 if (priv->caps & CAP_MODE_80211G)
2286 strcat(priv->name, "g");
2293 #define STATS_UPDATE_JIFFIES (HZ)
2294 static void rndis_update_wireless_stats(struct work_struct *work)
2296 struct rndis_wext_private *priv =
2297 container_of(work, struct rndis_wext_private, stats_work.work);
2298 struct usbnet *usbdev = priv->usbdev;
2299 struct iw_statistics iwstats;
2302 unsigned long flags;
2303 int update_jiffies = STATS_UPDATE_JIFFIES;
2306 spin_lock_irqsave(&priv->stats_lock, flags);
2307 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2308 spin_unlock_irqrestore(&priv->stats_lock, flags);
2310 /* only update stats when connected */
2311 if (!is_associated(usbdev)) {
2312 iwstats.qual.qual = 0;
2313 iwstats.qual.level = 0;
2314 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2315 | IW_QUAL_LEVEL_UPDATED
2316 | IW_QUAL_NOISE_INVALID
2317 | IW_QUAL_QUAL_INVALID
2318 | IW_QUAL_LEVEL_INVALID;
2323 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2325 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2328 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2329 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2330 iwstats.qual.level = le32_to_cpu(rssi);
2331 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2332 | IW_QUAL_LEVEL_UPDATED
2333 | IW_QUAL_NOISE_INVALID;
2336 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2339 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2341 iwstats.discard.misc += le32_to_cpu(tmp);
2344 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2346 iwstats.discard.misc += le32_to_cpu(tmp);
2349 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2351 iwstats.discard.misc += le32_to_cpu(tmp);
2353 /* Workaround transfer stalls on poor quality links.
2354 * TODO: find right way to fix these stalls (as stalls do not happen
2355 * with ndiswrapper/windows driver). */
2356 if (iwstats.qual.qual <= 25) {
2357 /* Decrease stats worker interval to catch stalls.
2358 * faster. Faster than 400-500ms causes packet loss,
2359 * Slower doesn't catch stalls fast enough.
2361 j = msecs_to_jiffies(priv->param_workaround_interval);
2362 if (j > STATS_UPDATE_JIFFIES)
2363 j = STATS_UPDATE_JIFFIES;
2368 /* Send scan OID. Use of both OIDs is required to get device
2372 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2375 len = CONTROL_BUFFER_SIZE;
2376 buf = kmalloc(len, GFP_KERNEL);
2380 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2384 spin_lock_irqsave(&priv->stats_lock, flags);
2385 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2386 spin_unlock_irqrestore(&priv->stats_lock, flags);
2388 if (update_jiffies >= HZ)
2389 update_jiffies = round_jiffies_relative(update_jiffies);
2391 j = round_jiffies_relative(update_jiffies);
2392 if (abs(j - update_jiffies) <= 10)
2396 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2400 static int bcm4320_early_init(struct usbnet *dev)
2402 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2405 /* Early initialization settings, setting these won't have effect
2406 * if called after generic_rndis_bind().
2409 priv->param_country[0] = modparam_country[0];
2410 priv->param_country[1] = modparam_country[1];
2411 priv->param_country[2] = 0;
2412 priv->param_frameburst = modparam_frameburst;
2413 priv->param_afterburner = modparam_afterburner;
2414 priv->param_power_save = modparam_power_save;
2415 priv->param_power_output = modparam_power_output;
2416 priv->param_roamtrigger = modparam_roamtrigger;
2417 priv->param_roamdelta = modparam_roamdelta;
2418 priv->param_workaround_interval = modparam_workaround_interval;
2420 priv->param_country[0] = toupper(priv->param_country[0]);
2421 priv->param_country[1] = toupper(priv->param_country[1]);
2422 /* doesn't support EU as country code, use FI instead */
2423 if (!strcmp(priv->param_country, "EU"))
2424 strcpy(priv->param_country, "FI");
2426 if (priv->param_power_save < 0)
2427 priv->param_power_save = 0;
2428 else if (priv->param_power_save > 2)
2429 priv->param_power_save = 2;
2431 if (priv->param_roamtrigger < -80)
2432 priv->param_roamtrigger = -80;
2433 else if (priv->param_roamtrigger > -60)
2434 priv->param_roamtrigger = -60;
2436 if (priv->param_roamdelta < 0)
2437 priv->param_roamdelta = 0;
2438 else if (priv->param_roamdelta > 2)
2439 priv->param_roamdelta = 2;
2441 if (priv->param_workaround_interval < 0)
2442 priv->param_workaround_interval = 500;
2444 rndis_set_config_parameter_str(dev, "Country", priv->param_country);
2445 rndis_set_config_parameter_str(dev, "FrameBursting",
2446 priv->param_frameburst ? "1" : "0");
2447 rndis_set_config_parameter_str(dev, "Afterburner",
2448 priv->param_afterburner ? "1" : "0");
2449 sprintf(buf, "%d", priv->param_power_save);
2450 rndis_set_config_parameter_str(dev, "PowerSaveMode", buf);
2451 sprintf(buf, "%d", priv->param_power_output);
2452 rndis_set_config_parameter_str(dev, "PwrOut", buf);
2453 sprintf(buf, "%d", priv->param_roamtrigger);
2454 rndis_set_config_parameter_str(dev, "RoamTrigger", buf);
2455 sprintf(buf, "%d", priv->param_roamdelta);
2456 rndis_set_config_parameter_str(dev, "RoamDelta", buf);
2462 static int rndis_wext_bind(struct usbnet *dev, struct usb_interface *intf)
2464 struct net_device *net = dev->net;
2465 struct rndis_wext_private *priv;
2469 /* allocate rndis private data */
2470 priv = kmalloc(sizeof(struct rndis_wext_private), GFP_KERNEL);
2474 /* These have to be initialized before calling generic_rndis_bind().
2475 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2477 dev->driver_priv = priv;
2478 memset(priv, 0, sizeof(*priv));
2479 memset(priv->name, 0, sizeof(priv->name));
2480 strcpy(priv->name, "IEEE802.11");
2481 net->wireless_handlers = &rndis_iw_handlers;
2484 mutex_init(&priv->command_lock);
2485 spin_lock_init(&priv->stats_lock);
2487 /* try bind rndis_host */
2488 retval = generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2492 /* generic_rndis_bind set packet filter to multicast_all+
2493 * promisc mode which doesn't work well for our devices (device
2494 * picks up rssi to closest station instead of to access point).
2496 * rndis_host wants to avoid all OID as much as possible
2497 * so do promisc/multicast handling in rndis_wext.
2499 dev->net->set_multicast_list = rndis_wext_set_multicast_list;
2500 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2501 retval = rndis_set_oid(dev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2505 retval = rndis_query_oid(dev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp, &len);
2506 priv->multicast_size = le32_to_cpu(tmp);
2507 if (retval < 0 || priv->multicast_size < 0)
2508 priv->multicast_size = 0;
2509 if (priv->multicast_size > 0)
2510 dev->net->flags |= IFF_MULTICAST;
2512 dev->net->flags &= ~IFF_MULTICAST;
2514 priv->iwstats.qual.qual = 0;
2515 priv->iwstats.qual.level = 0;
2516 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2517 | IW_QUAL_LEVEL_UPDATED
2518 | IW_QUAL_NOISE_INVALID
2519 | IW_QUAL_QUAL_INVALID
2520 | IW_QUAL_LEVEL_INVALID;
2522 rndis_wext_get_caps(dev);
2523 set_default_iw_params(dev);
2527 disassociate(dev, 1);
2529 /* because rndis_command() sleeps we need to use workqueue */
2530 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2531 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2532 queue_delayed_work(priv->workqueue, &priv->stats_work,
2533 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2534 INIT_WORK(&priv->work, rndis_wext_worker);
2544 static void rndis_wext_unbind(struct usbnet *dev, struct usb_interface *intf)
2546 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2548 /* turn radio off */
2549 disassociate(dev, 0);
2551 cancel_delayed_work_sync(&priv->stats_work);
2552 cancel_work_sync(&priv->work);
2553 flush_workqueue(priv->workqueue);
2554 destroy_workqueue(priv->workqueue);
2556 if (priv && priv->wpa_ie_len)
2557 kfree(priv->wpa_ie);
2560 rndis_unbind(dev, intf);
2564 static int rndis_wext_reset(struct usbnet *dev)
2566 return deauthenticate(dev);
2570 static const struct driver_info bcm4320b_info = {
2571 .description = "Wireless RNDIS device, BCM4320b based",
2572 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2573 .bind = rndis_wext_bind,
2574 .unbind = rndis_wext_unbind,
2575 .status = rndis_status,
2576 .rx_fixup = rndis_rx_fixup,
2577 .tx_fixup = rndis_tx_fixup,
2578 .reset = rndis_wext_reset,
2579 .early_init = bcm4320_early_init,
2580 .link_change = rndis_wext_link_change,
2583 static const struct driver_info bcm4320a_info = {
2584 .description = "Wireless RNDIS device, BCM4320a based",
2585 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2586 .bind = rndis_wext_bind,
2587 .unbind = rndis_wext_unbind,
2588 .status = rndis_status,
2589 .rx_fixup = rndis_rx_fixup,
2590 .tx_fixup = rndis_tx_fixup,
2591 .reset = rndis_wext_reset,
2592 .early_init = bcm4320_early_init,
2593 .link_change = rndis_wext_link_change,
2596 static const struct driver_info rndis_wext_info = {
2597 .description = "Wireless RNDIS device",
2598 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2599 .bind = rndis_wext_bind,
2600 .unbind = rndis_wext_unbind,
2601 .status = rndis_status,
2602 .rx_fixup = rndis_rx_fixup,
2603 .tx_fixup = rndis_tx_fixup,
2604 .reset = rndis_wext_reset,
2605 .early_init = bcm4320_early_init,
2606 .link_change = rndis_wext_link_change,
2609 /*-------------------------------------------------------------------------*/
2611 static const struct usb_device_id products [] = {
2612 #define RNDIS_MASTER_INTERFACE \
2613 .bInterfaceClass = USB_CLASS_COMM, \
2614 .bInterfaceSubClass = 2 /* ACM */, \
2615 .bInterfaceProtocol = 0x0ff
2617 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2618 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2621 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2622 | USB_DEVICE_ID_MATCH_DEVICE,
2624 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2625 RNDIS_MASTER_INTERFACE,
2626 .driver_info = (unsigned long) &bcm4320b_info,
2628 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2629 | USB_DEVICE_ID_MATCH_DEVICE,
2631 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2632 RNDIS_MASTER_INTERFACE,
2633 .driver_info = (unsigned long) &bcm4320b_info,
2635 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2636 | USB_DEVICE_ID_MATCH_DEVICE,
2638 .idProduct = 0x011b, /* Belkin F5D7051 */
2639 RNDIS_MASTER_INTERFACE,
2640 .driver_info = (unsigned long) &bcm4320b_info,
2642 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2643 | USB_DEVICE_ID_MATCH_DEVICE,
2644 .idVendor = 0x1799, /* Belkin has two vendor ids */
2645 .idProduct = 0x011b, /* Belkin F5D7051 */
2646 RNDIS_MASTER_INTERFACE,
2647 .driver_info = (unsigned long) &bcm4320b_info,
2649 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2650 | USB_DEVICE_ID_MATCH_DEVICE,
2652 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2653 RNDIS_MASTER_INTERFACE,
2654 .driver_info = (unsigned long) &bcm4320b_info,
2656 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2657 | USB_DEVICE_ID_MATCH_DEVICE,
2659 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2660 RNDIS_MASTER_INTERFACE,
2661 .driver_info = (unsigned long) &bcm4320b_info,
2663 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2664 | USB_DEVICE_ID_MATCH_DEVICE,
2666 .idProduct = 0x1717, /* Asus WL169gE */
2667 RNDIS_MASTER_INTERFACE,
2668 .driver_info = (unsigned long) &bcm4320b_info,
2670 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2671 | USB_DEVICE_ID_MATCH_DEVICE,
2673 .idProduct = 0xd11b, /* Eminent EM4045 */
2674 RNDIS_MASTER_INTERFACE,
2675 .driver_info = (unsigned long) &bcm4320b_info,
2677 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2678 | USB_DEVICE_ID_MATCH_DEVICE,
2680 .idProduct = 0x0715, /* BT Voyager 1055 */
2681 RNDIS_MASTER_INTERFACE,
2682 .driver_info = (unsigned long) &bcm4320b_info,
2684 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2685 * parameters available, hardware probably contain older firmware version with
2686 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2689 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2690 | USB_DEVICE_ID_MATCH_DEVICE,
2692 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2693 RNDIS_MASTER_INTERFACE,
2694 .driver_info = (unsigned long) &bcm4320a_info,
2696 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2697 | USB_DEVICE_ID_MATCH_DEVICE,
2699 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2700 RNDIS_MASTER_INTERFACE,
2701 .driver_info = (unsigned long) &bcm4320a_info,
2703 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2704 | USB_DEVICE_ID_MATCH_DEVICE,
2706 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2707 RNDIS_MASTER_INTERFACE,
2708 .driver_info = (unsigned long) &bcm4320a_info,
2710 /* Generic Wireless RNDIS devices that we don't have exact
2711 * idVendor/idProduct/chip yet.
2714 /* RNDIS is MSFT's un-official variant of CDC ACM */
2715 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2716 .driver_info = (unsigned long) &rndis_wext_info,
2718 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2719 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2720 .driver_info = (unsigned long) &rndis_wext_info,
2724 MODULE_DEVICE_TABLE(usb, products);
2726 static struct usb_driver rndis_wlan_driver = {
2727 .name = "rndis_wlan",
2728 .id_table = products,
2729 .probe = usbnet_probe,
2730 .disconnect = usbnet_disconnect,
2731 .suspend = usbnet_suspend,
2732 .resume = usbnet_resume,
2735 static int __init rndis_wlan_init(void)
2737 return usb_register(&rndis_wlan_driver);
2739 module_init(rndis_wlan_init);
2741 static void __exit rndis_wlan_exit(void)
2743 usb_deregister(&rndis_wlan_driver);
2745 module_exit(rndis_wlan_exit);
2747 MODULE_AUTHOR("Bjorge Dijkstra");
2748 MODULE_AUTHOR("Jussi Kivilinna");
2749 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2750 MODULE_LICENSE("GPL");