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/ieee80211.h>
41 #include <linux/if_arp.h>
42 #include <linux/ctype.h>
43 #include <linux/spinlock.h>
44 #include <net/iw_handler.h>
45 #include <linux/usb/usbnet.h>
46 #include <linux/usb/rndis_host.h>
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 cpu_to_le32(0x00010107)
94 #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
96 #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
97 #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
98 #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
99 #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
100 #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
102 #define OID_802_3_PERMANENT_ADDRESS cpu_to_le32(0x01010101)
103 #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
104 #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
105 #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
107 #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
108 #define OID_802_11_SSID cpu_to_le32(0x0d010102)
109 #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
110 #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
111 #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
112 #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
113 #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
114 #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
115 #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
116 #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
117 #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
118 #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
119 #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
120 #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
121 #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
122 #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
123 #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
124 #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
125 #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
126 #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
127 #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
128 #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
129 #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
130 #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
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 cpu_to_le32(0xc0010011)
148 #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(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_LINK_UP (1<<0)
314 #define WORK_LINK_DOWN (1<<1)
315 #define WORK_SET_MULTICAST_LIST (1<<2)
317 #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
319 /* RNDIS device private data */
320 struct rndis_wext_private {
323 struct usbnet *usbdev;
325 struct workqueue_struct *workqueue;
326 struct delayed_work stats_work;
327 struct work_struct work;
328 struct mutex command_lock;
329 spinlock_t stats_lock;
330 unsigned long work_pending;
332 struct iw_statistics iwstats;
333 struct iw_statistics privstats;
341 /* module parameters */
342 char param_country[4];
343 int param_frameburst;
344 int param_afterburner;
345 int param_power_save;
346 int param_power_output;
347 int param_roamtrigger;
349 u32 param_workaround_interval;
354 struct ndis_80211_ssid essid;
356 /* encryption stuff */
357 int encr_tx_key_index;
358 char encr_keys[4][32];
366 int wpa_cipher_group;
368 u8 command_buffer[COMMAND_BUFFER_SIZE];
372 static const int rates_80211g[8] = { 6, 9, 12, 18, 24, 36, 48, 54 };
374 static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
376 static const unsigned char zero_bssid[ETH_ALEN] = {0,};
377 static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
381 static struct rndis_wext_private *get_rndis_wext_priv(struct usbnet *dev)
383 return (struct rndis_wext_private *)dev->driver_priv;
387 static u32 get_bcm4320_power(struct rndis_wext_private *priv)
389 return BCM4320_DEFAULT_TXPOWER *
390 bcm4320_power_output[priv->param_power_output] / 100;
394 /* translate error code */
395 static int rndis_error_status(__le32 rndis_status)
398 switch (rndis_status) {
399 case RNDIS_STATUS_SUCCESS:
402 case RNDIS_STATUS_FAILURE:
403 case RNDIS_STATUS_INVALID_DATA:
406 case RNDIS_STATUS_NOT_SUPPORTED:
409 case RNDIS_STATUS_ADAPTER_NOT_READY:
410 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
418 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
420 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
423 struct rndis_msg_hdr *header;
424 struct rndis_query *get;
425 struct rndis_query_c *get_c;
429 buflen = *len + sizeof(*u.get);
430 if (buflen < CONTROL_BUFFER_SIZE)
431 buflen = CONTROL_BUFFER_SIZE;
433 if (buflen > COMMAND_BUFFER_SIZE) {
434 u.buf = kmalloc(buflen, GFP_KERNEL);
438 u.buf = priv->command_buffer;
441 mutex_lock(&priv->command_lock);
443 memset(u.get, 0, sizeof *u.get);
444 u.get->msg_type = RNDIS_MSG_QUERY;
445 u.get->msg_len = cpu_to_le32(sizeof *u.get);
448 ret = rndis_command(dev, u.header, buflen);
450 ret = le32_to_cpu(u.get_c->len);
451 *len = (*len > ret) ? ret : *len;
452 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
453 ret = rndis_error_status(u.get_c->status);
456 mutex_unlock(&priv->command_lock);
458 if (u.buf != priv->command_buffer)
464 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
466 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
469 struct rndis_msg_hdr *header;
470 struct rndis_set *set;
471 struct rndis_set_c *set_c;
475 buflen = len + sizeof(*u.set);
476 if (buflen < CONTROL_BUFFER_SIZE)
477 buflen = CONTROL_BUFFER_SIZE;
479 if (buflen > COMMAND_BUFFER_SIZE) {
480 u.buf = kmalloc(buflen, GFP_KERNEL);
484 u.buf = priv->command_buffer;
487 mutex_lock(&priv->command_lock);
489 memset(u.set, 0, sizeof *u.set);
490 u.set->msg_type = RNDIS_MSG_SET;
491 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
493 u.set->len = cpu_to_le32(len);
494 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
495 u.set->handle = cpu_to_le32(0);
496 memcpy(u.buf + sizeof(*u.set), data, len);
498 ret = rndis_command(dev, u.header, buflen);
500 ret = rndis_error_status(u.set_c->status);
502 mutex_unlock(&priv->command_lock);
504 if (u.buf != priv->command_buffer)
511 * Specs say that we can only set config parameters only soon after device
513 * value_type: 0 = u32, 2 = unicode string
515 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
516 int value_type, void *value)
518 struct ndis_config_param *infobuf;
519 int value_len, info_len, param_len, ret, i;
524 value_len = sizeof(__le32);
525 else if (value_type == 2)
526 value_len = strlen(value) * sizeof(__le16);
530 param_len = strlen(param) * sizeof(__le16);
531 info_len = sizeof(*infobuf) + param_len + value_len;
536 infobuf = kmalloc(info_len, GFP_KERNEL);
542 /* extra 12 bytes are for padding (debug output) */
543 memset(infobuf, 0xCC, info_len + 12);
547 devdbg(dev, "setting config parameter: %s, value: %s",
550 devdbg(dev, "setting config parameter: %s, value: %d",
551 param, *(u32 *)value);
553 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
554 infobuf->name_length = cpu_to_le32(param_len);
555 infobuf->type = cpu_to_le32(value_type);
556 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
557 infobuf->value_length = cpu_to_le32(value_len);
559 /* simple string to unicode string conversion */
560 unibuf = (void *)infobuf + sizeof(*infobuf);
561 for (i = 0; i < param_len / sizeof(__le16); i++)
562 unibuf[i] = cpu_to_le16(param[i]);
564 if (value_type == 2) {
565 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
566 for (i = 0; i < value_len / sizeof(__le16); i++)
567 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
569 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
570 *dst_value = cpu_to_le32(*(u32 *)value);
574 devdbg(dev, "info buffer (len: %d):", info_len);
575 for (i = 0; i < info_len; i += 12) {
576 u32 *tmp = (u32 *)((u8 *)infobuf + i);
577 devdbg(dev, "%08X:%08X:%08X",
580 cpu_to_be32(tmp[2]));
584 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
587 devdbg(dev, "setting rndis config paramater failed, %d.", ret);
593 static int rndis_set_config_parameter_str(struct usbnet *dev,
594 char *param, char *value)
596 return(rndis_set_config_parameter(dev, param, 2, value));
599 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
600 char *param, u32 value)
602 return(rndis_set_config_parameter(dev, param, 0, &value));
607 * data conversion functions
609 static int level_to_qual(int level)
611 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
612 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
616 static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
622 /* see comment in wireless.h above the "struct iw_freq"
623 * definition for an explanation of this if
624 * NOTE: 1000000 is due to the kHz
626 if (dsconfig > 1000000) {
627 freq->m = dsconfig / 10;
632 /* convert from kHz to Hz */
637 static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
639 if (freq->m < 1000 && freq->e == 0) {
640 if (freq->m >= 1 && freq->m <= 14)
641 *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
647 for (i = freq->e; i > 0; i--)
660 add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
662 static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
667 ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
674 unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
676 memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
677 tmp[le32_to_cpu(ssid->length)] = 0;
678 devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
685 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
687 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
690 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
692 memcpy(&priv->essid, ssid, sizeof(priv->essid));
694 devdbg(usbdev, "set_essid: radio_on = 1");
701 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
706 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
709 memset(bssid, 0, ETH_ALEN);
714 static int get_association_info(struct usbnet *usbdev,
715 struct ndis_80211_assoc_info *info, int len)
717 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
721 static int is_associated(struct usbnet *usbdev)
726 ret = get_bssid(usbdev, bssid);
728 return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
732 static int disassociate(struct usbnet *usbdev, int reset_ssid)
734 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
735 struct ndis_80211_ssid ssid;
738 if (priv->radio_on) {
739 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
742 devdbg(usbdev, "disassociate: radio_on = 0");
749 /* disassociate causes radio to be turned off; if reset_ssid
750 * is given, set random ssid to enable radio */
752 ssid.length = cpu_to_le32(sizeof(ssid.essid));
753 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
755 ssid.essid[1] = 0xff;
756 for (i = 2; i < sizeof(ssid.essid); i++)
757 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
758 ret = set_essid(usbdev, &ssid);
764 static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
766 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
770 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
771 "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
773 if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
774 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
775 auth_mode = ndis_80211_auth_wpa2;
777 auth_mode = ndis_80211_auth_wpa2_psk;
778 } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
779 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
780 auth_mode = ndis_80211_auth_wpa;
781 else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
782 auth_mode = ndis_80211_auth_wpa_psk;
784 auth_mode = ndis_80211_auth_wpa_none;
785 } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
786 if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
787 auth_mode = ndis_80211_auth_auto_switch;
789 auth_mode = ndis_80211_auth_shared;
791 auth_mode = ndis_80211_auth_open;
793 tmp = cpu_to_le32(auth_mode);
794 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
797 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
801 priv->wpa_version = wpa_version;
802 priv->wpa_authalg = authalg;
807 static int set_priv_filter(struct usbnet *usbdev)
809 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
812 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
814 if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
815 priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
816 tmp = cpu_to_le32(ndis_80211_priv_8021x_wep);
818 tmp = cpu_to_le32(ndis_80211_priv_accept_all);
820 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
825 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
827 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
831 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
835 if (pairwise & IW_AUTH_CIPHER_CCMP)
836 encr_mode = ndis_80211_encr_ccmp_enabled;
837 else if (pairwise & IW_AUTH_CIPHER_TKIP)
838 encr_mode = ndis_80211_encr_tkip_enabled;
840 (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
841 encr_mode = ndis_80211_encr_wep_enabled;
842 else if (groupwise & IW_AUTH_CIPHER_CCMP)
843 encr_mode = ndis_80211_encr_ccmp_enabled;
844 else if (groupwise & IW_AUTH_CIPHER_TKIP)
845 encr_mode = ndis_80211_encr_tkip_enabled;
847 encr_mode = ndis_80211_encr_disabled;
849 tmp = cpu_to_le32(encr_mode);
850 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
853 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
857 priv->wpa_cipher_pair = pairwise;
858 priv->wpa_cipher_group = groupwise;
863 static int set_assoc_params(struct usbnet *usbdev)
865 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
867 set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
868 set_priv_filter(usbdev);
869 set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
875 static int set_infra_mode(struct usbnet *usbdev, int mode)
877 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
881 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
883 tmp = cpu_to_le32(mode);
884 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
887 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
891 /* NDIS drivers clear keys when infrastructure mode is
892 * changed. But Linux tools assume otherwise. So set the
894 if (priv->wpa_keymgmt == 0 ||
895 priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
896 for (i = 0; i < 4; i++) {
897 if (priv->encr_key_len[i] > 0)
898 add_wep_key(usbdev, priv->encr_keys[i],
899 priv->encr_key_len[i], i);
903 priv->infra_mode = mode;
908 static void set_default_iw_params(struct usbnet *usbdev)
910 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
912 priv->wpa_keymgmt = 0;
913 priv->wpa_version = 0;
915 set_infra_mode(usbdev, ndis_80211_infra_infra);
916 set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
917 IW_AUTH_ALG_OPEN_SYSTEM);
918 set_priv_filter(usbdev);
919 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
923 static int deauthenticate(struct usbnet *usbdev)
927 ret = disassociate(usbdev, 1);
928 set_default_iw_params(usbdev);
933 /* index must be 0 - N, as per NDIS */
934 static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
936 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
937 struct ndis_80211_wep_key ndis_key;
940 if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
943 memset(&ndis_key, 0, sizeof(ndis_key));
945 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
946 ndis_key.length = cpu_to_le32(key_len);
947 ndis_key.index = cpu_to_le32(index);
948 memcpy(&ndis_key.material, key, key_len);
950 if (index == priv->encr_tx_key_index) {
951 ndis_key.index |= cpu_to_le32(1 << 31);
952 ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
953 IW_AUTH_CIPHER_NONE);
955 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
959 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
962 devwarn(usbdev, "adding encryption key %d failed (%08X)",
967 priv->encr_key_len[index] = key_len;
968 memcpy(&priv->encr_keys[index], key, key_len);
974 /* remove_key is for both wep and wpa */
975 static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
977 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
978 struct ndis_80211_remove_key remove_key;
982 if (priv->encr_key_len[index] == 0)
985 priv->encr_key_len[index] = 0;
986 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
988 if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
989 priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
990 priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
991 priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
992 remove_key.size = cpu_to_le32(sizeof(remove_key));
993 remove_key.index = cpu_to_le32(index);
996 if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
997 remove_key.index |= cpu_to_le32(1 << 30);
998 memcpy(remove_key.bssid, bssid,
999 sizeof(remove_key.bssid));
1001 memset(remove_key.bssid, 0xff,
1002 sizeof(remove_key.bssid));
1004 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1005 sizeof(remove_key));
1009 keyindex = cpu_to_le32(index);
1010 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1014 "removing encryption key %d failed (%08X)",
1020 /* if it is transmit key, disable encryption */
1021 if (index == priv->encr_tx_key_index)
1022 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
1028 static void set_multicast_list(struct usbnet *usbdev)
1030 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1031 struct dev_mc_list *mclist;
1036 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1038 if (usbdev->net->flags & IFF_PROMISC) {
1039 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1040 RNDIS_PACKET_TYPE_ALL_LOCAL;
1041 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1042 usbdev->net->mc_count > priv->multicast_size) {
1043 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1044 } else if (usbdev->net->mc_count > 0) {
1045 size = min(priv->multicast_size, usbdev->net->mc_count);
1046 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1049 "couldn't alloc %d bytes of memory",
1054 mclist = usbdev->net->mc_list;
1055 for (i = 0; i < size && mclist; mclist = mclist->next) {
1056 if (mclist->dmi_addrlen != ETH_ALEN)
1059 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1063 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1065 if (ret == 0 && i > 0)
1066 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1068 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1070 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1071 i, priv->multicast_size, ret);
1076 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1079 devwarn(usbdev, "couldn't set packet filter: %08x",
1080 le32_to_cpu(filter));
1083 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1084 le32_to_cpu(filter), ret);
1089 * wireless extension handlers
1092 static int rndis_iw_commit(struct net_device *dev,
1093 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1100 static int rndis_iw_get_range(struct net_device *dev,
1101 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1103 struct iw_range *range = (struct iw_range *)extra;
1104 struct usbnet *usbdev = netdev_priv(dev);
1105 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1106 int len, ret, i, j, num, has_80211g_rates;
1110 devdbg(usbdev, "SIOCGIWRANGE");
1112 /* clear iw_range struct */
1113 memset(range, 0, sizeof(*range));
1114 wrqu->data.length = sizeof(*range);
1116 range->txpower_capa = IW_TXPOW_MWATT;
1117 range->num_txpower = 1;
1118 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1119 len = sizeof(tx_power);
1120 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1122 if (ret == 0 && le32_to_cpu(tx_power) != 0xFF)
1123 range->txpower[0] = le32_to_cpu(tx_power);
1125 range->txpower[0] = get_bcm4320_power(priv);
1127 range->txpower[0] = get_bcm4320_power(priv);
1129 len = sizeof(rates);
1130 ret = rndis_query_oid(usbdev, OID_802_11_SUPPORTED_RATES, &rates,
1132 has_80211g_rates = 0;
1135 for (i = 0; i < len; i++) {
1138 range->bitrate[j] = (rates[i] & 0x7f) * 500000;
1139 /* check for non 802.11b rates */
1140 if (range->bitrate[j] == 6000000 ||
1141 range->bitrate[j] == 9000000 ||
1142 (range->bitrate[j] >= 12000000 &&
1143 range->bitrate[j] != 22000000))
1144 has_80211g_rates = 1;
1147 range->num_bitrates = j;
1149 range->num_bitrates = 0;
1151 /* fill in 802.11g rates */
1152 if (has_80211g_rates) {
1153 num = range->num_bitrates;
1154 for (i = 0; i < ARRAY_SIZE(rates_80211g); i++) {
1155 for (j = 0; j < num; j++) {
1156 if (range->bitrate[j] ==
1157 rates_80211g[i] * 1000000)
1161 range->bitrate[range->num_bitrates++] =
1162 rates_80211g[i] * 1000000;
1163 if (range->num_bitrates == IW_MAX_BITRATES)
1167 /* estimated max real througput in bps */
1168 range->throughput = 54 * 1000 * 1000 / 2;
1170 /* ~35% more with afterburner */
1171 if (priv->param_afterburner)
1172 range->throughput = range->throughput / 100 * 135;
1174 /* estimated max real througput in bps */
1175 range->throughput = 11 * 1000 * 1000 / 2;
1178 range->num_channels = 14;
1180 for (i = 0; (i < 14) && (i < IW_MAX_FREQUENCIES); i++) {
1181 range->freq[i].i = i + 1;
1182 range->freq[i].m = ieee80211_dsss_chan_to_freq(i + 1) * 100000;
1183 range->freq[i].e = 1;
1185 range->num_frequency = i;
1188 range->max_rts = 2347;
1189 range->min_frag = 256;
1190 range->max_frag = 2346;
1192 range->max_qual.qual = 100;
1193 range->max_qual.level = 154;
1194 range->max_qual.updated = IW_QUAL_QUAL_UPDATED
1195 | IW_QUAL_LEVEL_UPDATED
1196 | IW_QUAL_NOISE_INVALID;
1198 range->we_version_compiled = WIRELESS_EXT;
1199 range->we_version_source = WIRELESS_EXT;
1201 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
1202 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
1207 static int rndis_iw_get_name(struct net_device *dev,
1208 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1210 struct usbnet *usbdev = netdev_priv(dev);
1211 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1213 strcpy(wrqu->name, priv->name);
1218 static int rndis_iw_set_essid(struct net_device *dev,
1219 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1221 struct ndis_80211_ssid ssid;
1222 int length = wrqu->essid.length;
1223 struct usbnet *usbdev = netdev_priv(dev);
1225 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1226 wrqu->essid.flags, wrqu->essid.length, essid);
1228 if (length > NDIS_802_11_LENGTH_SSID)
1229 length = NDIS_802_11_LENGTH_SSID;
1231 ssid.length = cpu_to_le32(length);
1233 memcpy(ssid.essid, essid, length);
1235 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
1237 set_assoc_params(usbdev);
1239 if (!wrqu->essid.flags || length == 0)
1240 return disassociate(usbdev, 1);
1242 return set_essid(usbdev, &ssid);
1246 static int rndis_iw_get_essid(struct net_device *dev,
1247 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1249 struct ndis_80211_ssid ssid;
1250 struct usbnet *usbdev = netdev_priv(dev);
1253 ret = get_essid(usbdev, &ssid);
1255 if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
1256 wrqu->essid.flags = 1;
1257 wrqu->essid.length = le32_to_cpu(ssid.length);
1258 memcpy(essid, ssid.essid, wrqu->essid.length);
1259 essid[wrqu->essid.length] = 0;
1261 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1262 wrqu->essid.flags = 0;
1263 wrqu->essid.length = 0;
1265 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1270 static int rndis_iw_get_bssid(struct net_device *dev,
1271 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1273 struct usbnet *usbdev = netdev_priv(dev);
1274 unsigned char bssid[ETH_ALEN];
1277 ret = get_bssid(usbdev, bssid);
1280 devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
1282 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1284 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1285 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1291 static int rndis_iw_set_bssid(struct net_device *dev,
1292 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1294 struct usbnet *usbdev = netdev_priv(dev);
1295 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1298 devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
1300 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1302 /* user apps may set ap's mac address, which is not required;
1303 * they may fail to work if this function fails, so return
1306 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1312 static int rndis_iw_set_auth(struct net_device *dev,
1313 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1315 struct iw_param *p = &wrqu->param;
1316 struct usbnet *usbdev = netdev_priv(dev);
1317 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1318 int ret = -ENOTSUPP;
1320 switch (p->flags & IW_AUTH_INDEX) {
1321 case IW_AUTH_WPA_VERSION:
1322 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1323 priv->wpa_version = p->value;
1327 case IW_AUTH_CIPHER_PAIRWISE:
1328 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1329 priv->wpa_cipher_pair = p->value;
1333 case IW_AUTH_CIPHER_GROUP:
1334 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1335 priv->wpa_cipher_group = p->value;
1339 case IW_AUTH_KEY_MGMT:
1340 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1341 priv->wpa_keymgmt = p->value;
1345 case IW_AUTH_TKIP_COUNTERMEASURES:
1346 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1351 case IW_AUTH_DROP_UNENCRYPTED:
1352 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1356 case IW_AUTH_80211_AUTH_ALG:
1357 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1358 priv->wpa_authalg = p->value;
1362 case IW_AUTH_WPA_ENABLED:
1363 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1364 if (wrqu->param.value)
1365 deauthenticate(usbdev);
1369 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1370 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1375 case IW_AUTH_ROAMING_CONTROL:
1376 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1380 case IW_AUTH_PRIVACY_INVOKED:
1381 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1382 wrqu->param.flags & IW_AUTH_INDEX);
1386 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
1387 p->flags & IW_AUTH_INDEX, p->value);
1393 static int rndis_iw_get_auth(struct net_device *dev,
1394 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1396 struct iw_param *p = &wrqu->param;
1397 struct usbnet *usbdev = netdev_priv(dev);
1398 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1400 switch (p->flags & IW_AUTH_INDEX) {
1401 case IW_AUTH_WPA_VERSION:
1402 p->value = priv->wpa_version;
1404 case IW_AUTH_CIPHER_PAIRWISE:
1405 p->value = priv->wpa_cipher_pair;
1407 case IW_AUTH_CIPHER_GROUP:
1408 p->value = priv->wpa_cipher_group;
1410 case IW_AUTH_KEY_MGMT:
1411 p->value = priv->wpa_keymgmt;
1413 case IW_AUTH_80211_AUTH_ALG:
1414 p->value = priv->wpa_authalg;
1417 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1418 wrqu->param.flags & IW_AUTH_INDEX);
1425 static int rndis_iw_get_mode(struct net_device *dev,
1426 struct iw_request_info *info,
1427 union iwreq_data *wrqu, char *extra)
1429 struct usbnet *usbdev = netdev_priv(dev);
1430 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1432 switch (priv->infra_mode) {
1433 case ndis_80211_infra_adhoc:
1434 wrqu->mode = IW_MODE_ADHOC;
1436 case ndis_80211_infra_infra:
1437 wrqu->mode = IW_MODE_INFRA;
1439 /*case ndis_80211_infra_auto_unknown:*/
1441 wrqu->mode = IW_MODE_AUTO;
1444 devdbg(usbdev, "SIOCGIWMODE: %08x", wrqu->mode);
1449 static int rndis_iw_set_mode(struct net_device *dev,
1450 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1452 struct usbnet *usbdev = netdev_priv(dev);
1455 devdbg(usbdev, "SIOCSIWMODE: %08x", wrqu->mode);
1457 switch (wrqu->mode) {
1459 mode = ndis_80211_infra_adhoc;
1462 mode = ndis_80211_infra_infra;
1464 /*case IW_MODE_AUTO:*/
1466 mode = ndis_80211_infra_auto_unknown;
1470 return set_infra_mode(usbdev, mode);
1474 static int rndis_iw_set_encode(struct net_device *dev,
1475 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1477 struct usbnet *usbdev = netdev_priv(dev);
1478 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1479 int ret, index, key_len;
1482 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1484 /* iwconfig gives index as 1 - N */
1488 index = priv->encr_tx_key_index;
1490 if (index < 0 || index >= 4) {
1491 devwarn(usbdev, "encryption index out of range (%u)", index);
1495 /* remove key if disabled */
1496 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1497 if (remove_key(usbdev, index, NULL))
1503 /* global encryption state (for all keys) */
1504 if (wrqu->data.flags & IW_ENCODE_OPEN)
1505 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1506 IW_AUTH_ALG_OPEN_SYSTEM);
1507 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1508 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1509 IW_AUTH_ALG_SHARED_KEY);
1513 if (wrqu->data.length > 0) {
1514 key_len = wrqu->data.length;
1517 /* must be set as tx key */
1518 if (priv->encr_key_len[index] == 0)
1520 key_len = priv->encr_key_len[index];
1521 key = priv->encr_keys[index];
1522 priv->encr_tx_key_index = index;
1525 if (add_wep_key(usbdev, key, key_len, index) != 0)
1528 if (index == priv->encr_tx_key_index)
1529 /* ndis drivers want essid to be set after setting encr */
1530 set_essid(usbdev, &priv->essid);
1536 static int rndis_iw_set_encode_ext(struct net_device *dev,
1537 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1539 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1540 struct usbnet *usbdev = netdev_priv(dev);
1541 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1542 struct ndis_80211_key ndis_key;
1546 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1548 /* iwconfig gives index as 1 - N */
1552 keyidx = priv->encr_tx_key_index;
1554 if (keyidx < 0 || keyidx >= 4)
1557 if (ext->alg == WPA_ALG_WEP) {
1558 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1559 priv->encr_tx_key_index = keyidx;
1560 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1563 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1564 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1565 return remove_key(usbdev, keyidx, NULL);
1567 if (ext->key_len > sizeof(ndis_key.material))
1570 memset(&ndis_key, 0, sizeof(ndis_key));
1572 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1573 sizeof(ndis_key.material) + ext->key_len);
1574 ndis_key.length = cpu_to_le32(ext->key_len);
1575 ndis_key.index = cpu_to_le32(keyidx);
1577 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1578 memcpy(ndis_key.rsc, ext->rx_seq, 6);
1579 ndis_key.index |= cpu_to_le32(1 << 29);
1582 addr = ext->addr.sa_data;
1583 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1585 if (priv->infra_mode == ndis_80211_infra_adhoc)
1586 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1588 get_bssid(usbdev, ndis_key.bssid);
1591 ndis_key.index |= cpu_to_le32(1 << 30);
1592 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1595 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1596 ndis_key.index |= cpu_to_le32(1 << 31);
1598 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1599 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1600 * different order than NDIS spec, so swap the order here. */
1601 memcpy(ndis_key.material, ext->key, 16);
1602 memcpy(ndis_key.material + 16, ext->key + 24, 8);
1603 memcpy(ndis_key.material + 24, ext->key + 16, 8);
1605 memcpy(ndis_key.material, ext->key, ext->key_len);
1607 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1608 le32_to_cpu(ndis_key.size));
1609 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1613 priv->encr_key_len[keyidx] = ext->key_len;
1614 memcpy(&priv->encr_keys[keyidx], ndis_key.material, ext->key_len);
1615 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1616 priv->encr_tx_key_index = keyidx;
1622 static int rndis_iw_set_scan(struct net_device *dev,
1623 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1625 struct usbnet *usbdev = netdev_priv(dev);
1626 union iwreq_data evt;
1630 devdbg(usbdev, "SIOCSIWSCAN");
1632 if (wrqu->data.flags == 0) {
1633 tmp = cpu_to_le32(1);
1634 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1637 evt.data.length = 0;
1638 wireless_send_event(dev, SIOCGIWSCAN, &evt, NULL);
1644 static char *rndis_translate_scan(struct net_device *dev,
1645 struct iw_request_info *info, char *cev,
1647 struct ndis_80211_bssid_ex *bssid)
1649 struct usbnet *usbdev = netdev_priv(dev);
1652 int bssid_len, ie_len, i;
1654 struct iw_event iwe;
1655 unsigned char sbuf[32];
1657 bssid_len = le32_to_cpu(bssid->length);
1659 devdbg(usbdev, "BSSID %pM", bssid->mac);
1660 iwe.cmd = SIOCGIWAP;
1661 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1662 memcpy(iwe.u.ap_addr.sa_data, bssid->mac, ETH_ALEN);
1663 cev = iwe_stream_add_event(info, cev, end_buf, &iwe, IW_EV_ADDR_LEN);
1665 devdbg(usbdev, "SSID(%d) %s", le32_to_cpu(bssid->ssid.length),
1667 iwe.cmd = SIOCGIWESSID;
1668 iwe.u.essid.length = le32_to_cpu(bssid->ssid.length);
1669 iwe.u.essid.flags = 1;
1670 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, bssid->ssid.essid);
1672 devdbg(usbdev, "MODE %d", le32_to_cpu(bssid->net_infra));
1673 iwe.cmd = SIOCGIWMODE;
1674 switch (le32_to_cpu(bssid->net_infra)) {
1675 case ndis_80211_infra_adhoc:
1676 iwe.u.mode = IW_MODE_ADHOC;
1678 case ndis_80211_infra_infra:
1679 iwe.u.mode = IW_MODE_INFRA;
1681 /*case ndis_80211_infra_auto_unknown:*/
1683 iwe.u.mode = IW_MODE_AUTO;
1686 cev = iwe_stream_add_event(info, cev, end_buf, &iwe, IW_EV_UINT_LEN);
1688 devdbg(usbdev, "FREQ %d kHz", le32_to_cpu(bssid->config.ds_config));
1689 iwe.cmd = SIOCGIWFREQ;
1690 dsconfig_to_freq(le32_to_cpu(bssid->config.ds_config), &iwe.u.freq);
1691 cev = iwe_stream_add_event(info, cev, end_buf, &iwe, IW_EV_FREQ_LEN);
1693 devdbg(usbdev, "QUAL %d", le32_to_cpu(bssid->rssi));
1695 iwe.u.qual.qual = level_to_qual(le32_to_cpu(bssid->rssi));
1696 iwe.u.qual.level = le32_to_cpu(bssid->rssi);
1697 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
1698 | IW_QUAL_LEVEL_UPDATED
1699 | IW_QUAL_NOISE_INVALID;
1700 cev = iwe_stream_add_event(info, cev, end_buf, &iwe, IW_EV_QUAL_LEN);
1702 devdbg(usbdev, "ENCODE %d", le32_to_cpu(bssid->privacy));
1703 iwe.cmd = SIOCGIWENCODE;
1704 iwe.u.data.length = 0;
1705 if (le32_to_cpu(bssid->privacy) == ndis_80211_priv_accept_all)
1706 iwe.u.data.flags = IW_ENCODE_DISABLED;
1708 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1710 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, NULL);
1712 devdbg(usbdev, "RATES:");
1713 current_val = cev + iwe_stream_lcp_len(info);
1714 iwe.cmd = SIOCGIWRATE;
1715 for (i = 0; i < sizeof(bssid->rates); i++) {
1716 if (bssid->rates[i] & 0x7f) {
1717 iwe.u.bitrate.value =
1718 ((bssid->rates[i] & 0x7f) *
1720 devdbg(usbdev, " %d", iwe.u.bitrate.value);
1721 current_val = iwe_stream_add_value(info, cev,
1722 current_val, end_buf, &iwe,
1727 if ((current_val - cev) > iwe_stream_lcp_len(info))
1730 beacon = le32_to_cpu(bssid->config.beacon_period);
1731 devdbg(usbdev, "BCN_INT %d", beacon);
1732 iwe.cmd = IWEVCUSTOM;
1733 snprintf(sbuf, sizeof(sbuf), "bcn_int=%d", beacon);
1734 iwe.u.data.length = strlen(sbuf);
1735 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, sbuf);
1737 atim = le32_to_cpu(bssid->config.atim_window);
1738 devdbg(usbdev, "ATIM %d", atim);
1739 iwe.cmd = IWEVCUSTOM;
1740 snprintf(sbuf, sizeof(sbuf), "atim=%u", atim);
1741 iwe.u.data.length = strlen(sbuf);
1742 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, sbuf);
1744 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1745 ie_len = min(bssid_len - (int)sizeof(*bssid),
1746 (int)le32_to_cpu(bssid->ie_length));
1747 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1748 while (ie_len >= 2 && 2 + ie[1] <= ie_len) {
1749 if ((ie[0] == WLAN_EID_GENERIC && ie[1] >= 4 &&
1750 memcmp(ie + 2, "\x00\x50\xf2\x01", 4) == 0) ||
1751 ie[0] == WLAN_EID_RSN) {
1752 devdbg(usbdev, "IE: WPA%d",
1753 (ie[0] == WLAN_EID_RSN) ? 2 : 1);
1754 iwe.cmd = IWEVGENIE;
1755 /* arbitrary cut-off at 64 */
1756 iwe.u.data.length = min(ie[1] + 2, 64);
1757 cev = iwe_stream_add_point(info, cev, end_buf, &iwe, ie);
1760 ie_len -= 2 + ie[1];
1768 static int rndis_iw_get_scan(struct net_device *dev,
1769 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1771 struct usbnet *usbdev = netdev_priv(dev);
1774 struct ndis_80211_bssid_list_ex *bssid_list;
1775 struct ndis_80211_bssid_ex *bssid;
1776 int ret = -EINVAL, len, count, bssid_len;
1778 devdbg(usbdev, "SIOCGIWSCAN");
1780 len = CONTROL_BUFFER_SIZE;
1781 buf = kmalloc(len, GFP_KERNEL);
1787 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1793 bssid = bssid_list->bssid;
1794 bssid_len = le32_to_cpu(bssid->length);
1795 count = le32_to_cpu(bssid_list->num_items);
1796 devdbg(usbdev, "SIOCGIWSCAN: %d BSSIDs found", count);
1798 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1799 cev = rndis_translate_scan(dev, info, cev,
1800 extra + IW_SCAN_MAX_DATA, bssid);
1801 bssid = (void *)bssid + bssid_len;
1802 bssid_len = le32_to_cpu(bssid->length);
1807 wrqu->data.length = cev - extra;
1808 wrqu->data.flags = 0;
1814 static int rndis_iw_set_genie(struct net_device *dev,
1815 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1817 struct usbnet *usbdev = netdev_priv(dev);
1818 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1824 for (j = 0; j < wrqu->data.length; j += 8)
1826 "SIOCSIWGENIE %04x - "
1827 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1828 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1829 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1831 /* clear existing IEs */
1832 if (priv->wpa_ie_len) {
1833 kfree(priv->wpa_ie);
1834 priv->wpa_ie_len = 0;
1838 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1840 priv->wpa_ie_len = wrqu->data.length;
1841 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1848 static int rndis_iw_get_genie(struct net_device *dev,
1849 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1851 struct usbnet *usbdev = netdev_priv(dev);
1852 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1854 devdbg(usbdev, "SIOCGIWGENIE");
1856 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1857 wrqu->data.length = 0;
1861 if (wrqu->data.length < priv->wpa_ie_len)
1864 wrqu->data.length = priv->wpa_ie_len;
1865 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1871 static int rndis_iw_set_rts(struct net_device *dev,
1872 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1874 struct usbnet *usbdev = netdev_priv(dev);
1876 devdbg(usbdev, "SIOCSIWRTS");
1878 tmp = cpu_to_le32(wrqu->rts.value);
1879 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1884 static int rndis_iw_get_rts(struct net_device *dev,
1885 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1887 struct usbnet *usbdev = netdev_priv(dev);
1892 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1894 wrqu->rts.value = le32_to_cpu(tmp);
1895 wrqu->rts.flags = 1;
1896 wrqu->rts.disabled = 0;
1899 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1905 static int rndis_iw_set_frag(struct net_device *dev,
1906 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1908 struct usbnet *usbdev = netdev_priv(dev);
1911 devdbg(usbdev, "SIOCSIWFRAG");
1913 tmp = cpu_to_le32(wrqu->frag.value);
1914 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1919 static int rndis_iw_get_frag(struct net_device *dev,
1920 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1922 struct usbnet *usbdev = netdev_priv(dev);
1927 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1930 wrqu->frag.value = le32_to_cpu(tmp);
1931 wrqu->frag.flags = 1;
1932 wrqu->frag.disabled = 0;
1934 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1939 static int rndis_iw_set_nick(struct net_device *dev,
1940 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1942 struct usbnet *usbdev = netdev_priv(dev);
1943 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1945 devdbg(usbdev, "SIOCSIWNICK");
1947 priv->nick_len = wrqu->data.length;
1948 if (priv->nick_len > 32)
1949 priv->nick_len = 32;
1951 memcpy(priv->nick, extra, priv->nick_len);
1956 static int rndis_iw_get_nick(struct net_device *dev,
1957 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1959 struct usbnet *usbdev = netdev_priv(dev);
1960 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1962 wrqu->data.flags = 1;
1963 wrqu->data.length = priv->nick_len;
1964 memcpy(extra, priv->nick, priv->nick_len);
1966 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1972 static int rndis_iw_set_freq(struct net_device *dev,
1973 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1975 struct usbnet *usbdev = netdev_priv(dev);
1976 struct ndis_80211_conf config;
1977 unsigned int dsconfig;
1980 /* this OID is valid only when not associated */
1981 if (is_associated(usbdev))
1985 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1988 len = sizeof(config);
1989 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1991 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1995 config.ds_config = cpu_to_le32(dsconfig);
1997 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1998 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
2003 static int rndis_iw_get_freq(struct net_device *dev,
2004 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2006 struct usbnet *usbdev = netdev_priv(dev);
2007 struct ndis_80211_conf config;
2010 len = sizeof(config);
2011 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
2013 dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
2015 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
2020 static int rndis_iw_get_txpower(struct net_device *dev,
2021 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2023 struct usbnet *usbdev = netdev_priv(dev);
2024 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2028 if (priv->radio_on) {
2029 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2030 len = sizeof(tx_power);
2031 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2036 /* fake incase not supported */
2037 tx_power = cpu_to_le32(get_bcm4320_power(priv));
2039 wrqu->txpower.flags = IW_TXPOW_MWATT;
2040 wrqu->txpower.value = le32_to_cpu(tx_power);
2041 wrqu->txpower.disabled = 0;
2043 wrqu->txpower.flags = IW_TXPOW_MWATT;
2044 wrqu->txpower.value = 0;
2045 wrqu->txpower.disabled = 1;
2048 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
2054 static int rndis_iw_set_txpower(struct net_device *dev,
2055 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2057 struct usbnet *usbdev = netdev_priv(dev);
2058 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2059 __le32 tx_power = 0;
2062 if (!wrqu->txpower.disabled) {
2063 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
2064 tx_power = cpu_to_le32(wrqu->txpower.value);
2065 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
2066 if (wrqu->txpower.value > 20)
2067 tx_power = cpu_to_le32(128);
2068 else if (wrqu->txpower.value < -43)
2069 tx_power = cpu_to_le32(127);
2072 tmp = wrqu->txpower.value;
2075 tx_power = cpu_to_le32((unsigned char)tmp);
2080 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
2082 if (le32_to_cpu(tx_power) != 0) {
2083 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2084 /* turn radio on first */
2085 if (!priv->radio_on)
2086 disassociate(usbdev, 1);
2088 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2089 &tx_power, sizeof(tx_power));
2094 /* txpower unsupported, just turn radio on */
2095 if (!priv->radio_on)
2096 return disassociate(usbdev, 1);
2097 return 0; /* all ready on */
2101 /* tx_power == 0, turn off radio */
2102 return disassociate(usbdev, 0);
2106 static int rndis_iw_get_rate(struct net_device *dev,
2107 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2109 struct usbnet *usbdev = netdev_priv(dev);
2114 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2116 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2117 wrqu->bitrate.disabled = 0;
2118 wrqu->bitrate.flags = 1;
2124 static int rndis_iw_set_mlme(struct net_device *dev,
2125 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2127 struct usbnet *usbdev = netdev_priv(dev);
2128 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2129 struct iw_mlme *mlme = (struct iw_mlme *)extra;
2130 unsigned char bssid[ETH_ALEN];
2132 get_bssid(usbdev, bssid);
2134 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2137 switch (mlme->cmd) {
2138 case IW_MLME_DEAUTH:
2139 return deauthenticate(usbdev);
2140 case IW_MLME_DISASSOC:
2141 return disassociate(usbdev, priv->radio_on);
2150 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2152 struct usbnet *usbdev = netdev_priv(dev);
2153 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2154 unsigned long flags;
2156 spin_lock_irqsave(&priv->stats_lock, flags);
2157 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2158 spin_unlock_irqrestore(&priv->stats_lock, flags);
2160 return &priv->iwstats;
2164 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2165 static const iw_handler rndis_iw_handler[] =
2167 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2168 IW_IOCTL(SIOCGIWNAME) = rndis_iw_get_name,
2169 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2170 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2171 IW_IOCTL(SIOCSIWMODE) = rndis_iw_set_mode,
2172 IW_IOCTL(SIOCGIWMODE) = rndis_iw_get_mode,
2173 IW_IOCTL(SIOCGIWRANGE) = rndis_iw_get_range,
2174 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2175 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2176 IW_IOCTL(SIOCSIWSCAN) = rndis_iw_set_scan,
2177 IW_IOCTL(SIOCGIWSCAN) = rndis_iw_get_scan,
2178 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2179 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2180 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2181 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2182 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2183 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2184 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2185 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2186 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2187 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2188 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2189 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2190 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2191 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2192 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2193 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2194 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2195 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2198 static const iw_handler rndis_wext_private_handler[] = {
2201 static const struct iw_priv_args rndis_wext_private_args[] = {
2205 static const struct iw_handler_def rndis_iw_handlers = {
2206 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2207 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2208 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2209 .standard = (iw_handler *)rndis_iw_handler,
2210 .private = (iw_handler *)rndis_wext_private_handler,
2211 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2212 .get_wireless_stats = rndis_get_wireless_stats,
2216 static void rndis_wext_worker(struct work_struct *work)
2218 struct rndis_wext_private *priv =
2219 container_of(work, struct rndis_wext_private, work);
2220 struct usbnet *usbdev = priv->usbdev;
2221 union iwreq_data evt;
2222 unsigned char bssid[ETH_ALEN];
2223 struct ndis_80211_assoc_info *info;
2224 int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
2227 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
2228 netif_carrier_on(usbdev->net);
2230 info = kzalloc(assoc_size, GFP_KERNEL);
2234 /* Get association info IEs from device and send them back to
2236 ret = get_association_info(usbdev, info, assoc_size);
2238 evt.data.length = le32_to_cpu(info->req_ie_length);
2239 if (evt.data.length > 0) {
2240 offset = le32_to_cpu(info->offset_req_ies);
2241 wireless_send_event(usbdev->net,
2242 IWEVASSOCREQIE, &evt,
2243 (char *)info + offset);
2246 evt.data.length = le32_to_cpu(info->resp_ie_length);
2247 if (evt.data.length > 0) {
2248 offset = le32_to_cpu(info->offset_resp_ies);
2249 wireless_send_event(usbdev->net,
2250 IWEVASSOCRESPIE, &evt,
2251 (char *)info + offset);
2258 ret = get_bssid(usbdev, bssid);
2261 evt.data.length = 0;
2262 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2263 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2267 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
2268 netif_carrier_off(usbdev->net);
2271 evt.data.length = 0;
2272 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2273 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2276 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2277 set_multicast_list(usbdev);
2280 static void rndis_wext_set_multicast_list(struct net_device *dev)
2282 struct usbnet *usbdev = netdev_priv(dev);
2283 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2285 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2288 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2289 queue_work(priv->workqueue, &priv->work);
2292 static void rndis_wext_link_change(struct usbnet *usbdev, int state)
2294 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2296 /* queue work to avoid recursive calls into rndis_command */
2297 set_bit(state ? WORK_LINK_UP : WORK_LINK_DOWN, &priv->work_pending);
2298 queue_work(priv->workqueue, &priv->work);
2302 static int rndis_wext_get_caps(struct usbnet *usbdev)
2307 } networks_supported;
2308 int len, retval, i, n;
2310 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2312 /* determine if supports setting txpower */
2313 len = sizeof(tx_power);
2314 retval = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2316 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2317 priv->caps |= CAP_SUPPORT_TXPOWER;
2319 /* determine supported modes */
2320 len = sizeof(networks_supported);
2321 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2322 &networks_supported, &len);
2324 n = le32_to_cpu(networks_supported.num_items);
2327 for (i = 0; i < n; i++) {
2328 switch (le32_to_cpu(networks_supported.items[i])) {
2329 case ndis_80211_type_freq_hop:
2330 case ndis_80211_type_direct_seq:
2331 priv->caps |= CAP_MODE_80211B;
2333 case ndis_80211_type_ofdm_a:
2334 priv->caps |= CAP_MODE_80211A;
2336 case ndis_80211_type_ofdm_g:
2337 priv->caps |= CAP_MODE_80211G;
2341 if (priv->caps & CAP_MODE_80211A)
2342 strcat(priv->name, "a");
2343 if (priv->caps & CAP_MODE_80211B)
2344 strcat(priv->name, "b");
2345 if (priv->caps & CAP_MODE_80211G)
2346 strcat(priv->name, "g");
2353 #define STATS_UPDATE_JIFFIES (HZ)
2354 static void rndis_update_wireless_stats(struct work_struct *work)
2356 struct rndis_wext_private *priv =
2357 container_of(work, struct rndis_wext_private, stats_work.work);
2358 struct usbnet *usbdev = priv->usbdev;
2359 struct iw_statistics iwstats;
2362 unsigned long flags;
2363 int update_jiffies = STATS_UPDATE_JIFFIES;
2366 spin_lock_irqsave(&priv->stats_lock, flags);
2367 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2368 spin_unlock_irqrestore(&priv->stats_lock, flags);
2370 /* only update stats when connected */
2371 if (!is_associated(usbdev)) {
2372 iwstats.qual.qual = 0;
2373 iwstats.qual.level = 0;
2374 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2375 | IW_QUAL_LEVEL_UPDATED
2376 | IW_QUAL_NOISE_INVALID
2377 | IW_QUAL_QUAL_INVALID
2378 | IW_QUAL_LEVEL_INVALID;
2383 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2385 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2388 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2389 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2390 iwstats.qual.level = le32_to_cpu(rssi);
2391 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2392 | IW_QUAL_LEVEL_UPDATED
2393 | IW_QUAL_NOISE_INVALID;
2396 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2399 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2401 iwstats.discard.misc += le32_to_cpu(tmp);
2404 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2406 iwstats.discard.misc += le32_to_cpu(tmp);
2409 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2411 iwstats.discard.misc += le32_to_cpu(tmp);
2413 /* Workaround transfer stalls on poor quality links.
2414 * TODO: find right way to fix these stalls (as stalls do not happen
2415 * with ndiswrapper/windows driver). */
2416 if (iwstats.qual.qual <= 25) {
2417 /* Decrease stats worker interval to catch stalls.
2418 * faster. Faster than 400-500ms causes packet loss,
2419 * Slower doesn't catch stalls fast enough.
2421 j = msecs_to_jiffies(priv->param_workaround_interval);
2422 if (j > STATS_UPDATE_JIFFIES)
2423 j = STATS_UPDATE_JIFFIES;
2428 /* Send scan OID. Use of both OIDs is required to get device
2431 tmp = cpu_to_le32(1);
2432 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2435 len = CONTROL_BUFFER_SIZE;
2436 buf = kmalloc(len, GFP_KERNEL);
2440 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2444 spin_lock_irqsave(&priv->stats_lock, flags);
2445 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2446 spin_unlock_irqrestore(&priv->stats_lock, flags);
2448 if (update_jiffies >= HZ)
2449 update_jiffies = round_jiffies_relative(update_jiffies);
2451 j = round_jiffies_relative(update_jiffies);
2452 if (abs(j - update_jiffies) <= 10)
2456 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2460 static int bcm4320_early_init(struct usbnet *usbdev)
2462 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2465 /* Early initialization settings, setting these won't have effect
2466 * if called after generic_rndis_bind().
2469 priv->param_country[0] = modparam_country[0];
2470 priv->param_country[1] = modparam_country[1];
2471 priv->param_country[2] = 0;
2472 priv->param_frameburst = modparam_frameburst;
2473 priv->param_afterburner = modparam_afterburner;
2474 priv->param_power_save = modparam_power_save;
2475 priv->param_power_output = modparam_power_output;
2476 priv->param_roamtrigger = modparam_roamtrigger;
2477 priv->param_roamdelta = modparam_roamdelta;
2479 priv->param_country[0] = toupper(priv->param_country[0]);
2480 priv->param_country[1] = toupper(priv->param_country[1]);
2481 /* doesn't support EU as country code, use FI instead */
2482 if (!strcmp(priv->param_country, "EU"))
2483 strcpy(priv->param_country, "FI");
2485 if (priv->param_power_save < 0)
2486 priv->param_power_save = 0;
2487 else if (priv->param_power_save > 2)
2488 priv->param_power_save = 2;
2490 if (priv->param_power_output < 0)
2491 priv->param_power_output = 0;
2492 else if (priv->param_power_output > 3)
2493 priv->param_power_output = 3;
2495 if (priv->param_roamtrigger < -80)
2496 priv->param_roamtrigger = -80;
2497 else if (priv->param_roamtrigger > -60)
2498 priv->param_roamtrigger = -60;
2500 if (priv->param_roamdelta < 0)
2501 priv->param_roamdelta = 0;
2502 else if (priv->param_roamdelta > 2)
2503 priv->param_roamdelta = 2;
2505 if (modparam_workaround_interval < 0)
2506 priv->param_workaround_interval = 500;
2508 priv->param_workaround_interval = modparam_workaround_interval;
2510 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
2511 rndis_set_config_parameter_str(usbdev, "FrameBursting",
2512 priv->param_frameburst ? "1" : "0");
2513 rndis_set_config_parameter_str(usbdev, "Afterburner",
2514 priv->param_afterburner ? "1" : "0");
2515 sprintf(buf, "%d", priv->param_power_save);
2516 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
2517 sprintf(buf, "%d", priv->param_power_output);
2518 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
2519 sprintf(buf, "%d", priv->param_roamtrigger);
2520 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
2521 sprintf(buf, "%d", priv->param_roamdelta);
2522 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
2527 /* same as rndis_netdev_ops but with local multicast handler */
2528 static const struct net_device_ops rndis_wext_netdev_ops = {
2529 .ndo_open = usbnet_open,
2530 .ndo_stop = usbnet_stop,
2531 .ndo_start_xmit = usbnet_start_xmit,
2532 .ndo_tx_timeout = usbnet_tx_timeout,
2533 .ndo_set_mac_address = eth_mac_addr,
2534 .ndo_validate_addr = eth_validate_addr,
2535 .ndo_set_multicast_list = rndis_wext_set_multicast_list,
2539 static int rndis_wext_bind(struct usbnet *usbdev, struct usb_interface *intf)
2541 struct rndis_wext_private *priv;
2545 /* allocate rndis private data */
2546 priv = kzalloc(sizeof(struct rndis_wext_private), GFP_KERNEL);
2550 /* These have to be initialized before calling generic_rndis_bind().
2551 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2553 usbdev->driver_priv = priv;
2554 strcpy(priv->name, "IEEE802.11");
2555 usbdev->net->wireless_handlers = &rndis_iw_handlers;
2556 priv->usbdev = usbdev;
2558 mutex_init(&priv->command_lock);
2559 spin_lock_init(&priv->stats_lock);
2561 /* try bind rndis_host */
2562 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2566 /* generic_rndis_bind set packet filter to multicast_all+
2567 * promisc mode which doesn't work well for our devices (device
2568 * picks up rssi to closest station instead of to access point).
2570 * rndis_host wants to avoid all OID as much as possible
2571 * so do promisc/multicast handling in rndis_wext.
2573 usbdev->net->netdev_ops = &rndis_wext_netdev_ops;
2575 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2576 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2580 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
2582 priv->multicast_size = le32_to_cpu(tmp);
2583 if (retval < 0 || priv->multicast_size < 0)
2584 priv->multicast_size = 0;
2585 if (priv->multicast_size > 0)
2586 usbdev->net->flags |= IFF_MULTICAST;
2588 usbdev->net->flags &= ~IFF_MULTICAST;
2590 priv->iwstats.qual.qual = 0;
2591 priv->iwstats.qual.level = 0;
2592 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2593 | IW_QUAL_LEVEL_UPDATED
2594 | IW_QUAL_NOISE_INVALID
2595 | IW_QUAL_QUAL_INVALID
2596 | IW_QUAL_LEVEL_INVALID;
2598 rndis_wext_get_caps(usbdev);
2599 set_default_iw_params(usbdev);
2603 disassociate(usbdev, 1);
2604 netif_carrier_off(usbdev->net);
2606 /* because rndis_command() sleeps we need to use workqueue */
2607 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2608 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2609 queue_delayed_work(priv->workqueue, &priv->stats_work,
2610 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2611 INIT_WORK(&priv->work, rndis_wext_worker);
2621 static void rndis_wext_unbind(struct usbnet *usbdev, struct usb_interface *intf)
2623 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2625 /* turn radio off */
2626 disassociate(usbdev, 0);
2628 cancel_delayed_work_sync(&priv->stats_work);
2629 cancel_work_sync(&priv->work);
2630 flush_workqueue(priv->workqueue);
2631 destroy_workqueue(priv->workqueue);
2633 if (priv && priv->wpa_ie_len)
2634 kfree(priv->wpa_ie);
2637 rndis_unbind(usbdev, intf);
2641 static int rndis_wext_reset(struct usbnet *usbdev)
2643 return deauthenticate(usbdev);
2647 static const struct driver_info bcm4320b_info = {
2648 .description = "Wireless RNDIS device, BCM4320b based",
2649 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2650 .bind = rndis_wext_bind,
2651 .unbind = rndis_wext_unbind,
2652 .status = rndis_status,
2653 .rx_fixup = rndis_rx_fixup,
2654 .tx_fixup = rndis_tx_fixup,
2655 .reset = rndis_wext_reset,
2656 .early_init = bcm4320_early_init,
2657 .link_change = rndis_wext_link_change,
2660 static const struct driver_info bcm4320a_info = {
2661 .description = "Wireless RNDIS device, BCM4320a based",
2662 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2663 .bind = rndis_wext_bind,
2664 .unbind = rndis_wext_unbind,
2665 .status = rndis_status,
2666 .rx_fixup = rndis_rx_fixup,
2667 .tx_fixup = rndis_tx_fixup,
2668 .reset = rndis_wext_reset,
2669 .early_init = bcm4320_early_init,
2670 .link_change = rndis_wext_link_change,
2673 static const struct driver_info rndis_wext_info = {
2674 .description = "Wireless RNDIS device",
2675 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2676 .bind = rndis_wext_bind,
2677 .unbind = rndis_wext_unbind,
2678 .status = rndis_status,
2679 .rx_fixup = rndis_rx_fixup,
2680 .tx_fixup = rndis_tx_fixup,
2681 .reset = rndis_wext_reset,
2682 .early_init = bcm4320_early_init,
2683 .link_change = rndis_wext_link_change,
2686 /*-------------------------------------------------------------------------*/
2688 static const struct usb_device_id products [] = {
2689 #define RNDIS_MASTER_INTERFACE \
2690 .bInterfaceClass = USB_CLASS_COMM, \
2691 .bInterfaceSubClass = 2 /* ACM */, \
2692 .bInterfaceProtocol = 0x0ff
2694 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2695 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2698 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2699 | USB_DEVICE_ID_MATCH_DEVICE,
2701 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
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 = 0x011b, /* U.S. Robotics USR5421 */
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 = 0x011b, /* Belkin F5D7051 */
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,
2721 .idVendor = 0x1799, /* Belkin has two vendor ids */
2722 .idProduct = 0x011b, /* Belkin F5D7051 */
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 = 0x0014, /* Linksys WUSB54GSv2 */
2730 RNDIS_MASTER_INTERFACE,
2731 .driver_info = (unsigned long) &bcm4320b_info,
2733 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2734 | USB_DEVICE_ID_MATCH_DEVICE,
2736 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2737 RNDIS_MASTER_INTERFACE,
2738 .driver_info = (unsigned long) &bcm4320b_info,
2740 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2741 | USB_DEVICE_ID_MATCH_DEVICE,
2743 .idProduct = 0x1717, /* Asus WL169gE */
2744 RNDIS_MASTER_INTERFACE,
2745 .driver_info = (unsigned long) &bcm4320b_info,
2747 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2748 | USB_DEVICE_ID_MATCH_DEVICE,
2750 .idProduct = 0xd11b, /* Eminent EM4045 */
2751 RNDIS_MASTER_INTERFACE,
2752 .driver_info = (unsigned long) &bcm4320b_info,
2754 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2755 | USB_DEVICE_ID_MATCH_DEVICE,
2757 .idProduct = 0x0715, /* BT Voyager 1055 */
2758 RNDIS_MASTER_INTERFACE,
2759 .driver_info = (unsigned long) &bcm4320b_info,
2761 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2762 * parameters available, hardware probably contain older firmware version with
2763 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2766 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2767 | USB_DEVICE_ID_MATCH_DEVICE,
2769 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2770 RNDIS_MASTER_INTERFACE,
2771 .driver_info = (unsigned long) &bcm4320a_info,
2773 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2774 | USB_DEVICE_ID_MATCH_DEVICE,
2776 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2777 RNDIS_MASTER_INTERFACE,
2778 .driver_info = (unsigned long) &bcm4320a_info,
2780 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2781 | USB_DEVICE_ID_MATCH_DEVICE,
2783 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2784 RNDIS_MASTER_INTERFACE,
2785 .driver_info = (unsigned long) &bcm4320a_info,
2787 /* Generic Wireless RNDIS devices that we don't have exact
2788 * idVendor/idProduct/chip yet.
2791 /* RNDIS is MSFT's un-official variant of CDC ACM */
2792 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2793 .driver_info = (unsigned long) &rndis_wext_info,
2795 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2796 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2797 .driver_info = (unsigned long) &rndis_wext_info,
2801 MODULE_DEVICE_TABLE(usb, products);
2803 static struct usb_driver rndis_wlan_driver = {
2804 .name = "rndis_wlan",
2805 .id_table = products,
2806 .probe = usbnet_probe,
2807 .disconnect = usbnet_disconnect,
2808 .suspend = usbnet_suspend,
2809 .resume = usbnet_resume,
2812 static int __init rndis_wlan_init(void)
2814 return usb_register(&rndis_wlan_driver);
2816 module_init(rndis_wlan_init);
2818 static void __exit rndis_wlan_exit(void)
2820 usb_deregister(&rndis_wlan_driver);
2822 module_exit(rndis_wlan_exit);
2824 MODULE_AUTHOR("Bjorge Dijkstra");
2825 MODULE_AUTHOR("Jussi Kivilinna");
2826 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2827 MODULE_LICENSE("GPL");