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_ASSOCIATION_INFORMATION ccpu2(0x0d01011f)
120 #define OID_802_11_PMKID ccpu2(0x0d010123)
121 #define OID_802_11_NETWORK_TYPES_SUPPORTED ccpu2(0x0d010203)
122 #define OID_802_11_NETWORK_TYPE_IN_USE ccpu2(0x0d010204)
123 #define OID_802_11_TX_POWER_LEVEL ccpu2(0x0d010205)
124 #define OID_802_11_RSSI ccpu2(0x0d010206)
125 #define OID_802_11_RSSI_TRIGGER ccpu2(0x0d010207)
126 #define OID_802_11_FRAGMENTATION_THRESHOLD ccpu2(0x0d010209)
127 #define OID_802_11_RTS_THRESHOLD ccpu2(0x0d01020a)
128 #define OID_802_11_SUPPORTED_RATES ccpu2(0x0d01020e)
129 #define OID_802_11_CONFIGURATION ccpu2(0x0d010211)
130 #define OID_802_11_BSSID_LIST ccpu2(0x0d010217)
133 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
134 #define WL_NOISE -96 /* typical noise level in dBm */
135 #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
138 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
139 * based on wireless rndis) has default txpower of 13dBm.
140 * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
143 #define BCM4320_DEFAULT_TXPOWER 20
146 /* codes for "status" field of completion messages */
147 #define RNDIS_STATUS_ADAPTER_NOT_READY ccpu2(0xc0010011)
148 #define RNDIS_STATUS_ADAPTER_NOT_OPEN ccpu2(0xc0010012)
151 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
152 * slightly modified for datatype endianess, etc
154 #define NDIS_802_11_LENGTH_SSID 32
155 #define NDIS_802_11_LENGTH_RATES 8
156 #define NDIS_802_11_LENGTH_RATES_EX 16
158 enum ndis_80211_net_type {
159 ndis_80211_type_freq_hop,
160 ndis_80211_type_direct_seq,
161 ndis_80211_type_ofdm_a,
162 ndis_80211_type_ofdm_g
165 enum ndis_80211_net_infra {
166 ndis_80211_infra_adhoc,
167 ndis_80211_infra_infra,
168 ndis_80211_infra_auto_unknown
171 enum ndis_80211_auth_mode {
172 ndis_80211_auth_open,
173 ndis_80211_auth_shared,
174 ndis_80211_auth_auto_switch,
176 ndis_80211_auth_wpa_psk,
177 ndis_80211_auth_wpa_none,
178 ndis_80211_auth_wpa2,
179 ndis_80211_auth_wpa2_psk
182 enum ndis_80211_encr_status {
183 ndis_80211_encr_wep_enabled,
184 ndis_80211_encr_disabled,
185 ndis_80211_encr_wep_key_absent,
186 ndis_80211_encr_not_supported,
187 ndis_80211_encr_tkip_enabled,
188 ndis_80211_encr_tkip_key_absent,
189 ndis_80211_encr_ccmp_enabled,
190 ndis_80211_encr_ccmp_key_absent
193 enum ndis_80211_priv_filter {
194 ndis_80211_priv_accept_all,
195 ndis_80211_priv_8021x_wep
198 struct ndis_80211_ssid {
200 u8 essid[NDIS_802_11_LENGTH_SSID];
201 } __attribute__((packed));
203 struct ndis_80211_conf_freq_hop {
208 } __attribute__((packed));
210 struct ndis_80211_conf {
212 __le32 beacon_period;
215 struct ndis_80211_conf_freq_hop fh_config;
216 } __attribute__((packed));
218 struct ndis_80211_bssid_ex {
222 struct ndis_80211_ssid ssid;
226 struct ndis_80211_conf config;
228 u8 rates[NDIS_802_11_LENGTH_RATES_EX];
231 } __attribute__((packed));
233 struct ndis_80211_bssid_list_ex {
235 struct ndis_80211_bssid_ex bssid[0];
236 } __attribute__((packed));
238 struct ndis_80211_fixed_ies {
240 __le16 beacon_interval;
242 } __attribute__((packed));
244 struct ndis_80211_wep_key {
249 } __attribute__((packed));
251 struct ndis_80211_key {
259 } __attribute__((packed));
261 struct ndis_80211_remove_key {
265 } __attribute__((packed));
267 struct ndis_config_param {
273 } __attribute__((packed));
275 struct ndis_80211_assoc_info {
280 __le16 listen_interval;
281 u8 cur_ap_address[6];
283 __le32 req_ie_length;
284 __le32 offset_req_ies;
291 __le32 resp_ie_length;
292 __le32 offset_resp_ies;
293 } __attribute__((packed));
295 /* these have to match what is in wpa_supplicant */
296 enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
297 enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
299 enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
300 KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
305 #define NET_TYPE_11FB 0
307 #define CAP_MODE_80211A 1
308 #define CAP_MODE_80211B 2
309 #define CAP_MODE_80211G 4
310 #define CAP_MODE_MASK 7
311 #define CAP_SUPPORT_TXPOWER 8
313 #define WORK_CONNECTION_EVENT (1<<0)
314 #define WORK_SET_MULTICAST_LIST (1<<1)
316 /* RNDIS device private data */
317 struct rndis_wext_private {
320 struct usbnet *usbdev;
322 struct workqueue_struct *workqueue;
323 struct delayed_work stats_work;
324 struct work_struct work;
325 struct mutex command_lock;
326 spinlock_t stats_lock;
327 unsigned long work_pending;
329 struct iw_statistics iwstats;
330 struct iw_statistics privstats;
338 /* module parameters */
339 char param_country[4];
340 int param_frameburst;
341 int param_afterburner;
342 int param_power_save;
343 int param_power_output;
344 int param_roamtrigger;
346 u32 param_workaround_interval;
351 struct ndis_80211_ssid essid;
353 /* encryption stuff */
354 int encr_tx_key_index;
355 char encr_keys[4][32];
363 int wpa_cipher_group;
367 static const int freq_chan[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
368 2447, 2452, 2457, 2462, 2467, 2472, 2484 };
370 static const int rates_80211g[8] = { 6, 9, 12, 18, 24, 36, 48, 54 };
372 static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
374 static const unsigned char zero_bssid[ETH_ALEN] = {0,};
375 static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
379 static struct rndis_wext_private *get_rndis_wext_priv(struct usbnet *dev)
381 return (struct rndis_wext_private *)dev->driver_priv;
385 static u32 get_bcm4320_power(struct rndis_wext_private *priv)
387 return BCM4320_DEFAULT_TXPOWER *
388 bcm4320_power_output[priv->param_power_output] / 100;
392 /* translate error code */
393 static int rndis_error_status(__le32 rndis_status)
396 switch (rndis_status) {
397 case RNDIS_STATUS_SUCCESS:
400 case RNDIS_STATUS_FAILURE:
401 case RNDIS_STATUS_INVALID_DATA:
404 case RNDIS_STATUS_NOT_SUPPORTED:
407 case RNDIS_STATUS_ADAPTER_NOT_READY:
408 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
416 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
418 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
421 struct rndis_msg_hdr *header;
422 struct rndis_query *get;
423 struct rndis_query_c *get_c;
427 buflen = *len + sizeof(*u.get);
428 if (buflen < CONTROL_BUFFER_SIZE)
429 buflen = CONTROL_BUFFER_SIZE;
430 u.buf = kmalloc(buflen, GFP_KERNEL);
433 memset(u.get, 0, sizeof *u.get);
434 u.get->msg_type = RNDIS_MSG_QUERY;
435 u.get->msg_len = ccpu2(sizeof *u.get);
438 mutex_lock(&priv->command_lock);
439 ret = rndis_command(dev, u.header);
440 mutex_unlock(&priv->command_lock);
443 ret = le32_to_cpu(u.get_c->len);
444 *len = (*len > ret) ? ret : *len;
445 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
446 ret = rndis_error_status(u.get_c->status);
454 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
456 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
459 struct rndis_msg_hdr *header;
460 struct rndis_set *set;
461 struct rndis_set_c *set_c;
465 buflen = len + sizeof(*u.set);
466 if (buflen < CONTROL_BUFFER_SIZE)
467 buflen = CONTROL_BUFFER_SIZE;
468 u.buf = kmalloc(buflen, GFP_KERNEL);
472 memset(u.set, 0, sizeof *u.set);
473 u.set->msg_type = RNDIS_MSG_SET;
474 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
476 u.set->len = cpu_to_le32(len);
477 u.set->offset = ccpu2(sizeof(*u.set) - 8);
478 u.set->handle = ccpu2(0);
479 memcpy(u.buf + sizeof(*u.set), data, len);
481 mutex_lock(&priv->command_lock);
482 ret = rndis_command(dev, u.header);
483 mutex_unlock(&priv->command_lock);
486 ret = rndis_error_status(u.set_c->status);
494 * Specs say that we can only set config parameters only soon after device
496 * value_type: 0 = u32, 2 = unicode string
498 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
499 int value_type, void *value)
501 struct ndis_config_param *infobuf;
502 int value_len, info_len, param_len, ret, i;
507 value_len = sizeof(__le32);
508 else if (value_type == 2)
509 value_len = strlen(value) * sizeof(__le16);
513 param_len = strlen(param) * sizeof(__le16);
514 info_len = sizeof(*infobuf) + param_len + value_len;
519 infobuf = kmalloc(info_len, GFP_KERNEL);
525 /* extra 12 bytes are for padding (debug output) */
526 memset(infobuf, 0xCC, info_len + 12);
530 devdbg(dev, "setting config parameter: %s, value: %s",
533 devdbg(dev, "setting config parameter: %s, value: %d",
534 param, *(u32 *)value);
536 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
537 infobuf->name_length = cpu_to_le32(param_len);
538 infobuf->type = cpu_to_le32(value_type);
539 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
540 infobuf->value_length = cpu_to_le32(value_len);
542 /* simple string to unicode string conversion */
543 unibuf = (void *)infobuf + sizeof(*infobuf);
544 for (i = 0; i < param_len / sizeof(__le16); i++)
545 unibuf[i] = cpu_to_le16(param[i]);
547 if (value_type == 2) {
548 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
549 for (i = 0; i < value_len / sizeof(__le16); i++)
550 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
552 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
553 *dst_value = cpu_to_le32(*(u32 *)value);
557 devdbg(dev, "info buffer (len: %d):", info_len);
558 for (i = 0; i < info_len; i += 12) {
559 u32 *tmp = (u32 *)((u8 *)infobuf + i);
560 devdbg(dev, "%08X:%08X:%08X",
563 cpu_to_be32(tmp[2]));
567 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
570 devdbg(dev, "setting rndis config paramater failed, %d.", ret);
576 static int rndis_set_config_parameter_str(struct usbnet *dev,
577 char *param, char *value)
579 return(rndis_set_config_parameter(dev, param, 2, value));
582 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
583 char *param, u32 value)
585 return(rndis_set_config_parameter(dev, param, 0, &value));
590 * data conversion functions
592 static int level_to_qual(int level)
594 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
595 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
599 static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
605 /* see comment in wireless.h above the "struct iw_freq"
606 * definition for an explanation of this if
607 * NOTE: 1000000 is due to the kHz
609 if (dsconfig > 1000000) {
610 freq->m = dsconfig / 10;
615 /* convert from kHz to Hz */
620 static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
622 if (freq->m < 1000 && freq->e == 0) {
624 freq->m <= (sizeof(freq_chan) / sizeof(freq_chan[0])))
625 *dsconfig = freq_chan[freq->m - 1] * 1000;
631 for (i = freq->e; i > 0; i--)
644 add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
646 static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
651 ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
658 unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
660 memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
661 tmp[le32_to_cpu(ssid->length)] = 0;
662 devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
669 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
671 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
674 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
676 memcpy(&priv->essid, ssid, sizeof(priv->essid));
678 devdbg(usbdev, "set_essid: radio_on = 1");
685 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
690 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
693 memset(bssid, 0, ETH_ALEN);
698 static int get_association_info(struct usbnet *usbdev,
699 struct ndis_80211_assoc_info *info, int len)
701 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
705 static int is_associated(struct usbnet *usbdev)
710 ret = get_bssid(usbdev, bssid);
712 return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
716 static int disassociate(struct usbnet *usbdev, int reset_ssid)
718 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
719 struct ndis_80211_ssid ssid;
722 if (priv->radio_on) {
723 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
726 devdbg(usbdev, "disassociate: radio_on = 0");
733 /* disassociate causes radio to be turned off; if reset_ssid
734 * is given, set random ssid to enable radio */
736 ssid.length = cpu_to_le32(sizeof(ssid.essid));
737 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
739 ssid.essid[1] = 0xff;
740 for (i = 2; i < sizeof(ssid.essid); i++)
741 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
742 ret = set_essid(usbdev, &ssid);
748 static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
750 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
754 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
755 "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
757 if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
758 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
759 auth_mode = ndis_80211_auth_wpa2;
761 auth_mode = ndis_80211_auth_wpa2_psk;
762 } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
763 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
764 auth_mode = ndis_80211_auth_wpa;
765 else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
766 auth_mode = ndis_80211_auth_wpa_psk;
768 auth_mode = ndis_80211_auth_wpa_none;
769 } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
770 if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
771 auth_mode = ndis_80211_auth_auto_switch;
773 auth_mode = ndis_80211_auth_shared;
775 auth_mode = ndis_80211_auth_open;
777 tmp = cpu_to_le32(auth_mode);
778 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
781 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
785 priv->wpa_version = wpa_version;
786 priv->wpa_authalg = authalg;
791 static int set_priv_filter(struct usbnet *usbdev)
793 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
796 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
798 if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
799 priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
800 tmp = cpu_to_le32(ndis_80211_priv_8021x_wep);
802 tmp = cpu_to_le32(ndis_80211_priv_accept_all);
804 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
809 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
811 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
815 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
819 if (pairwise & IW_AUTH_CIPHER_CCMP)
820 encr_mode = ndis_80211_encr_ccmp_enabled;
821 else if (pairwise & IW_AUTH_CIPHER_TKIP)
822 encr_mode = ndis_80211_encr_tkip_enabled;
824 (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
825 encr_mode = ndis_80211_encr_wep_enabled;
826 else if (groupwise & IW_AUTH_CIPHER_CCMP)
827 encr_mode = ndis_80211_encr_ccmp_enabled;
828 else if (groupwise & IW_AUTH_CIPHER_TKIP)
829 encr_mode = ndis_80211_encr_tkip_enabled;
831 encr_mode = ndis_80211_encr_disabled;
833 tmp = cpu_to_le32(encr_mode);
834 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
837 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
841 priv->wpa_cipher_pair = pairwise;
842 priv->wpa_cipher_group = groupwise;
847 static int set_assoc_params(struct usbnet *usbdev)
849 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
851 set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
852 set_priv_filter(usbdev);
853 set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
859 static int set_infra_mode(struct usbnet *usbdev, int mode)
861 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
865 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
867 tmp = cpu_to_le32(mode);
868 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
871 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
875 /* NDIS drivers clear keys when infrastructure mode is
876 * changed. But Linux tools assume otherwise. So set the
878 if (priv->wpa_keymgmt == 0 ||
879 priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
880 for (i = 0; i < 4; i++) {
881 if (priv->encr_key_len[i] > 0)
882 add_wep_key(usbdev, priv->encr_keys[i],
883 priv->encr_key_len[i], i);
887 priv->infra_mode = mode;
892 static void set_default_iw_params(struct usbnet *usbdev)
894 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
896 priv->wpa_keymgmt = 0;
897 priv->wpa_version = 0;
899 set_infra_mode(usbdev, ndis_80211_infra_infra);
900 set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
901 IW_AUTH_ALG_OPEN_SYSTEM);
902 set_priv_filter(usbdev);
903 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
907 static int deauthenticate(struct usbnet *usbdev)
911 ret = disassociate(usbdev, 1);
912 set_default_iw_params(usbdev);
917 /* index must be 0 - N, as per NDIS */
918 static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
920 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
921 struct ndis_80211_wep_key ndis_key;
924 if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
927 memset(&ndis_key, 0, sizeof(ndis_key));
929 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
930 ndis_key.length = cpu_to_le32(key_len);
931 ndis_key.index = cpu_to_le32(index);
932 memcpy(&ndis_key.material, key, key_len);
934 if (index == priv->encr_tx_key_index) {
935 ndis_key.index |= cpu_to_le32(1 << 31);
936 ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
937 IW_AUTH_CIPHER_NONE);
939 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
943 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
946 devwarn(usbdev, "adding encryption key %d failed (%08X)",
951 priv->encr_key_len[index] = key_len;
952 memcpy(&priv->encr_keys[index], key, key_len);
958 /* remove_key is for both wep and wpa */
959 static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
961 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
962 struct ndis_80211_remove_key remove_key;
966 if (priv->encr_key_len[index] == 0)
969 priv->encr_key_len[index] = 0;
970 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
972 if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
973 priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
974 priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
975 priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
976 remove_key.size = cpu_to_le32(sizeof(remove_key));
977 remove_key.index = cpu_to_le32(index);
980 if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
981 remove_key.index |= cpu_to_le32(1 << 30);
982 memcpy(remove_key.bssid, bssid,
983 sizeof(remove_key.bssid));
985 memset(remove_key.bssid, 0xff,
986 sizeof(remove_key.bssid));
988 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
993 keyindex = cpu_to_le32(index);
994 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
998 "removing encryption key %d failed (%08X)",
1004 /* if it is transmit key, disable encryption */
1005 if (index == priv->encr_tx_key_index)
1006 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
1012 static void set_multicast_list(struct usbnet *usbdev)
1014 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1015 struct dev_mc_list *mclist;
1020 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1022 if (usbdev->net->flags & IFF_PROMISC) {
1023 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1024 RNDIS_PACKET_TYPE_ALL_LOCAL;
1025 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1026 usbdev->net->mc_count > priv->multicast_size) {
1027 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1028 } else if (usbdev->net->mc_count > 0) {
1029 size = min(priv->multicast_size, usbdev->net->mc_count);
1030 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1033 "couldn't alloc %d bytes of memory",
1038 mclist = usbdev->net->mc_list;
1039 for (i = 0; i < size && mclist; mclist = mclist->next) {
1040 if (mclist->dmi_addrlen != ETH_ALEN)
1043 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1047 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1049 if (ret == 0 && i > 0)
1050 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1052 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1054 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1055 i, priv->multicast_size, ret);
1060 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1063 devwarn(usbdev, "couldn't set packet filter: %08x",
1064 le32_to_cpu(filter));
1067 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1068 le32_to_cpu(filter), ret);
1073 * wireless extension handlers
1076 static int rndis_iw_commit(struct net_device *dev,
1077 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1084 static int rndis_iw_get_range(struct net_device *dev,
1085 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1087 struct iw_range *range = (struct iw_range *)extra;
1088 struct usbnet *usbdev = dev->priv;
1089 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1090 int len, ret, i, j, num, has_80211g_rates;
1094 devdbg(usbdev, "SIOCGIWRANGE");
1096 /* clear iw_range struct */
1097 memset(range, 0, sizeof(*range));
1098 wrqu->data.length = sizeof(*range);
1100 range->txpower_capa = IW_TXPOW_MWATT;
1101 range->num_txpower = 1;
1102 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1103 len = sizeof(tx_power);
1104 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1106 if (ret == 0 && le32_to_cpu(tx_power) != 0xFF)
1107 range->txpower[0] = le32_to_cpu(tx_power);
1109 range->txpower[0] = get_bcm4320_power(priv);
1111 range->txpower[0] = get_bcm4320_power(priv);
1113 len = sizeof(rates);
1114 ret = rndis_query_oid(usbdev, OID_802_11_SUPPORTED_RATES, &rates,
1116 has_80211g_rates = 0;
1119 for (i = 0; i < len; i++) {
1122 range->bitrate[j] = (rates[i] & 0x7f) * 500000;
1123 /* check for non 802.11b rates */
1124 if (range->bitrate[j] == 6000000 ||
1125 range->bitrate[j] == 9000000 ||
1126 (range->bitrate[j] >= 12000000 &&
1127 range->bitrate[j] != 22000000))
1128 has_80211g_rates = 1;
1131 range->num_bitrates = j;
1133 range->num_bitrates = 0;
1135 /* fill in 802.11g rates */
1136 if (has_80211g_rates) {
1137 num = range->num_bitrates;
1138 for (i = 0; i < sizeof(rates_80211g); i++) {
1139 for (j = 0; j < num; j++) {
1140 if (range->bitrate[j] ==
1141 rates_80211g[i] * 1000000)
1145 range->bitrate[range->num_bitrates++] =
1146 rates_80211g[i] * 1000000;
1147 if (range->num_bitrates == IW_MAX_BITRATES)
1151 /* estimated max real througput in bps */
1152 range->throughput = 54 * 1000 * 1000 / 2;
1154 /* ~35% more with afterburner */
1155 if (priv->param_afterburner)
1156 range->throughput = range->throughput / 100 * 135;
1158 /* estimated max real througput in bps */
1159 range->throughput = 11 * 1000 * 1000 / 2;
1162 range->num_channels = (sizeof(freq_chan)/sizeof(freq_chan[0]));
1164 for (i = 0; i < (sizeof(freq_chan)/sizeof(freq_chan[0])) &&
1165 i < IW_MAX_FREQUENCIES; i++) {
1166 range->freq[i].i = i + 1;
1167 range->freq[i].m = freq_chan[i] * 100000;
1168 range->freq[i].e = 1;
1170 range->num_frequency = i;
1173 range->max_rts = 2347;
1174 range->min_frag = 256;
1175 range->max_frag = 2346;
1177 range->max_qual.qual = 100;
1178 range->max_qual.level = 154;
1179 range->max_qual.updated = IW_QUAL_QUAL_UPDATED
1180 | IW_QUAL_LEVEL_UPDATED
1181 | IW_QUAL_NOISE_INVALID;
1183 range->we_version_compiled = WIRELESS_EXT;
1184 range->we_version_source = WIRELESS_EXT;
1186 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
1187 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
1192 static int rndis_iw_get_name(struct net_device *dev,
1193 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1195 struct usbnet *usbdev = dev->priv;
1196 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1198 strcpy(wrqu->name, priv->name);
1203 static int rndis_iw_set_essid(struct net_device *dev,
1204 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1206 struct ndis_80211_ssid ssid;
1207 int length = wrqu->essid.length;
1208 struct usbnet *usbdev = dev->priv;
1210 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1211 wrqu->essid.flags, wrqu->essid.length, essid);
1213 if (length > NDIS_802_11_LENGTH_SSID)
1214 length = NDIS_802_11_LENGTH_SSID;
1216 ssid.length = cpu_to_le32(length);
1218 memcpy(ssid.essid, essid, length);
1220 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
1222 set_assoc_params(usbdev);
1224 if (!wrqu->essid.flags || length == 0)
1225 return disassociate(usbdev, 1);
1227 return set_essid(usbdev, &ssid);
1231 static int rndis_iw_get_essid(struct net_device *dev,
1232 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1234 struct ndis_80211_ssid ssid;
1235 struct usbnet *usbdev = dev->priv;
1238 ret = get_essid(usbdev, &ssid);
1240 if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
1241 wrqu->essid.flags = 1;
1242 wrqu->essid.length = le32_to_cpu(ssid.length);
1243 memcpy(essid, ssid.essid, wrqu->essid.length);
1244 essid[wrqu->essid.length] = 0;
1246 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1247 wrqu->essid.flags = 0;
1248 wrqu->essid.length = 0;
1250 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1255 static int rndis_iw_get_bssid(struct net_device *dev,
1256 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1258 struct usbnet *usbdev = dev->priv;
1259 unsigned char bssid[ETH_ALEN];
1261 DECLARE_MAC_BUF(mac);
1263 ret = get_bssid(usbdev, bssid);
1266 devdbg(usbdev, "SIOCGIWAP: %s", print_mac(mac, bssid));
1268 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1270 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1271 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1277 static int rndis_iw_set_bssid(struct net_device *dev,
1278 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1280 struct usbnet *usbdev = dev->priv;
1281 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1282 DECLARE_MAC_BUF(mac);
1285 devdbg(usbdev, "SIOCSIWAP: %s", print_mac(mac, bssid));
1287 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1289 /* user apps may set ap's mac address, which is not required;
1290 * they may fail to work if this function fails, so return
1293 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1299 static int rndis_iw_set_auth(struct net_device *dev,
1300 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1302 struct iw_param *p = &wrqu->param;
1303 struct usbnet *usbdev = dev->priv;
1304 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1305 int ret = -ENOTSUPP;
1307 switch (p->flags & IW_AUTH_INDEX) {
1308 case IW_AUTH_WPA_VERSION:
1309 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1310 priv->wpa_version = p->value;
1314 case IW_AUTH_CIPHER_PAIRWISE:
1315 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1316 priv->wpa_cipher_pair = p->value;
1320 case IW_AUTH_CIPHER_GROUP:
1321 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1322 priv->wpa_cipher_group = p->value;
1326 case IW_AUTH_KEY_MGMT:
1327 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1328 priv->wpa_keymgmt = p->value;
1332 case IW_AUTH_TKIP_COUNTERMEASURES:
1333 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1338 case IW_AUTH_DROP_UNENCRYPTED:
1339 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1343 case IW_AUTH_80211_AUTH_ALG:
1344 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1345 priv->wpa_authalg = p->value;
1349 case IW_AUTH_WPA_ENABLED:
1350 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1351 if (wrqu->param.value)
1352 deauthenticate(usbdev);
1356 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1357 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1362 case IW_AUTH_ROAMING_CONTROL:
1363 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1367 case IW_AUTH_PRIVACY_INVOKED:
1368 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1369 wrqu->param.flags & IW_AUTH_INDEX);
1373 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
1374 p->flags & IW_AUTH_INDEX, p->value);
1380 static int rndis_iw_get_auth(struct net_device *dev,
1381 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1383 struct iw_param *p = &wrqu->param;
1384 struct usbnet *usbdev = dev->priv;
1385 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1387 switch (p->flags & IW_AUTH_INDEX) {
1388 case IW_AUTH_WPA_VERSION:
1389 p->value = priv->wpa_version;
1391 case IW_AUTH_CIPHER_PAIRWISE:
1392 p->value = priv->wpa_cipher_pair;
1394 case IW_AUTH_CIPHER_GROUP:
1395 p->value = priv->wpa_cipher_group;
1397 case IW_AUTH_KEY_MGMT:
1398 p->value = priv->wpa_keymgmt;
1400 case IW_AUTH_80211_AUTH_ALG:
1401 p->value = priv->wpa_authalg;
1404 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1405 wrqu->param.flags & IW_AUTH_INDEX);
1412 static int rndis_iw_get_mode(struct net_device *dev,
1413 struct iw_request_info *info,
1414 union iwreq_data *wrqu, char *extra)
1416 struct usbnet *usbdev = dev->priv;
1417 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1419 switch (priv->infra_mode) {
1420 case ndis_80211_infra_adhoc:
1421 wrqu->mode = IW_MODE_ADHOC;
1423 case ndis_80211_infra_infra:
1424 wrqu->mode = IW_MODE_INFRA;
1426 /*case ndis_80211_infra_auto_unknown:*/
1428 wrqu->mode = IW_MODE_AUTO;
1431 devdbg(usbdev, "SIOCGIWMODE: %08x", wrqu->mode);
1436 static int rndis_iw_set_mode(struct net_device *dev,
1437 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1439 struct usbnet *usbdev = dev->priv;
1442 devdbg(usbdev, "SIOCSIWMODE: %08x", wrqu->mode);
1444 switch (wrqu->mode) {
1446 mode = ndis_80211_infra_adhoc;
1449 mode = ndis_80211_infra_infra;
1451 /*case IW_MODE_AUTO:*/
1453 mode = ndis_80211_infra_auto_unknown;
1457 return set_infra_mode(usbdev, mode);
1461 static int rndis_iw_set_encode(struct net_device *dev,
1462 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1464 struct usbnet *usbdev = dev->priv;
1465 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1466 int ret, index, key_len;
1469 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1471 /* iwconfig gives index as 1 - N */
1475 index = priv->encr_tx_key_index;
1477 if (index < 0 || index >= 4) {
1478 devwarn(usbdev, "encryption index out of range (%u)", index);
1482 /* remove key if disabled */
1483 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1484 if (remove_key(usbdev, index, NULL))
1490 /* global encryption state (for all keys) */
1491 if (wrqu->data.flags & IW_ENCODE_OPEN)
1492 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1493 IW_AUTH_ALG_OPEN_SYSTEM);
1494 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1495 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1496 IW_AUTH_ALG_SHARED_KEY);
1500 if (wrqu->data.length > 0) {
1501 key_len = wrqu->data.length;
1504 /* must be set as tx key */
1505 if (priv->encr_key_len[index] == 0)
1507 key_len = priv->encr_key_len[index];
1508 key = priv->encr_keys[index];
1509 priv->encr_tx_key_index = index;
1512 if (add_wep_key(usbdev, key, key_len, index) != 0)
1515 if (index == priv->encr_tx_key_index)
1516 /* ndis drivers want essid to be set after setting encr */
1517 set_essid(usbdev, &priv->essid);
1523 static int rndis_iw_set_encode_ext(struct net_device *dev,
1524 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1526 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1527 struct usbnet *usbdev = dev->priv;
1528 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1529 struct ndis_80211_key ndis_key;
1533 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1535 /* iwconfig gives index as 1 - N */
1539 keyidx = priv->encr_tx_key_index;
1541 if (keyidx < 0 || keyidx >= 4)
1544 if (ext->alg == WPA_ALG_WEP) {
1545 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1546 priv->encr_tx_key_index = keyidx;
1547 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1550 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1551 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1552 return remove_key(usbdev, keyidx, NULL);
1554 if (ext->key_len > sizeof(ndis_key.material))
1557 memset(&ndis_key, 0, sizeof(ndis_key));
1559 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1560 sizeof(ndis_key.material) + ext->key_len);
1561 ndis_key.length = cpu_to_le32(ext->key_len);
1562 ndis_key.index = cpu_to_le32(keyidx);
1564 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1565 memcpy(ndis_key.rsc, ext->rx_seq, 6);
1566 ndis_key.index |= cpu_to_le32(1 << 29);
1569 addr = ext->addr.sa_data;
1570 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1572 if (priv->infra_mode == ndis_80211_infra_adhoc)
1573 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1575 get_bssid(usbdev, ndis_key.bssid);
1578 ndis_key.index |= cpu_to_le32(1 << 30);
1579 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1582 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1583 ndis_key.index |= cpu_to_le32(1 << 31);
1585 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1586 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1587 * different order than NDIS spec, so swap the order here. */
1588 memcpy(ndis_key.material, ext->key, 16);
1589 memcpy(ndis_key.material + 16, ext->key + 24, 8);
1590 memcpy(ndis_key.material + 24, ext->key + 16, 8);
1592 memcpy(ndis_key.material, ext->key, ext->key_len);
1594 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1595 le32_to_cpu(ndis_key.size));
1596 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1600 priv->encr_key_len[keyidx] = ext->key_len;
1601 memcpy(&priv->encr_keys[keyidx], ndis_key.material, ext->key_len);
1602 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1603 priv->encr_tx_key_index = keyidx;
1609 static int rndis_iw_set_scan(struct net_device *dev,
1610 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1612 struct iw_param *param = &wrqu->param;
1613 struct usbnet *usbdev = dev->priv;
1614 union iwreq_data evt;
1618 devdbg(usbdev, "SIOCSIWSCAN");
1620 if (param->flags == 0) {
1622 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1625 evt.data.length = 0;
1626 wireless_send_event(dev, SIOCGIWSCAN, &evt, NULL);
1632 static char *rndis_translate_scan(struct net_device *dev,
1633 char *cev, char *end_buf, struct ndis_80211_bssid_ex *bssid)
1636 struct usbnet *usbdev = dev->priv;
1638 struct ieee80211_info_element *ie;
1640 int bssid_len, ie_len, i;
1642 struct iw_event iwe;
1643 unsigned char sbuf[32];
1644 DECLARE_MAC_BUF(mac);
1646 bssid_len = le32_to_cpu(bssid->length);
1648 devdbg(usbdev, "BSSID %s", print_mac(mac, bssid->mac));
1649 iwe.cmd = SIOCGIWAP;
1650 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1651 memcpy(iwe.u.ap_addr.sa_data, bssid->mac, ETH_ALEN);
1652 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_ADDR_LEN);
1654 devdbg(usbdev, "SSID(%d) %s", le32_to_cpu(bssid->ssid.length),
1656 iwe.cmd = SIOCGIWESSID;
1657 iwe.u.essid.length = le32_to_cpu(bssid->ssid.length);
1658 iwe.u.essid.flags = 1;
1659 cev = iwe_stream_add_point(cev, end_buf, &iwe, bssid->ssid.essid);
1661 devdbg(usbdev, "MODE %d", le32_to_cpu(bssid->net_infra));
1662 iwe.cmd = SIOCGIWMODE;
1663 switch (le32_to_cpu(bssid->net_infra)) {
1664 case ndis_80211_infra_adhoc:
1665 iwe.u.mode = IW_MODE_ADHOC;
1667 case ndis_80211_infra_infra:
1668 iwe.u.mode = IW_MODE_INFRA;
1670 /*case ndis_80211_infra_auto_unknown:*/
1672 iwe.u.mode = IW_MODE_AUTO;
1675 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_UINT_LEN);
1677 devdbg(usbdev, "FREQ %d kHz", le32_to_cpu(bssid->config.ds_config));
1678 iwe.cmd = SIOCGIWFREQ;
1679 dsconfig_to_freq(le32_to_cpu(bssid->config.ds_config), &iwe.u.freq);
1680 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_FREQ_LEN);
1682 devdbg(usbdev, "QUAL %d", le32_to_cpu(bssid->rssi));
1684 iwe.u.qual.qual = level_to_qual(le32_to_cpu(bssid->rssi));
1685 iwe.u.qual.level = le32_to_cpu(bssid->rssi);
1686 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
1687 | IW_QUAL_LEVEL_UPDATED
1688 | IW_QUAL_NOISE_INVALID;
1689 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_QUAL_LEN);
1691 devdbg(usbdev, "ENCODE %d", le32_to_cpu(bssid->privacy));
1692 iwe.cmd = SIOCGIWENCODE;
1693 iwe.u.data.length = 0;
1694 if (le32_to_cpu(bssid->privacy) == ndis_80211_priv_accept_all)
1695 iwe.u.data.flags = IW_ENCODE_DISABLED;
1697 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1699 cev = iwe_stream_add_point(cev, end_buf, &iwe, NULL);
1701 devdbg(usbdev, "RATES:");
1702 current_val = cev + IW_EV_LCP_LEN;
1703 iwe.cmd = SIOCGIWRATE;
1704 for (i = 0; i < sizeof(bssid->rates); i++) {
1705 if (bssid->rates[i] & 0x7f) {
1706 iwe.u.bitrate.value =
1707 ((bssid->rates[i] & 0x7f) *
1709 devdbg(usbdev, " %d", iwe.u.bitrate.value);
1710 current_val = iwe_stream_add_value(cev,
1711 current_val, end_buf, &iwe,
1716 if ((current_val - cev) > IW_EV_LCP_LEN)
1719 beacon = le32_to_cpu(bssid->config.beacon_period);
1720 devdbg(usbdev, "BCN_INT %d", beacon);
1721 iwe.cmd = IWEVCUSTOM;
1722 snprintf(sbuf, sizeof(sbuf), "bcn_int=%d", beacon);
1723 iwe.u.data.length = strlen(sbuf);
1724 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1726 atim = le32_to_cpu(bssid->config.atim_window);
1727 devdbg(usbdev, "ATIM %d", atim);
1728 iwe.cmd = IWEVCUSTOM;
1729 snprintf(sbuf, sizeof(sbuf), "atim=%u", atim);
1730 iwe.u.data.length = strlen(sbuf);
1731 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1733 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1734 ie_len = min(bssid_len - (int)sizeof(*bssid),
1735 (int)le32_to_cpu(bssid->ie_length));
1736 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1737 while (ie_len >= sizeof(*ie) && sizeof(*ie) + ie->len <= ie_len) {
1738 if ((ie->id == MFIE_TYPE_GENERIC && ie->len >= 4 &&
1739 memcmp(ie->data, "\x00\x50\xf2\x01", 4) == 0) ||
1740 ie->id == MFIE_TYPE_RSN) {
1741 devdbg(usbdev, "IE: WPA%d",
1742 (ie->id == MFIE_TYPE_RSN) ? 2 : 1);
1743 iwe.cmd = IWEVGENIE;
1744 iwe.u.data.length = min(ie->len + 2, MAX_WPA_IE_LEN);
1745 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1749 ie_len -= sizeof(*ie) + ie->len;
1750 ie = (struct ieee80211_info_element *)&ie->data[ie->len];
1757 static int rndis_iw_get_scan(struct net_device *dev,
1758 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1760 struct usbnet *usbdev = dev->priv;
1763 struct ndis_80211_bssid_list_ex *bssid_list;
1764 struct ndis_80211_bssid_ex *bssid;
1765 int ret = -EINVAL, len, count, bssid_len;
1767 devdbg(usbdev, "SIOCGIWSCAN");
1769 len = CONTROL_BUFFER_SIZE;
1770 buf = kmalloc(len, GFP_KERNEL);
1776 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1782 bssid = bssid_list->bssid;
1783 bssid_len = le32_to_cpu(bssid->length);
1784 count = le32_to_cpu(bssid_list->num_items);
1785 devdbg(usbdev, "SIOCGIWSCAN: %d BSSIDs found", count);
1787 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1788 cev = rndis_translate_scan(dev, cev, extra + IW_SCAN_MAX_DATA,
1790 bssid = (void *)bssid + bssid_len;
1791 bssid_len = le32_to_cpu(bssid->length);
1796 wrqu->data.length = cev - extra;
1797 wrqu->data.flags = 0;
1803 static int rndis_iw_set_genie(struct net_device *dev,
1804 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1806 struct usbnet *usbdev = dev->priv;
1807 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1813 for (j = 0; j < wrqu->data.length; j += 8)
1815 "SIOCSIWGENIE %04x - "
1816 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1817 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1818 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1820 /* clear existing IEs */
1821 if (priv->wpa_ie_len) {
1822 kfree(priv->wpa_ie);
1823 priv->wpa_ie_len = 0;
1827 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1829 priv->wpa_ie_len = wrqu->data.length;
1830 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1837 static int rndis_iw_get_genie(struct net_device *dev,
1838 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1840 struct usbnet *usbdev = dev->priv;
1841 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1843 devdbg(usbdev, "SIOCGIWGENIE");
1845 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1846 wrqu->data.length = 0;
1850 if (wrqu->data.length < priv->wpa_ie_len)
1853 wrqu->data.length = priv->wpa_ie_len;
1854 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1860 static int rndis_iw_set_rts(struct net_device *dev,
1861 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1863 struct usbnet *usbdev = dev->priv;
1865 devdbg(usbdev, "SIOCSIWRTS");
1867 tmp = cpu_to_le32(wrqu->rts.value);
1868 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1873 static int rndis_iw_get_rts(struct net_device *dev,
1874 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1876 struct usbnet *usbdev = dev->priv;
1881 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1883 wrqu->rts.value = le32_to_cpu(tmp);
1884 wrqu->rts.flags = 1;
1885 wrqu->rts.disabled = 0;
1888 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1894 static int rndis_iw_set_frag(struct net_device *dev,
1895 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1897 struct usbnet *usbdev = dev->priv;
1900 devdbg(usbdev, "SIOCSIWFRAG");
1902 tmp = cpu_to_le32(wrqu->frag.value);
1903 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1908 static int rndis_iw_get_frag(struct net_device *dev,
1909 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1911 struct usbnet *usbdev = dev->priv;
1916 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1919 wrqu->frag.value = le32_to_cpu(tmp);
1920 wrqu->frag.flags = 1;
1921 wrqu->frag.disabled = 0;
1923 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1928 static int rndis_iw_set_nick(struct net_device *dev,
1929 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1931 struct usbnet *usbdev = dev->priv;
1932 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1934 devdbg(usbdev, "SIOCSIWNICK");
1936 priv->nick_len = wrqu->data.length;
1937 if (priv->nick_len > 32)
1938 priv->nick_len = 32;
1940 memcpy(priv->nick, extra, priv->nick_len);
1945 static int rndis_iw_get_nick(struct net_device *dev,
1946 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1948 struct usbnet *usbdev = dev->priv;
1949 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1951 wrqu->data.flags = 1;
1952 wrqu->data.length = priv->nick_len;
1953 memcpy(extra, priv->nick, priv->nick_len);
1955 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1961 static int rndis_iw_set_freq(struct net_device *dev,
1962 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1964 struct usbnet *usbdev = dev->priv;
1965 struct ndis_80211_conf config;
1966 unsigned int dsconfig;
1969 /* this OID is valid only when not associated */
1970 if (is_associated(usbdev))
1974 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1977 len = sizeof(config);
1978 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1980 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1984 config.ds_config = cpu_to_le32(dsconfig);
1986 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1987 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1992 static int rndis_iw_get_freq(struct net_device *dev,
1993 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1995 struct usbnet *usbdev = dev->priv;
1996 struct ndis_80211_conf config;
1999 len = sizeof(config);
2000 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
2002 dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
2004 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
2009 static int rndis_iw_get_txpower(struct net_device *dev,
2010 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2012 struct usbnet *usbdev = dev->priv;
2013 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2017 if (priv->radio_on) {
2018 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2019 len = sizeof(tx_power);
2020 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2025 /* fake incase not supported */
2026 tx_power = cpu_to_le32(get_bcm4320_power(priv));
2028 wrqu->txpower.flags = IW_TXPOW_MWATT;
2029 wrqu->txpower.value = le32_to_cpu(tx_power);
2030 wrqu->txpower.disabled = 0;
2032 wrqu->txpower.flags = IW_TXPOW_MWATT;
2033 wrqu->txpower.value = 0;
2034 wrqu->txpower.disabled = 1;
2037 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
2043 static int rndis_iw_set_txpower(struct net_device *dev,
2044 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2046 struct usbnet *usbdev = dev->priv;
2047 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2048 __le32 tx_power = 0;
2051 if (!wrqu->txpower.disabled) {
2052 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
2053 tx_power = cpu_to_le32(wrqu->txpower.value);
2054 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
2055 if (wrqu->txpower.value > 20)
2056 tx_power = cpu_to_le32(128);
2057 else if (wrqu->txpower.value < -43)
2058 tx_power = cpu_to_le32(127);
2061 tmp = wrqu->txpower.value;
2064 tx_power = cpu_to_le32((unsigned char)tmp);
2069 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
2071 if (le32_to_cpu(tx_power) != 0) {
2072 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2073 /* turn radio on first */
2074 if (!priv->radio_on)
2075 disassociate(usbdev, 1);
2077 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2078 &tx_power, sizeof(tx_power));
2083 /* txpower unsupported, just turn radio on */
2084 if (!priv->radio_on)
2085 return disassociate(usbdev, 1);
2086 return 0; /* all ready on */
2090 /* tx_power == 0, turn off radio */
2091 return disassociate(usbdev, 0);
2095 static int rndis_iw_get_rate(struct net_device *dev,
2096 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2098 struct usbnet *usbdev = dev->priv;
2103 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2105 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2106 wrqu->bitrate.disabled = 0;
2107 wrqu->bitrate.flags = 1;
2113 static int rndis_iw_set_mlme(struct net_device *dev,
2114 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2116 struct usbnet *usbdev = dev->priv;
2117 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2118 struct iw_mlme *mlme = (struct iw_mlme *)extra;
2119 unsigned char bssid[ETH_ALEN];
2121 get_bssid(usbdev, bssid);
2123 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2126 switch (mlme->cmd) {
2127 case IW_MLME_DEAUTH:
2128 return deauthenticate(usbdev);
2129 case IW_MLME_DISASSOC:
2130 return disassociate(usbdev, priv->radio_on);
2139 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2141 struct usbnet *usbdev = dev->priv;
2142 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2143 unsigned long flags;
2145 spin_lock_irqsave(&priv->stats_lock, flags);
2146 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2147 spin_unlock_irqrestore(&priv->stats_lock, flags);
2149 return &priv->iwstats;
2153 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2154 static const iw_handler rndis_iw_handler[] =
2156 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2157 IW_IOCTL(SIOCGIWNAME) = rndis_iw_get_name,
2158 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2159 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2160 IW_IOCTL(SIOCSIWMODE) = rndis_iw_set_mode,
2161 IW_IOCTL(SIOCGIWMODE) = rndis_iw_get_mode,
2162 IW_IOCTL(SIOCGIWRANGE) = rndis_iw_get_range,
2163 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2164 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2165 IW_IOCTL(SIOCSIWSCAN) = rndis_iw_set_scan,
2166 IW_IOCTL(SIOCGIWSCAN) = rndis_iw_get_scan,
2167 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2168 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2169 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2170 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2171 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2172 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2173 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2174 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2175 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2176 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2177 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2178 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2179 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2180 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2181 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2182 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2183 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2184 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2187 static const iw_handler rndis_wext_private_handler[] = {
2190 static const struct iw_priv_args rndis_wext_private_args[] = {
2194 static const struct iw_handler_def rndis_iw_handlers = {
2195 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2196 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2197 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2198 .standard = (iw_handler *)rndis_iw_handler,
2199 .private = (iw_handler *)rndis_wext_private_handler,
2200 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2201 .get_wireless_stats = rndis_get_wireless_stats,
2205 static void rndis_wext_worker(struct work_struct *work)
2207 struct rndis_wext_private *priv =
2208 container_of(work, struct rndis_wext_private, work);
2209 struct usbnet *usbdev = priv->usbdev;
2210 union iwreq_data evt;
2211 unsigned char bssid[ETH_ALEN];
2212 struct ndis_80211_assoc_info *info;
2213 int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
2216 if (test_and_clear_bit(WORK_CONNECTION_EVENT, &priv->work_pending)) {
2217 info = kzalloc(assoc_size, GFP_KERNEL);
2221 /* Get association info IEs from device and send them back to
2223 ret = get_association_info(usbdev, info, assoc_size);
2225 evt.data.length = le32_to_cpu(info->req_ie_length);
2226 if (evt.data.length > 0) {
2227 offset = le32_to_cpu(info->offset_req_ies);
2228 wireless_send_event(usbdev->net,
2229 IWEVASSOCREQIE, &evt,
2230 (char *)info + offset);
2233 evt.data.length = le32_to_cpu(info->resp_ie_length);
2234 if (evt.data.length > 0) {
2235 offset = le32_to_cpu(info->offset_resp_ies);
2236 wireless_send_event(usbdev->net,
2237 IWEVASSOCRESPIE, &evt,
2238 (char *)info + offset);
2245 ret = get_bssid(usbdev, bssid);
2248 evt.data.length = 0;
2249 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2250 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2254 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2255 set_multicast_list(usbdev);
2258 static void rndis_wext_set_multicast_list(struct net_device *dev)
2260 struct usbnet *usbdev = dev->priv;
2261 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2263 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2264 queue_work(priv->workqueue, &priv->work);
2267 static void rndis_wext_link_change(struct usbnet *dev, int state)
2269 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2270 union iwreq_data evt;
2273 /* queue work to avoid recursive calls into rndis_command */
2274 set_bit(WORK_CONNECTION_EVENT, &priv->work_pending);
2275 queue_work(priv->workqueue, &priv->work);
2278 evt.data.length = 0;
2279 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2280 wireless_send_event(dev->net, SIOCGIWAP, &evt, NULL);
2285 static int rndis_wext_get_caps(struct usbnet *dev)
2290 } networks_supported;
2291 int len, retval, i, n;
2293 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2295 /* determine if supports setting txpower */
2296 len = sizeof(tx_power);
2297 retval = rndis_query_oid(dev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2299 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2300 priv->caps |= CAP_SUPPORT_TXPOWER;
2302 /* determine supported modes */
2303 len = sizeof(networks_supported);
2304 retval = rndis_query_oid(dev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2305 &networks_supported, &len);
2307 n = le32_to_cpu(networks_supported.num_items);
2310 for (i = 0; i < n; i++) {
2311 switch (le32_to_cpu(networks_supported.items[i])) {
2312 case ndis_80211_type_freq_hop:
2313 case ndis_80211_type_direct_seq:
2314 priv->caps |= CAP_MODE_80211B;
2316 case ndis_80211_type_ofdm_a:
2317 priv->caps |= CAP_MODE_80211A;
2319 case ndis_80211_type_ofdm_g:
2320 priv->caps |= CAP_MODE_80211G;
2324 if (priv->caps & CAP_MODE_80211A)
2325 strcat(priv->name, "a");
2326 if (priv->caps & CAP_MODE_80211B)
2327 strcat(priv->name, "b");
2328 if (priv->caps & CAP_MODE_80211G)
2329 strcat(priv->name, "g");
2336 #define STATS_UPDATE_JIFFIES (HZ)
2337 static void rndis_update_wireless_stats(struct work_struct *work)
2339 struct rndis_wext_private *priv =
2340 container_of(work, struct rndis_wext_private, stats_work.work);
2341 struct usbnet *usbdev = priv->usbdev;
2342 struct iw_statistics iwstats;
2345 unsigned long flags;
2346 int update_jiffies = STATS_UPDATE_JIFFIES;
2349 spin_lock_irqsave(&priv->stats_lock, flags);
2350 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2351 spin_unlock_irqrestore(&priv->stats_lock, flags);
2353 /* only update stats when connected */
2354 if (!is_associated(usbdev)) {
2355 iwstats.qual.qual = 0;
2356 iwstats.qual.level = 0;
2357 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2358 | IW_QUAL_LEVEL_UPDATED
2359 | IW_QUAL_NOISE_INVALID
2360 | IW_QUAL_QUAL_INVALID
2361 | IW_QUAL_LEVEL_INVALID;
2366 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2368 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2371 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2372 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2373 iwstats.qual.level = le32_to_cpu(rssi);
2374 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2375 | IW_QUAL_LEVEL_UPDATED
2376 | IW_QUAL_NOISE_INVALID;
2379 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2382 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2384 iwstats.discard.misc += le32_to_cpu(tmp);
2387 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2389 iwstats.discard.misc += le32_to_cpu(tmp);
2392 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2394 iwstats.discard.misc += le32_to_cpu(tmp);
2396 /* Workaround transfer stalls on poor quality links.
2397 * TODO: find right way to fix these stalls (as stalls do not happen
2398 * with ndiswrapper/windows driver). */
2399 if (iwstats.qual.qual <= 25) {
2400 /* Decrease stats worker interval to catch stalls.
2401 * faster. Faster than 400-500ms causes packet loss,
2402 * Slower doesn't catch stalls fast enough.
2404 j = msecs_to_jiffies(priv->param_workaround_interval);
2405 if (j > STATS_UPDATE_JIFFIES)
2406 j = STATS_UPDATE_JIFFIES;
2411 /* Send scan OID. Use of both OIDs is required to get device
2415 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2418 len = CONTROL_BUFFER_SIZE;
2419 buf = kmalloc(len, GFP_KERNEL);
2423 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2427 spin_lock_irqsave(&priv->stats_lock, flags);
2428 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2429 spin_unlock_irqrestore(&priv->stats_lock, flags);
2431 if (update_jiffies >= HZ)
2432 update_jiffies = round_jiffies_relative(update_jiffies);
2434 j = round_jiffies_relative(update_jiffies);
2435 if (abs(j - update_jiffies) <= 10)
2439 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2443 static int bcm4320_early_init(struct usbnet *dev)
2445 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2448 /* Early initialization settings, setting these won't have effect
2449 * if called after generic_rndis_bind().
2452 priv->param_country[0] = modparam_country[0];
2453 priv->param_country[1] = modparam_country[1];
2454 priv->param_country[2] = 0;
2455 priv->param_frameburst = modparam_frameburst;
2456 priv->param_afterburner = modparam_afterburner;
2457 priv->param_power_save = modparam_power_save;
2458 priv->param_power_output = modparam_power_output;
2459 priv->param_roamtrigger = modparam_roamtrigger;
2460 priv->param_roamdelta = modparam_roamdelta;
2462 priv->param_country[0] = toupper(priv->param_country[0]);
2463 priv->param_country[1] = toupper(priv->param_country[1]);
2464 /* doesn't support EU as country code, use FI instead */
2465 if (!strcmp(priv->param_country, "EU"))
2466 strcpy(priv->param_country, "FI");
2468 if (priv->param_power_save < 0)
2469 priv->param_power_save = 0;
2470 else if (priv->param_power_save > 2)
2471 priv->param_power_save = 2;
2473 if (priv->param_power_output < 0)
2474 priv->param_power_output = 0;
2475 else if (priv->param_power_output > 3)
2476 priv->param_power_output = 3;
2478 if (priv->param_roamtrigger < -80)
2479 priv->param_roamtrigger = -80;
2480 else if (priv->param_roamtrigger > -60)
2481 priv->param_roamtrigger = -60;
2483 if (priv->param_roamdelta < 0)
2484 priv->param_roamdelta = 0;
2485 else if (priv->param_roamdelta > 2)
2486 priv->param_roamdelta = 2;
2488 if (modparam_workaround_interval < 0)
2489 priv->param_workaround_interval = 500;
2491 priv->param_workaround_interval = modparam_workaround_interval;
2493 rndis_set_config_parameter_str(dev, "Country", priv->param_country);
2494 rndis_set_config_parameter_str(dev, "FrameBursting",
2495 priv->param_frameburst ? "1" : "0");
2496 rndis_set_config_parameter_str(dev, "Afterburner",
2497 priv->param_afterburner ? "1" : "0");
2498 sprintf(buf, "%d", priv->param_power_save);
2499 rndis_set_config_parameter_str(dev, "PowerSaveMode", buf);
2500 sprintf(buf, "%d", priv->param_power_output);
2501 rndis_set_config_parameter_str(dev, "PwrOut", buf);
2502 sprintf(buf, "%d", priv->param_roamtrigger);
2503 rndis_set_config_parameter_str(dev, "RoamTrigger", buf);
2504 sprintf(buf, "%d", priv->param_roamdelta);
2505 rndis_set_config_parameter_str(dev, "RoamDelta", buf);
2511 static int rndis_wext_bind(struct usbnet *dev, struct usb_interface *intf)
2513 struct net_device *net = dev->net;
2514 struct rndis_wext_private *priv;
2518 /* allocate rndis private data */
2519 priv = kmalloc(sizeof(struct rndis_wext_private), GFP_KERNEL);
2523 /* These have to be initialized before calling generic_rndis_bind().
2524 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2526 dev->driver_priv = priv;
2527 memset(priv, 0, sizeof(*priv));
2528 memset(priv->name, 0, sizeof(priv->name));
2529 strcpy(priv->name, "IEEE802.11");
2530 net->wireless_handlers = &rndis_iw_handlers;
2533 mutex_init(&priv->command_lock);
2534 spin_lock_init(&priv->stats_lock);
2536 /* try bind rndis_host */
2537 retval = generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2541 /* generic_rndis_bind set packet filter to multicast_all+
2542 * promisc mode which doesn't work well for our devices (device
2543 * picks up rssi to closest station instead of to access point).
2545 * rndis_host wants to avoid all OID as much as possible
2546 * so do promisc/multicast handling in rndis_wext.
2548 dev->net->set_multicast_list = rndis_wext_set_multicast_list;
2549 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2550 retval = rndis_set_oid(dev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2554 retval = rndis_query_oid(dev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp, &len);
2555 priv->multicast_size = le32_to_cpu(tmp);
2556 if (retval < 0 || priv->multicast_size < 0)
2557 priv->multicast_size = 0;
2558 if (priv->multicast_size > 0)
2559 dev->net->flags |= IFF_MULTICAST;
2561 dev->net->flags &= ~IFF_MULTICAST;
2563 priv->iwstats.qual.qual = 0;
2564 priv->iwstats.qual.level = 0;
2565 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2566 | IW_QUAL_LEVEL_UPDATED
2567 | IW_QUAL_NOISE_INVALID
2568 | IW_QUAL_QUAL_INVALID
2569 | IW_QUAL_LEVEL_INVALID;
2571 rndis_wext_get_caps(dev);
2572 set_default_iw_params(dev);
2576 disassociate(dev, 1);
2578 /* because rndis_command() sleeps we need to use workqueue */
2579 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2580 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2581 queue_delayed_work(priv->workqueue, &priv->stats_work,
2582 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2583 INIT_WORK(&priv->work, rndis_wext_worker);
2593 static void rndis_wext_unbind(struct usbnet *dev, struct usb_interface *intf)
2595 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2597 /* turn radio off */
2598 disassociate(dev, 0);
2600 cancel_delayed_work_sync(&priv->stats_work);
2601 cancel_work_sync(&priv->work);
2602 flush_workqueue(priv->workqueue);
2603 destroy_workqueue(priv->workqueue);
2605 if (priv && priv->wpa_ie_len)
2606 kfree(priv->wpa_ie);
2609 rndis_unbind(dev, intf);
2613 static int rndis_wext_reset(struct usbnet *dev)
2615 return deauthenticate(dev);
2619 static const struct driver_info bcm4320b_info = {
2620 .description = "Wireless RNDIS device, BCM4320b based",
2621 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2622 .bind = rndis_wext_bind,
2623 .unbind = rndis_wext_unbind,
2624 .status = rndis_status,
2625 .rx_fixup = rndis_rx_fixup,
2626 .tx_fixup = rndis_tx_fixup,
2627 .reset = rndis_wext_reset,
2628 .early_init = bcm4320_early_init,
2629 .link_change = rndis_wext_link_change,
2632 static const struct driver_info bcm4320a_info = {
2633 .description = "Wireless RNDIS device, BCM4320a based",
2634 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2635 .bind = rndis_wext_bind,
2636 .unbind = rndis_wext_unbind,
2637 .status = rndis_status,
2638 .rx_fixup = rndis_rx_fixup,
2639 .tx_fixup = rndis_tx_fixup,
2640 .reset = rndis_wext_reset,
2641 .early_init = bcm4320_early_init,
2642 .link_change = rndis_wext_link_change,
2645 static const struct driver_info rndis_wext_info = {
2646 .description = "Wireless RNDIS device",
2647 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2648 .bind = rndis_wext_bind,
2649 .unbind = rndis_wext_unbind,
2650 .status = rndis_status,
2651 .rx_fixup = rndis_rx_fixup,
2652 .tx_fixup = rndis_tx_fixup,
2653 .reset = rndis_wext_reset,
2654 .early_init = bcm4320_early_init,
2655 .link_change = rndis_wext_link_change,
2658 /*-------------------------------------------------------------------------*/
2660 static const struct usb_device_id products [] = {
2661 #define RNDIS_MASTER_INTERFACE \
2662 .bInterfaceClass = USB_CLASS_COMM, \
2663 .bInterfaceSubClass = 2 /* ACM */, \
2664 .bInterfaceProtocol = 0x0ff
2666 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2667 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2670 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2671 | USB_DEVICE_ID_MATCH_DEVICE,
2673 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
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 = 0x011b, /* U.S. Robotics USR5421 */
2681 RNDIS_MASTER_INTERFACE,
2682 .driver_info = (unsigned long) &bcm4320b_info,
2684 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2685 | USB_DEVICE_ID_MATCH_DEVICE,
2687 .idProduct = 0x011b, /* Belkin F5D7051 */
2688 RNDIS_MASTER_INTERFACE,
2689 .driver_info = (unsigned long) &bcm4320b_info,
2691 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2692 | USB_DEVICE_ID_MATCH_DEVICE,
2693 .idVendor = 0x1799, /* Belkin has two vendor ids */
2694 .idProduct = 0x011b, /* Belkin F5D7051 */
2695 RNDIS_MASTER_INTERFACE,
2696 .driver_info = (unsigned long) &bcm4320b_info,
2698 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2699 | USB_DEVICE_ID_MATCH_DEVICE,
2701 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2702 RNDIS_MASTER_INTERFACE,
2703 .driver_info = (unsigned long) &bcm4320b_info,
2705 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2706 | USB_DEVICE_ID_MATCH_DEVICE,
2708 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2709 RNDIS_MASTER_INTERFACE,
2710 .driver_info = (unsigned long) &bcm4320b_info,
2712 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2713 | USB_DEVICE_ID_MATCH_DEVICE,
2715 .idProduct = 0x1717, /* Asus WL169gE */
2716 RNDIS_MASTER_INTERFACE,
2717 .driver_info = (unsigned long) &bcm4320b_info,
2719 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2720 | USB_DEVICE_ID_MATCH_DEVICE,
2722 .idProduct = 0xd11b, /* Eminent EM4045 */
2723 RNDIS_MASTER_INTERFACE,
2724 .driver_info = (unsigned long) &bcm4320b_info,
2726 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2727 | USB_DEVICE_ID_MATCH_DEVICE,
2729 .idProduct = 0x0715, /* BT Voyager 1055 */
2730 RNDIS_MASTER_INTERFACE,
2731 .driver_info = (unsigned long) &bcm4320b_info,
2733 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2734 * parameters available, hardware probably contain older firmware version with
2735 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2738 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2739 | USB_DEVICE_ID_MATCH_DEVICE,
2741 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2742 RNDIS_MASTER_INTERFACE,
2743 .driver_info = (unsigned long) &bcm4320a_info,
2745 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2746 | USB_DEVICE_ID_MATCH_DEVICE,
2748 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2749 RNDIS_MASTER_INTERFACE,
2750 .driver_info = (unsigned long) &bcm4320a_info,
2752 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2753 | USB_DEVICE_ID_MATCH_DEVICE,
2755 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2756 RNDIS_MASTER_INTERFACE,
2757 .driver_info = (unsigned long) &bcm4320a_info,
2759 /* Generic Wireless RNDIS devices that we don't have exact
2760 * idVendor/idProduct/chip yet.
2763 /* RNDIS is MSFT's un-official variant of CDC ACM */
2764 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2765 .driver_info = (unsigned long) &rndis_wext_info,
2767 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2768 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2769 .driver_info = (unsigned long) &rndis_wext_info,
2773 MODULE_DEVICE_TABLE(usb, products);
2775 static struct usb_driver rndis_wlan_driver = {
2776 .name = "rndis_wlan",
2777 .id_table = products,
2778 .probe = usbnet_probe,
2779 .disconnect = usbnet_disconnect,
2780 .suspend = usbnet_suspend,
2781 .resume = usbnet_resume,
2784 static int __init rndis_wlan_init(void)
2786 return usb_register(&rndis_wlan_driver);
2788 module_init(rndis_wlan_init);
2790 static void __exit rndis_wlan_exit(void)
2792 usb_deregister(&rndis_wlan_driver);
2794 module_exit(rndis_wlan_exit);
2796 MODULE_AUTHOR("Bjorge Dijkstra");
2797 MODULE_AUTHOR("Jussi Kivilinna");
2798 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2799 MODULE_LICENSE("GPL");