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 enum NDIS_802_11_NETWORK_TYPE NetworkTypeInUse;
232 struct NDIS_802_11_CONFIGURATION Configuration;
233 enum NDIS_802_11_NETWORK_INFRASTRUCTURE 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_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP } wpa_alg;
283 enum { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP, CIPHER_WEP104 }
285 enum { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE, KEY_MGMT_802_1X_NO_WPA,
286 KEY_MGMT_WPA_NONE } wpa_key_mgmt;
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 for (i = 0; i < 6; i++)
1548 cpu_to_le64(ext->rx_seq[i] << (i * 8));
1549 ndis_key.KeyIndex |= cpu_to_le32(1 << 29);
1552 addr = ext->addr.sa_data;
1553 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1555 if (priv->infra_mode == Ndis802_11IBSS)
1556 memset(ndis_key.Bssid, 0xff, ETH_ALEN);
1558 get_bssid(usbdev, ndis_key.Bssid);
1561 ndis_key.KeyIndex |= cpu_to_le32(1 << 30);
1562 memcpy(ndis_key.Bssid, addr, ETH_ALEN);
1565 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1566 ndis_key.KeyIndex |= cpu_to_le32(1 << 31);
1568 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1569 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1570 * different order than NDIS spec, so swap the order here. */
1571 memcpy(ndis_key.KeyMaterial, ext->key, 16);
1572 memcpy(ndis_key.KeyMaterial + 16, ext->key + 24, 8);
1573 memcpy(ndis_key.KeyMaterial + 24, ext->key + 16, 8);
1575 memcpy(ndis_key.KeyMaterial, ext->key, ext->key_len);
1577 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1578 le32_to_cpu(ndis_key.Length));
1579 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1583 priv->encr_key_len[keyidx] = ext->key_len;
1584 memcpy(&priv->encr_keys[keyidx], ndis_key.KeyMaterial, ext->key_len);
1585 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1586 priv->encr_tx_key_index = keyidx;
1592 static int rndis_iw_set_scan(struct net_device *dev,
1593 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1595 struct iw_param *param = &wrqu->param;
1596 struct usbnet *usbdev = dev->priv;
1597 union iwreq_data evt;
1601 devdbg(usbdev, "SIOCSIWSCAN");
1603 if (param->flags == 0) {
1605 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1608 evt.data.length = 0;
1609 wireless_send_event(dev, SIOCGIWSCAN, &evt, NULL);
1615 static char *rndis_translate_scan(struct net_device *dev,
1616 char *cev, char *end_buf, struct NDIS_WLAN_BSSID_EX *bssid)
1619 struct usbnet *usbdev = dev->priv;
1621 struct ieee80211_info_element *ie;
1623 int bssid_len, ie_len, i;
1625 struct iw_event iwe;
1626 unsigned char sbuf[32];
1627 DECLARE_MAC_BUF(mac);
1629 bssid_len = le32_to_cpu(bssid->Length);
1631 devdbg(usbdev, "BSSID %s", print_mac(mac, bssid->MacAddress));
1632 iwe.cmd = SIOCGIWAP;
1633 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1634 memcpy(iwe.u.ap_addr.sa_data, bssid->MacAddress, ETH_ALEN);
1635 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_ADDR_LEN);
1637 devdbg(usbdev, "SSID(%d) %s",
1638 le32_to_cpu(bssid->Ssid.SsidLength),
1640 iwe.cmd = SIOCGIWESSID;
1641 iwe.u.essid.length = le32_to_cpu(bssid->Ssid.SsidLength);
1642 iwe.u.essid.flags = 1;
1643 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1646 devdbg(usbdev, "MODE %d",
1647 le32_to_cpu(bssid->InfrastructureMode));
1648 iwe.cmd = SIOCGIWMODE;
1649 switch (le32_to_cpu(bssid->InfrastructureMode)) {
1650 case Ndis802_11IBSS:
1651 iwe.u.mode = IW_MODE_ADHOC;
1653 case Ndis802_11Infrastructure:
1654 iwe.u.mode = IW_MODE_INFRA;
1656 /*case Ndis802_11AutoUnknown:*/
1658 iwe.u.mode = IW_MODE_AUTO;
1661 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_UINT_LEN);
1663 devdbg(usbdev, "FREQ %d kHz",
1664 le32_to_cpu(bssid->Configuration.DSConfig));
1665 iwe.cmd = SIOCGIWFREQ;
1666 dsconfig_to_freq(le32_to_cpu(bssid->Configuration.DSConfig),
1668 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_FREQ_LEN);
1670 devdbg(usbdev, "QUAL %d", le32_to_cpu(bssid->Rssi));
1672 iwe.u.qual.qual = level_to_qual(le32_to_cpu(bssid->Rssi));
1673 iwe.u.qual.level = le32_to_cpu(bssid->Rssi);
1674 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
1675 | IW_QUAL_LEVEL_UPDATED
1676 | IW_QUAL_NOISE_INVALID;
1677 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_QUAL_LEN);
1679 devdbg(usbdev, "ENCODE %d", le32_to_cpu(bssid->Privacy));
1680 iwe.cmd = SIOCGIWENCODE;
1681 iwe.u.data.length = 0;
1682 if (le32_to_cpu(bssid->Privacy) == Ndis802_11PrivFilterAcceptAll)
1683 iwe.u.data.flags = IW_ENCODE_DISABLED;
1685 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1687 cev = iwe_stream_add_point(cev, end_buf, &iwe, NULL);
1689 devdbg(usbdev, "RATES:");
1690 current_val = cev + IW_EV_LCP_LEN;
1691 iwe.cmd = SIOCGIWRATE;
1692 for (i = 0; i < sizeof(bssid->SupportedRates); i++) {
1693 if (bssid->SupportedRates[i] & 0x7f) {
1694 iwe.u.bitrate.value =
1695 ((bssid->SupportedRates[i] & 0x7f) *
1697 devdbg(usbdev, " %d", iwe.u.bitrate.value);
1698 current_val = iwe_stream_add_value(cev,
1699 current_val, end_buf, &iwe,
1704 if ((current_val - cev) > IW_EV_LCP_LEN)
1707 beacon = le32_to_cpu(bssid->Configuration.BeaconPeriod);
1708 devdbg(usbdev, "BCN_INT %d", beacon);
1709 iwe.cmd = IWEVCUSTOM;
1710 snprintf(sbuf, sizeof(sbuf), "bcn_int=%d", beacon);
1711 iwe.u.data.length = strlen(sbuf);
1712 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1714 atim = le32_to_cpu(bssid->Configuration.ATIMWindow);
1715 devdbg(usbdev, "ATIM %d", atim);
1716 iwe.cmd = IWEVCUSTOM;
1717 snprintf(sbuf, sizeof(sbuf), "atim=%u", atim);
1718 iwe.u.data.length = strlen(sbuf);
1719 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1721 ie = (void *)(bssid->IEs + sizeof(struct NDIS_802_11_FIXED_IEs));
1722 ie_len = min(bssid_len - (int)sizeof(*bssid),
1723 (int)le32_to_cpu(bssid->IELength));
1724 ie_len -= sizeof(struct NDIS_802_11_FIXED_IEs);
1725 while (ie_len >= sizeof(*ie) && sizeof(*ie) + ie->len <= ie_len) {
1726 if ((ie->id == MFIE_TYPE_GENERIC && ie->len >= 4 &&
1727 memcmp(ie->data, "\x00\x50\xf2\x01", 4) == 0) ||
1728 ie->id == MFIE_TYPE_RSN) {
1729 devdbg(usbdev, "IE: WPA%d",
1730 (ie->id == MFIE_TYPE_RSN) ? 2 : 1);
1731 iwe.cmd = IWEVGENIE;
1732 iwe.u.data.length = min(ie->len + 2, MAX_WPA_IE_LEN);
1733 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1737 ie_len -= sizeof(*ie) + ie->len;
1738 ie = (struct ieee80211_info_element *)&ie->data[ie->len];
1745 static int rndis_iw_get_scan(struct net_device *dev,
1746 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1748 struct usbnet *usbdev = dev->priv;
1751 struct NDIS_802_11_BSSID_LIST_EX *bssid_list;
1752 struct NDIS_WLAN_BSSID_EX *bssid;
1753 int ret = -EINVAL, len, count, bssid_len;
1755 devdbg(usbdev, "SIOCGIWSCAN");
1757 len = CONTROL_BUFFER_SIZE;
1758 buf = kmalloc(len, GFP_KERNEL);
1764 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1770 bssid = bssid_list->Bssid;
1771 bssid_len = le32_to_cpu(bssid->Length);
1772 count = le32_to_cpu(bssid_list->NumberOfItems);
1773 devdbg(usbdev, "SIOCGIWSCAN: %d BSSIDs found", count);
1775 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1776 cev = rndis_translate_scan(dev, cev, extra + IW_SCAN_MAX_DATA,
1778 bssid = (void *)bssid + bssid_len;
1779 bssid_len = le32_to_cpu(bssid->Length);
1784 wrqu->data.length = cev - extra;
1785 wrqu->data.flags = 0;
1791 static int rndis_iw_set_genie(struct net_device *dev,
1792 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1794 struct usbnet *usbdev = dev->priv;
1795 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1801 for (j = 0; j < wrqu->data.length; j += 8)
1803 "SIOCSIWGENIE %04x - "
1804 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1805 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1806 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1808 /* clear existing IEs */
1809 if (priv->wpa_ie_len) {
1810 kfree(priv->wpa_ie);
1811 priv->wpa_ie_len = 0;
1815 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1817 priv->wpa_ie_len = wrqu->data.length;
1818 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1825 static int rndis_iw_get_genie(struct net_device *dev,
1826 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1828 struct usbnet *usbdev = dev->priv;
1829 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1831 devdbg(usbdev, "SIOCGIWGENIE");
1833 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1834 wrqu->data.length = 0;
1838 if (wrqu->data.length < priv->wpa_ie_len)
1841 wrqu->data.length = priv->wpa_ie_len;
1842 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1848 static int rndis_iw_set_rts(struct net_device *dev,
1849 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1851 struct usbnet *usbdev = dev->priv;
1853 devdbg(usbdev, "SIOCSIWRTS");
1855 tmp = cpu_to_le32(wrqu->rts.value);
1856 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1861 static int rndis_iw_get_rts(struct net_device *dev,
1862 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1864 struct usbnet *usbdev = dev->priv;
1869 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1871 wrqu->rts.value = le32_to_cpu(tmp);
1872 wrqu->rts.flags = 1;
1873 wrqu->rts.disabled = 0;
1876 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1882 static int rndis_iw_set_frag(struct net_device *dev,
1883 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1885 struct usbnet *usbdev = dev->priv;
1888 devdbg(usbdev, "SIOCSIWFRAG");
1890 tmp = cpu_to_le32(wrqu->frag.value);
1891 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1896 static int rndis_iw_get_frag(struct net_device *dev,
1897 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1899 struct usbnet *usbdev = dev->priv;
1904 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1907 wrqu->frag.value = le32_to_cpu(tmp);
1908 wrqu->frag.flags = 1;
1909 wrqu->frag.disabled = 0;
1911 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1916 static int rndis_iw_set_nick(struct net_device *dev,
1917 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1919 struct usbnet *usbdev = dev->priv;
1920 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1922 devdbg(usbdev, "SIOCSIWNICK");
1924 priv->nick_len = wrqu->data.length;
1925 if (priv->nick_len > 32)
1926 priv->nick_len = 32;
1928 memcpy(priv->nick, extra, priv->nick_len);
1933 static int rndis_iw_get_nick(struct net_device *dev,
1934 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1936 struct usbnet *usbdev = dev->priv;
1937 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1939 wrqu->data.flags = 1;
1940 wrqu->data.length = priv->nick_len;
1941 memcpy(extra, priv->nick, priv->nick_len);
1943 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1949 static int rndis_iw_set_freq(struct net_device *dev,
1950 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1952 struct usbnet *usbdev = dev->priv;
1953 struct NDIS_802_11_CONFIGURATION config;
1954 unsigned int dsconfig;
1957 /* this OID is valid only when not associated */
1958 if (is_associated(usbdev))
1962 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1965 len = sizeof(config);
1966 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1968 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1972 config.DSConfig = cpu_to_le32(dsconfig);
1974 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1975 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1980 static int rndis_iw_get_freq(struct net_device *dev,
1981 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1983 struct usbnet *usbdev = dev->priv;
1984 struct NDIS_802_11_CONFIGURATION config;
1987 len = sizeof(config);
1988 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1990 dsconfig_to_freq(le32_to_cpu(config.DSConfig), &wrqu->freq);
1992 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
1997 static int rndis_iw_get_txpower(struct net_device *dev,
1998 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2000 struct usbnet *usbdev = dev->priv;
2001 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2005 if (priv->radio_on) {
2006 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2007 len = sizeof(tx_power);
2008 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2013 /* fake incase not supported */
2014 tx_power = cpu_to_le32(get_bcm4320_power(priv));
2016 wrqu->txpower.flags = IW_TXPOW_MWATT;
2017 wrqu->txpower.value = le32_to_cpu(tx_power);
2018 wrqu->txpower.disabled = 0;
2020 wrqu->txpower.flags = IW_TXPOW_MWATT;
2021 wrqu->txpower.value = 0;
2022 wrqu->txpower.disabled = 1;
2025 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
2031 static int rndis_iw_set_txpower(struct net_device *dev,
2032 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2034 struct usbnet *usbdev = dev->priv;
2035 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2036 __le32 tx_power = 0;
2039 if (!wrqu->txpower.disabled) {
2040 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
2041 tx_power = cpu_to_le32(wrqu->txpower.value);
2042 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
2043 if (wrqu->txpower.value > 20)
2044 tx_power = cpu_to_le32(128);
2045 else if (wrqu->txpower.value < -43)
2046 tx_power = cpu_to_le32(127);
2049 tmp = wrqu->txpower.value;
2052 tx_power = cpu_to_le32((unsigned char)tmp);
2057 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
2059 if (le32_to_cpu(tx_power) != 0) {
2060 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2061 /* turn radio on first */
2062 if (!priv->radio_on)
2063 disassociate(usbdev, 1);
2065 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2066 &tx_power, sizeof(tx_power));
2071 /* txpower unsupported, just turn radio on */
2072 if (!priv->radio_on)
2073 return disassociate(usbdev, 1);
2074 return 0; /* all ready on */
2078 /* tx_power == 0, turn off radio */
2079 return disassociate(usbdev, 0);
2083 static int rndis_iw_get_rate(struct net_device *dev,
2084 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2086 struct usbnet *usbdev = dev->priv;
2091 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2093 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2094 wrqu->bitrate.disabled = 0;
2095 wrqu->bitrate.flags = 1;
2101 static int rndis_iw_set_mlme(struct net_device *dev,
2102 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2104 struct usbnet *usbdev = dev->priv;
2105 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2106 struct iw_mlme *mlme = (struct iw_mlme *)extra;
2107 unsigned char bssid[ETH_ALEN];
2109 get_bssid(usbdev, bssid);
2111 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2114 switch (mlme->cmd) {
2115 case IW_MLME_DEAUTH:
2116 return deauthenticate(usbdev);
2117 case IW_MLME_DISASSOC:
2118 return disassociate(usbdev, priv->radio_on);
2127 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2129 struct usbnet *usbdev = dev->priv;
2130 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2131 unsigned long flags;
2133 spin_lock_irqsave(&priv->stats_lock, flags);
2134 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2135 spin_unlock_irqrestore(&priv->stats_lock, flags);
2137 return &priv->iwstats;
2141 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2142 static const iw_handler rndis_iw_handler[] =
2144 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2145 IW_IOCTL(SIOCGIWNAME) = rndis_iw_get_name,
2146 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2147 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2148 IW_IOCTL(SIOCSIWMODE) = rndis_iw_set_mode,
2149 IW_IOCTL(SIOCGIWMODE) = rndis_iw_get_mode,
2150 IW_IOCTL(SIOCGIWRANGE) = rndis_iw_get_range,
2151 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2152 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2153 IW_IOCTL(SIOCSIWSCAN) = rndis_iw_set_scan,
2154 IW_IOCTL(SIOCGIWSCAN) = rndis_iw_get_scan,
2155 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2156 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2157 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2158 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2159 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2160 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2161 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2162 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2163 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2164 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2165 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2166 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2167 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2168 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2169 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2170 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2171 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2172 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2175 static const iw_handler rndis_wext_private_handler[] = {
2178 static const struct iw_priv_args rndis_wext_private_args[] = {
2182 static const struct iw_handler_def rndis_iw_handlers = {
2183 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2184 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2185 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2186 .standard = (iw_handler *)rndis_iw_handler,
2187 .private = (iw_handler *)rndis_wext_private_handler,
2188 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2189 .get_wireless_stats = rndis_get_wireless_stats,
2193 static void rndis_wext_worker(struct work_struct *work)
2195 struct rndis_wext_private *priv =
2196 container_of(work, struct rndis_wext_private, work);
2197 struct usbnet *usbdev = priv->usbdev;
2198 union iwreq_data evt;
2199 unsigned char bssid[ETH_ALEN];
2202 if (test_and_clear_bit(WORK_CONNECTION_EVENT, &priv->work_pending)) {
2203 ret = get_bssid(usbdev, bssid);
2207 evt.data.length = 0;
2208 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2209 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2213 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2214 set_multicast_list(usbdev);
2217 static void rndis_wext_set_multicast_list(struct net_device *dev)
2219 struct usbnet *usbdev = dev->priv;
2220 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2222 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2223 queue_work(priv->workqueue, &priv->work);
2226 static void rndis_wext_link_change(struct usbnet *dev, int state)
2228 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2229 union iwreq_data evt;
2232 /* queue work to avoid recursive calls into rndis_command */
2233 set_bit(WORK_CONNECTION_EVENT, &priv->work_pending);
2234 queue_work(priv->workqueue, &priv->work);
2237 evt.data.length = 0;
2238 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2239 wireless_send_event(dev->net, SIOCGIWAP, &evt, NULL);
2244 static int rndis_wext_get_caps(struct usbnet *dev)
2249 } networks_supported;
2250 int len, retval, i, n;
2252 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2254 /* determine if supports setting txpower */
2255 len = sizeof(tx_power);
2256 retval = rndis_query_oid(dev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2258 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2259 priv->caps |= CAP_SUPPORT_TXPOWER;
2261 /* determine supported modes */
2262 len = sizeof(networks_supported);
2263 retval = rndis_query_oid(dev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2264 &networks_supported, &len);
2266 n = le32_to_cpu(networks_supported.num_items);
2269 for (i = 0; i < n; i++) {
2270 switch (le32_to_cpu(networks_supported.items[i])) {
2273 priv->caps |= CAP_MODE_80211B;
2275 case Ndis802_11OFDM5:
2276 priv->caps |= CAP_MODE_80211A;
2278 case Ndis802_11OFDM24:
2279 priv->caps |= CAP_MODE_80211G;
2283 if (priv->caps & CAP_MODE_80211A)
2284 strcat(priv->name, "a");
2285 if (priv->caps & CAP_MODE_80211B)
2286 strcat(priv->name, "b");
2287 if (priv->caps & CAP_MODE_80211G)
2288 strcat(priv->name, "g");
2295 #define STATS_UPDATE_JIFFIES (HZ)
2296 static void rndis_update_wireless_stats(struct work_struct *work)
2298 struct rndis_wext_private *priv =
2299 container_of(work, struct rndis_wext_private, stats_work.work);
2300 struct usbnet *usbdev = priv->usbdev;
2301 struct iw_statistics iwstats;
2303 int len, ret, bitrate, j;
2304 unsigned long flags;
2305 int update_jiffies = STATS_UPDATE_JIFFIES;
2308 spin_lock_irqsave(&priv->stats_lock, flags);
2309 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2310 spin_unlock_irqrestore(&priv->stats_lock, flags);
2312 /* only update stats when connected */
2313 if (!is_associated(usbdev)) {
2314 iwstats.qual.qual = 0;
2315 iwstats.qual.level = 0;
2316 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2317 | IW_QUAL_LEVEL_UPDATED
2318 | IW_QUAL_NOISE_INVALID
2319 | IW_QUAL_QUAL_INVALID
2320 | IW_QUAL_LEVEL_INVALID;
2325 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2327 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2330 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2331 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2332 iwstats.qual.level = le32_to_cpu(rssi);
2333 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2334 | IW_QUAL_LEVEL_UPDATED
2335 | IW_QUAL_NOISE_INVALID;
2338 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2341 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2343 iwstats.discard.misc += le32_to_cpu(tmp);
2346 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2348 iwstats.discard.misc += le32_to_cpu(tmp);
2351 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2353 iwstats.discard.misc += le32_to_cpu(tmp);
2355 /* Workaround transfer stalls on poor quality links. */
2357 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2359 bitrate = le32_to_cpu(tmp) * 100;
2360 if (bitrate > 11000000)
2363 /* Decrease stats worker interval to catch stalls.
2364 * faster. Faster than 400-500ms causes packet loss,
2365 * Slower doesn't catch stalls fast enough.
2367 j = msecs_to_jiffies(priv->param_workaround_interval);
2368 if (j > STATS_UPDATE_JIFFIES)
2369 j = STATS_UPDATE_JIFFIES;
2374 /* Send scan OID. Use of both OIDs is required to get device
2378 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2381 len = CONTROL_BUFFER_SIZE;
2382 buf = kmalloc(len, GFP_KERNEL);
2386 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2390 spin_lock_irqsave(&priv->stats_lock, flags);
2391 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2392 spin_unlock_irqrestore(&priv->stats_lock, flags);
2394 if (update_jiffies >= HZ)
2395 update_jiffies = round_jiffies_relative(update_jiffies);
2397 j = round_jiffies_relative(update_jiffies);
2398 if (abs(j - update_jiffies) <= 10)
2402 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2406 static int bcm4320_early_init(struct usbnet *dev)
2408 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2411 /* Early initialization settings, setting these won't have effect
2412 * if called after generic_rndis_bind().
2415 priv->param_country[0] = modparam_country[0];
2416 priv->param_country[1] = modparam_country[1];
2417 priv->param_country[2] = 0;
2418 priv->param_frameburst = modparam_frameburst;
2419 priv->param_afterburner = modparam_afterburner;
2420 priv->param_power_save = modparam_power_save;
2421 priv->param_power_output = modparam_power_output;
2422 priv->param_roamtrigger = modparam_roamtrigger;
2423 priv->param_roamdelta = modparam_roamdelta;
2424 priv->param_workaround_interval = modparam_workaround_interval;
2426 priv->param_country[0] = toupper(priv->param_country[0]);
2427 priv->param_country[1] = toupper(priv->param_country[1]);
2428 /* doesn't support EU as country code, use FI instead */
2429 if (!strcmp(priv->param_country, "EU"))
2430 strcpy(priv->param_country, "FI");
2432 if (priv->param_power_save < 0)
2433 priv->param_power_save = 0;
2434 else if (priv->param_power_save > 2)
2435 priv->param_power_save = 2;
2437 if (priv->param_roamtrigger < -80)
2438 priv->param_roamtrigger = -80;
2439 else if (priv->param_roamtrigger > -60)
2440 priv->param_roamtrigger = -60;
2442 if (priv->param_roamdelta < 0)
2443 priv->param_roamdelta = 0;
2444 else if (priv->param_roamdelta > 2)
2445 priv->param_roamdelta = 2;
2447 if (priv->param_workaround_interval < 0)
2448 priv->param_workaround_interval = 500;
2450 rndis_set_config_parameter_str(dev, "Country", priv->param_country);
2451 rndis_set_config_parameter_str(dev, "FrameBursting",
2452 priv->param_frameburst ? "1" : "0");
2453 rndis_set_config_parameter_str(dev, "Afterburner",
2454 priv->param_afterburner ? "1" : "0");
2455 sprintf(buf, "%d", priv->param_power_save);
2456 rndis_set_config_parameter_str(dev, "PowerSaveMode", buf);
2457 sprintf(buf, "%d", priv->param_power_output);
2458 rndis_set_config_parameter_str(dev, "PwrOut", buf);
2459 sprintf(buf, "%d", priv->param_roamtrigger);
2460 rndis_set_config_parameter_str(dev, "RoamTrigger", buf);
2461 sprintf(buf, "%d", priv->param_roamdelta);
2462 rndis_set_config_parameter_str(dev, "RoamDelta", buf);
2468 static int rndis_wext_bind(struct usbnet *dev, struct usb_interface *intf)
2470 struct net_device *net = dev->net;
2471 struct rndis_wext_private *priv;
2475 /* allocate rndis private data */
2476 priv = kmalloc(sizeof(struct rndis_wext_private), GFP_KERNEL);
2480 /* These have to be initialized before calling generic_rndis_bind().
2481 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2483 dev->driver_priv = priv;
2484 memset(priv, 0, sizeof(*priv));
2485 memset(priv->name, 0, sizeof(priv->name));
2486 strcpy(priv->name, "IEEE802.11");
2487 net->wireless_handlers = &rndis_iw_handlers;
2490 mutex_init(&priv->command_lock);
2491 spin_lock_init(&priv->stats_lock);
2493 /* try bind rndis_host */
2494 retval = generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2498 /* generic_rndis_bind set packet filter to multicast_all+
2499 * promisc mode which doesn't work well for our devices (device
2500 * picks up rssi to closest station instead of to access point).
2502 * rndis_host wants to avoid all OID as much as possible
2503 * so do promisc/multicast handling in rndis_wext.
2505 dev->net->set_multicast_list = rndis_wext_set_multicast_list;
2506 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2507 retval = rndis_set_oid(dev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2511 retval = rndis_query_oid(dev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp, &len);
2512 priv->multicast_size = le32_to_cpu(tmp);
2513 if (retval < 0 || priv->multicast_size < 0)
2514 priv->multicast_size = 0;
2515 if (priv->multicast_size > 0)
2516 dev->net->flags |= IFF_MULTICAST;
2518 dev->net->flags &= ~IFF_MULTICAST;
2520 priv->iwstats.qual.qual = 0;
2521 priv->iwstats.qual.level = 0;
2522 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2523 | IW_QUAL_LEVEL_UPDATED
2524 | IW_QUAL_NOISE_INVALID
2525 | IW_QUAL_QUAL_INVALID
2526 | IW_QUAL_LEVEL_INVALID;
2528 rndis_wext_get_caps(dev);
2529 set_default_iw_params(dev);
2533 disassociate(dev, 1);
2535 /* because rndis_command() sleeps we need to use workqueue */
2536 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2537 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2538 queue_delayed_work(priv->workqueue, &priv->stats_work,
2539 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2540 INIT_WORK(&priv->work, rndis_wext_worker);
2550 static void rndis_wext_unbind(struct usbnet *dev, struct usb_interface *intf)
2552 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2554 /* turn radio off */
2555 disassociate(dev, 0);
2557 cancel_delayed_work_sync(&priv->stats_work);
2558 cancel_work_sync(&priv->work);
2559 flush_workqueue(priv->workqueue);
2560 destroy_workqueue(priv->workqueue);
2562 if (priv && priv->wpa_ie_len)
2563 kfree(priv->wpa_ie);
2566 rndis_unbind(dev, intf);
2570 static int rndis_wext_reset(struct usbnet *dev)
2572 return deauthenticate(dev);
2576 static const struct driver_info bcm4320b_info = {
2577 .description = "Wireless RNDIS device, BCM4320b based",
2578 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2579 .bind = rndis_wext_bind,
2580 .unbind = rndis_wext_unbind,
2581 .status = rndis_status,
2582 .rx_fixup = rndis_rx_fixup,
2583 .tx_fixup = rndis_tx_fixup,
2584 .reset = rndis_wext_reset,
2585 .early_init = bcm4320_early_init,
2586 .link_change = rndis_wext_link_change,
2589 static const struct driver_info bcm4320a_info = {
2590 .description = "Wireless RNDIS device, BCM4320a based",
2591 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2592 .bind = rndis_wext_bind,
2593 .unbind = rndis_wext_unbind,
2594 .status = rndis_status,
2595 .rx_fixup = rndis_rx_fixup,
2596 .tx_fixup = rndis_tx_fixup,
2597 .reset = rndis_wext_reset,
2598 .early_init = bcm4320_early_init,
2599 .link_change = rndis_wext_link_change,
2602 static const struct driver_info rndis_wext_info = {
2603 .description = "Wireless RNDIS device",
2604 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2605 .bind = rndis_wext_bind,
2606 .unbind = rndis_wext_unbind,
2607 .status = rndis_status,
2608 .rx_fixup = rndis_rx_fixup,
2609 .tx_fixup = rndis_tx_fixup,
2610 .reset = rndis_wext_reset,
2611 .early_init = bcm4320_early_init,
2612 .link_change = rndis_wext_link_change,
2615 /*-------------------------------------------------------------------------*/
2617 static const struct usb_device_id products [] = {
2618 #define RNDIS_MASTER_INTERFACE \
2619 .bInterfaceClass = USB_CLASS_COMM, \
2620 .bInterfaceSubClass = 2 /* ACM */, \
2621 .bInterfaceProtocol = 0x0ff
2623 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2624 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2627 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2628 | USB_DEVICE_ID_MATCH_DEVICE,
2630 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2631 RNDIS_MASTER_INTERFACE,
2632 .driver_info = (unsigned long) &bcm4320b_info,
2634 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2635 | USB_DEVICE_ID_MATCH_DEVICE,
2637 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2638 RNDIS_MASTER_INTERFACE,
2639 .driver_info = (unsigned long) &bcm4320b_info,
2641 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2642 | USB_DEVICE_ID_MATCH_DEVICE,
2644 .idProduct = 0x011b, /* Belkin F5D7051 */
2645 RNDIS_MASTER_INTERFACE,
2646 .driver_info = (unsigned long) &bcm4320b_info,
2648 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2649 | USB_DEVICE_ID_MATCH_DEVICE,
2650 .idVendor = 0x1799, /* Belkin has two vendor ids */
2651 .idProduct = 0x011b, /* Belkin F5D7051 */
2652 RNDIS_MASTER_INTERFACE,
2653 .driver_info = (unsigned long) &bcm4320b_info,
2655 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2656 | USB_DEVICE_ID_MATCH_DEVICE,
2658 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2659 RNDIS_MASTER_INTERFACE,
2660 .driver_info = (unsigned long) &bcm4320b_info,
2662 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2663 | USB_DEVICE_ID_MATCH_DEVICE,
2665 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2666 RNDIS_MASTER_INTERFACE,
2667 .driver_info = (unsigned long) &bcm4320b_info,
2669 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2670 | USB_DEVICE_ID_MATCH_DEVICE,
2672 .idProduct = 0x1717, /* Asus WL169gE */
2673 RNDIS_MASTER_INTERFACE,
2674 .driver_info = (unsigned long) &bcm4320b_info,
2676 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2677 | USB_DEVICE_ID_MATCH_DEVICE,
2679 .idProduct = 0xd11b, /* Eminent EM4045 */
2680 RNDIS_MASTER_INTERFACE,
2681 .driver_info = (unsigned long) &bcm4320b_info,
2683 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2684 | USB_DEVICE_ID_MATCH_DEVICE,
2686 .idProduct = 0x0715, /* BT Voyager 1055 */
2687 RNDIS_MASTER_INTERFACE,
2688 .driver_info = (unsigned long) &bcm4320b_info,
2690 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2691 * parameters available, hardware probably contain older firmware version with
2692 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2695 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2696 | USB_DEVICE_ID_MATCH_DEVICE,
2698 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2699 RNDIS_MASTER_INTERFACE,
2700 .driver_info = (unsigned long) &bcm4320a_info,
2702 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2703 | USB_DEVICE_ID_MATCH_DEVICE,
2705 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2706 RNDIS_MASTER_INTERFACE,
2707 .driver_info = (unsigned long) &bcm4320a_info,
2709 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2710 | USB_DEVICE_ID_MATCH_DEVICE,
2712 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2713 RNDIS_MASTER_INTERFACE,
2714 .driver_info = (unsigned long) &bcm4320a_info,
2716 /* Generic Wireless RNDIS devices that we don't have exact
2717 * idVendor/idProduct/chip yet.
2720 /* RNDIS is MSFT's un-official variant of CDC ACM */
2721 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2722 .driver_info = (unsigned long) &rndis_wext_info,
2724 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2725 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2726 .driver_info = (unsigned long) &rndis_wext_info,
2730 MODULE_DEVICE_TABLE(usb, products);
2732 static struct usb_driver rndis_wlan_driver = {
2733 .name = "rndis_wlan",
2734 .id_table = products,
2735 .probe = usbnet_probe,
2736 .disconnect = usbnet_disconnect,
2737 .suspend = usbnet_suspend,
2738 .resume = usbnet_resume,
2741 static int __init rndis_wlan_init(void)
2743 return usb_register(&rndis_wlan_driver);
2745 module_init(rndis_wlan_init);
2747 static void __exit rndis_wlan_exit(void)
2749 usb_deregister(&rndis_wlan_driver);
2751 module_exit(rndis_wlan_exit);
2753 MODULE_AUTHOR("Bjorge Dijkstra");
2754 MODULE_AUTHOR("Jussi Kivilinna");
2755 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2756 MODULE_LICENSE("GPL");