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_LINK_UP (1<<0)
314 #define WORK_LINK_DOWN (1<<1)
315 #define WORK_SET_MULTICAST_LIST (1<<2)
317 /* RNDIS device private data */
318 struct rndis_wext_private {
321 struct usbnet *usbdev;
323 struct workqueue_struct *workqueue;
324 struct delayed_work stats_work;
325 struct work_struct work;
326 struct mutex command_lock;
327 spinlock_t stats_lock;
328 unsigned long work_pending;
330 struct iw_statistics iwstats;
331 struct iw_statistics privstats;
339 /* module parameters */
340 char param_country[4];
341 int param_frameburst;
342 int param_afterburner;
343 int param_power_save;
344 int param_power_output;
345 int param_roamtrigger;
347 u32 param_workaround_interval;
352 struct ndis_80211_ssid essid;
354 /* encryption stuff */
355 int encr_tx_key_index;
356 char encr_keys[4][32];
364 int wpa_cipher_group;
368 static const int freq_chan[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
369 2447, 2452, 2457, 2462, 2467, 2472, 2484 };
371 static const int rates_80211g[8] = { 6, 9, 12, 18, 24, 36, 48, 54 };
373 static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
375 static const unsigned char zero_bssid[ETH_ALEN] = {0,};
376 static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
380 static struct rndis_wext_private *get_rndis_wext_priv(struct usbnet *dev)
382 return (struct rndis_wext_private *)dev->driver_priv;
386 static u32 get_bcm4320_power(struct rndis_wext_private *priv)
388 return BCM4320_DEFAULT_TXPOWER *
389 bcm4320_power_output[priv->param_power_output] / 100;
393 /* translate error code */
394 static int rndis_error_status(__le32 rndis_status)
397 switch (rndis_status) {
398 case RNDIS_STATUS_SUCCESS:
401 case RNDIS_STATUS_FAILURE:
402 case RNDIS_STATUS_INVALID_DATA:
405 case RNDIS_STATUS_NOT_SUPPORTED:
408 case RNDIS_STATUS_ADAPTER_NOT_READY:
409 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
417 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
419 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
422 struct rndis_msg_hdr *header;
423 struct rndis_query *get;
424 struct rndis_query_c *get_c;
428 buflen = *len + sizeof(*u.get);
429 if (buflen < CONTROL_BUFFER_SIZE)
430 buflen = CONTROL_BUFFER_SIZE;
431 u.buf = kmalloc(buflen, GFP_KERNEL);
434 memset(u.get, 0, sizeof *u.get);
435 u.get->msg_type = RNDIS_MSG_QUERY;
436 u.get->msg_len = ccpu2(sizeof *u.get);
439 mutex_lock(&priv->command_lock);
440 ret = rndis_command(dev, u.header);
441 mutex_unlock(&priv->command_lock);
444 ret = le32_to_cpu(u.get_c->len);
445 *len = (*len > ret) ? ret : *len;
446 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
447 ret = rndis_error_status(u.get_c->status);
455 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
457 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
460 struct rndis_msg_hdr *header;
461 struct rndis_set *set;
462 struct rndis_set_c *set_c;
466 buflen = len + sizeof(*u.set);
467 if (buflen < CONTROL_BUFFER_SIZE)
468 buflen = CONTROL_BUFFER_SIZE;
469 u.buf = kmalloc(buflen, GFP_KERNEL);
473 memset(u.set, 0, sizeof *u.set);
474 u.set->msg_type = RNDIS_MSG_SET;
475 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
477 u.set->len = cpu_to_le32(len);
478 u.set->offset = ccpu2(sizeof(*u.set) - 8);
479 u.set->handle = ccpu2(0);
480 memcpy(u.buf + sizeof(*u.set), data, len);
482 mutex_lock(&priv->command_lock);
483 ret = rndis_command(dev, u.header);
484 mutex_unlock(&priv->command_lock);
487 ret = rndis_error_status(u.set_c->status);
495 * Specs say that we can only set config parameters only soon after device
497 * value_type: 0 = u32, 2 = unicode string
499 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
500 int value_type, void *value)
502 struct ndis_config_param *infobuf;
503 int value_len, info_len, param_len, ret, i;
508 value_len = sizeof(__le32);
509 else if (value_type == 2)
510 value_len = strlen(value) * sizeof(__le16);
514 param_len = strlen(param) * sizeof(__le16);
515 info_len = sizeof(*infobuf) + param_len + value_len;
520 infobuf = kmalloc(info_len, GFP_KERNEL);
526 /* extra 12 bytes are for padding (debug output) */
527 memset(infobuf, 0xCC, info_len + 12);
531 devdbg(dev, "setting config parameter: %s, value: %s",
534 devdbg(dev, "setting config parameter: %s, value: %d",
535 param, *(u32 *)value);
537 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
538 infobuf->name_length = cpu_to_le32(param_len);
539 infobuf->type = cpu_to_le32(value_type);
540 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
541 infobuf->value_length = cpu_to_le32(value_len);
543 /* simple string to unicode string conversion */
544 unibuf = (void *)infobuf + sizeof(*infobuf);
545 for (i = 0; i < param_len / sizeof(__le16); i++)
546 unibuf[i] = cpu_to_le16(param[i]);
548 if (value_type == 2) {
549 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
550 for (i = 0; i < value_len / sizeof(__le16); i++)
551 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
553 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
554 *dst_value = cpu_to_le32(*(u32 *)value);
558 devdbg(dev, "info buffer (len: %d):", info_len);
559 for (i = 0; i < info_len; i += 12) {
560 u32 *tmp = (u32 *)((u8 *)infobuf + i);
561 devdbg(dev, "%08X:%08X:%08X",
564 cpu_to_be32(tmp[2]));
568 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
571 devdbg(dev, "setting rndis config paramater failed, %d.", ret);
577 static int rndis_set_config_parameter_str(struct usbnet *dev,
578 char *param, char *value)
580 return(rndis_set_config_parameter(dev, param, 2, value));
583 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
584 char *param, u32 value)
586 return(rndis_set_config_parameter(dev, param, 0, &value));
591 * data conversion functions
593 static int level_to_qual(int level)
595 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
596 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
600 static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
606 /* see comment in wireless.h above the "struct iw_freq"
607 * definition for an explanation of this if
608 * NOTE: 1000000 is due to the kHz
610 if (dsconfig > 1000000) {
611 freq->m = dsconfig / 10;
616 /* convert from kHz to Hz */
621 static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
623 if (freq->m < 1000 && freq->e == 0) {
625 freq->m <= (sizeof(freq_chan) / sizeof(freq_chan[0])))
626 *dsconfig = freq_chan[freq->m - 1] * 1000;
632 for (i = freq->e; i > 0; i--)
645 add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
647 static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
652 ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
659 unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
661 memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
662 tmp[le32_to_cpu(ssid->length)] = 0;
663 devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
670 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
672 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
675 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
677 memcpy(&priv->essid, ssid, sizeof(priv->essid));
679 devdbg(usbdev, "set_essid: radio_on = 1");
686 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
691 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
694 memset(bssid, 0, ETH_ALEN);
699 static int get_association_info(struct usbnet *usbdev,
700 struct ndis_80211_assoc_info *info, int len)
702 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
706 static int is_associated(struct usbnet *usbdev)
711 ret = get_bssid(usbdev, bssid);
713 return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
717 static int disassociate(struct usbnet *usbdev, int reset_ssid)
719 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
720 struct ndis_80211_ssid ssid;
723 if (priv->radio_on) {
724 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
727 devdbg(usbdev, "disassociate: radio_on = 0");
734 /* disassociate causes radio to be turned off; if reset_ssid
735 * is given, set random ssid to enable radio */
737 ssid.length = cpu_to_le32(sizeof(ssid.essid));
738 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
740 ssid.essid[1] = 0xff;
741 for (i = 2; i < sizeof(ssid.essid); i++)
742 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
743 ret = set_essid(usbdev, &ssid);
749 static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
751 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
755 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
756 "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
758 if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
759 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
760 auth_mode = ndis_80211_auth_wpa2;
762 auth_mode = ndis_80211_auth_wpa2_psk;
763 } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
764 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
765 auth_mode = ndis_80211_auth_wpa;
766 else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
767 auth_mode = ndis_80211_auth_wpa_psk;
769 auth_mode = ndis_80211_auth_wpa_none;
770 } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
771 if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
772 auth_mode = ndis_80211_auth_auto_switch;
774 auth_mode = ndis_80211_auth_shared;
776 auth_mode = ndis_80211_auth_open;
778 tmp = cpu_to_le32(auth_mode);
779 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
782 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
786 priv->wpa_version = wpa_version;
787 priv->wpa_authalg = authalg;
792 static int set_priv_filter(struct usbnet *usbdev)
794 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
797 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
799 if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
800 priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
801 tmp = cpu_to_le32(ndis_80211_priv_8021x_wep);
803 tmp = cpu_to_le32(ndis_80211_priv_accept_all);
805 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
810 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
812 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
816 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
820 if (pairwise & IW_AUTH_CIPHER_CCMP)
821 encr_mode = ndis_80211_encr_ccmp_enabled;
822 else if (pairwise & IW_AUTH_CIPHER_TKIP)
823 encr_mode = ndis_80211_encr_tkip_enabled;
825 (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
826 encr_mode = ndis_80211_encr_wep_enabled;
827 else if (groupwise & IW_AUTH_CIPHER_CCMP)
828 encr_mode = ndis_80211_encr_ccmp_enabled;
829 else if (groupwise & IW_AUTH_CIPHER_TKIP)
830 encr_mode = ndis_80211_encr_tkip_enabled;
832 encr_mode = ndis_80211_encr_disabled;
834 tmp = cpu_to_le32(encr_mode);
835 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
838 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
842 priv->wpa_cipher_pair = pairwise;
843 priv->wpa_cipher_group = groupwise;
848 static int set_assoc_params(struct usbnet *usbdev)
850 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
852 set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
853 set_priv_filter(usbdev);
854 set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
860 static int set_infra_mode(struct usbnet *usbdev, int mode)
862 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
866 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
868 tmp = cpu_to_le32(mode);
869 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
872 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
876 /* NDIS drivers clear keys when infrastructure mode is
877 * changed. But Linux tools assume otherwise. So set the
879 if (priv->wpa_keymgmt == 0 ||
880 priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
881 for (i = 0; i < 4; i++) {
882 if (priv->encr_key_len[i] > 0)
883 add_wep_key(usbdev, priv->encr_keys[i],
884 priv->encr_key_len[i], i);
888 priv->infra_mode = mode;
893 static void set_default_iw_params(struct usbnet *usbdev)
895 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
897 priv->wpa_keymgmt = 0;
898 priv->wpa_version = 0;
900 set_infra_mode(usbdev, ndis_80211_infra_infra);
901 set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
902 IW_AUTH_ALG_OPEN_SYSTEM);
903 set_priv_filter(usbdev);
904 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
908 static int deauthenticate(struct usbnet *usbdev)
912 ret = disassociate(usbdev, 1);
913 set_default_iw_params(usbdev);
918 /* index must be 0 - N, as per NDIS */
919 static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
921 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
922 struct ndis_80211_wep_key ndis_key;
925 if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
928 memset(&ndis_key, 0, sizeof(ndis_key));
930 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
931 ndis_key.length = cpu_to_le32(key_len);
932 ndis_key.index = cpu_to_le32(index);
933 memcpy(&ndis_key.material, key, key_len);
935 if (index == priv->encr_tx_key_index) {
936 ndis_key.index |= cpu_to_le32(1 << 31);
937 ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
938 IW_AUTH_CIPHER_NONE);
940 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
944 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
947 devwarn(usbdev, "adding encryption key %d failed (%08X)",
952 priv->encr_key_len[index] = key_len;
953 memcpy(&priv->encr_keys[index], key, key_len);
959 /* remove_key is for both wep and wpa */
960 static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
962 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
963 struct ndis_80211_remove_key remove_key;
967 if (priv->encr_key_len[index] == 0)
970 priv->encr_key_len[index] = 0;
971 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
973 if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
974 priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
975 priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
976 priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
977 remove_key.size = cpu_to_le32(sizeof(remove_key));
978 remove_key.index = cpu_to_le32(index);
981 if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
982 remove_key.index |= cpu_to_le32(1 << 30);
983 memcpy(remove_key.bssid, bssid,
984 sizeof(remove_key.bssid));
986 memset(remove_key.bssid, 0xff,
987 sizeof(remove_key.bssid));
989 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
994 keyindex = cpu_to_le32(index);
995 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
999 "removing encryption key %d failed (%08X)",
1005 /* if it is transmit key, disable encryption */
1006 if (index == priv->encr_tx_key_index)
1007 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
1013 static void set_multicast_list(struct usbnet *usbdev)
1015 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1016 struct dev_mc_list *mclist;
1021 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1023 if (usbdev->net->flags & IFF_PROMISC) {
1024 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1025 RNDIS_PACKET_TYPE_ALL_LOCAL;
1026 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1027 usbdev->net->mc_count > priv->multicast_size) {
1028 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1029 } else if (usbdev->net->mc_count > 0) {
1030 size = min(priv->multicast_size, usbdev->net->mc_count);
1031 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1034 "couldn't alloc %d bytes of memory",
1039 mclist = usbdev->net->mc_list;
1040 for (i = 0; i < size && mclist; mclist = mclist->next) {
1041 if (mclist->dmi_addrlen != ETH_ALEN)
1044 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1048 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1050 if (ret == 0 && i > 0)
1051 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1053 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1055 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1056 i, priv->multicast_size, ret);
1061 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1064 devwarn(usbdev, "couldn't set packet filter: %08x",
1065 le32_to_cpu(filter));
1068 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1069 le32_to_cpu(filter), ret);
1074 * wireless extension handlers
1077 static int rndis_iw_commit(struct net_device *dev,
1078 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1085 static int rndis_iw_get_range(struct net_device *dev,
1086 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1088 struct iw_range *range = (struct iw_range *)extra;
1089 struct usbnet *usbdev = dev->priv;
1090 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1091 int len, ret, i, j, num, has_80211g_rates;
1095 devdbg(usbdev, "SIOCGIWRANGE");
1097 /* clear iw_range struct */
1098 memset(range, 0, sizeof(*range));
1099 wrqu->data.length = sizeof(*range);
1101 range->txpower_capa = IW_TXPOW_MWATT;
1102 range->num_txpower = 1;
1103 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1104 len = sizeof(tx_power);
1105 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1107 if (ret == 0 && le32_to_cpu(tx_power) != 0xFF)
1108 range->txpower[0] = le32_to_cpu(tx_power);
1110 range->txpower[0] = get_bcm4320_power(priv);
1112 range->txpower[0] = get_bcm4320_power(priv);
1114 len = sizeof(rates);
1115 ret = rndis_query_oid(usbdev, OID_802_11_SUPPORTED_RATES, &rates,
1117 has_80211g_rates = 0;
1120 for (i = 0; i < len; i++) {
1123 range->bitrate[j] = (rates[i] & 0x7f) * 500000;
1124 /* check for non 802.11b rates */
1125 if (range->bitrate[j] == 6000000 ||
1126 range->bitrate[j] == 9000000 ||
1127 (range->bitrate[j] >= 12000000 &&
1128 range->bitrate[j] != 22000000))
1129 has_80211g_rates = 1;
1132 range->num_bitrates = j;
1134 range->num_bitrates = 0;
1136 /* fill in 802.11g rates */
1137 if (has_80211g_rates) {
1138 num = range->num_bitrates;
1139 for (i = 0; i < ARRAY_SIZE(rates_80211g); i++) {
1140 for (j = 0; j < num; j++) {
1141 if (range->bitrate[j] ==
1142 rates_80211g[i] * 1000000)
1146 range->bitrate[range->num_bitrates++] =
1147 rates_80211g[i] * 1000000;
1148 if (range->num_bitrates == IW_MAX_BITRATES)
1152 /* estimated max real througput in bps */
1153 range->throughput = 54 * 1000 * 1000 / 2;
1155 /* ~35% more with afterburner */
1156 if (priv->param_afterburner)
1157 range->throughput = range->throughput / 100 * 135;
1159 /* estimated max real througput in bps */
1160 range->throughput = 11 * 1000 * 1000 / 2;
1163 range->num_channels = (sizeof(freq_chan)/sizeof(freq_chan[0]));
1165 for (i = 0; i < (sizeof(freq_chan)/sizeof(freq_chan[0])) &&
1166 i < IW_MAX_FREQUENCIES; i++) {
1167 range->freq[i].i = i + 1;
1168 range->freq[i].m = freq_chan[i] * 100000;
1169 range->freq[i].e = 1;
1171 range->num_frequency = i;
1174 range->max_rts = 2347;
1175 range->min_frag = 256;
1176 range->max_frag = 2346;
1178 range->max_qual.qual = 100;
1179 range->max_qual.level = 154;
1180 range->max_qual.updated = IW_QUAL_QUAL_UPDATED
1181 | IW_QUAL_LEVEL_UPDATED
1182 | IW_QUAL_NOISE_INVALID;
1184 range->we_version_compiled = WIRELESS_EXT;
1185 range->we_version_source = WIRELESS_EXT;
1187 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
1188 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
1193 static int rndis_iw_get_name(struct net_device *dev,
1194 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1196 struct usbnet *usbdev = dev->priv;
1197 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1199 strcpy(wrqu->name, priv->name);
1204 static int rndis_iw_set_essid(struct net_device *dev,
1205 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1207 struct ndis_80211_ssid ssid;
1208 int length = wrqu->essid.length;
1209 struct usbnet *usbdev = dev->priv;
1211 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1212 wrqu->essid.flags, wrqu->essid.length, essid);
1214 if (length > NDIS_802_11_LENGTH_SSID)
1215 length = NDIS_802_11_LENGTH_SSID;
1217 ssid.length = cpu_to_le32(length);
1219 memcpy(ssid.essid, essid, length);
1221 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
1223 set_assoc_params(usbdev);
1225 if (!wrqu->essid.flags || length == 0)
1226 return disassociate(usbdev, 1);
1228 return set_essid(usbdev, &ssid);
1232 static int rndis_iw_get_essid(struct net_device *dev,
1233 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1235 struct ndis_80211_ssid ssid;
1236 struct usbnet *usbdev = dev->priv;
1239 ret = get_essid(usbdev, &ssid);
1241 if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
1242 wrqu->essid.flags = 1;
1243 wrqu->essid.length = le32_to_cpu(ssid.length);
1244 memcpy(essid, ssid.essid, wrqu->essid.length);
1245 essid[wrqu->essid.length] = 0;
1247 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1248 wrqu->essid.flags = 0;
1249 wrqu->essid.length = 0;
1251 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1256 static int rndis_iw_get_bssid(struct net_device *dev,
1257 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1259 struct usbnet *usbdev = dev->priv;
1260 unsigned char bssid[ETH_ALEN];
1262 DECLARE_MAC_BUF(mac);
1264 ret = get_bssid(usbdev, bssid);
1267 devdbg(usbdev, "SIOCGIWAP: %s", print_mac(mac, bssid));
1269 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1271 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1272 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1278 static int rndis_iw_set_bssid(struct net_device *dev,
1279 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1281 struct usbnet *usbdev = dev->priv;
1282 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1283 DECLARE_MAC_BUF(mac);
1286 devdbg(usbdev, "SIOCSIWAP: %s", print_mac(mac, bssid));
1288 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1290 /* user apps may set ap's mac address, which is not required;
1291 * they may fail to work if this function fails, so return
1294 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1300 static int rndis_iw_set_auth(struct net_device *dev,
1301 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1303 struct iw_param *p = &wrqu->param;
1304 struct usbnet *usbdev = dev->priv;
1305 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1306 int ret = -ENOTSUPP;
1308 switch (p->flags & IW_AUTH_INDEX) {
1309 case IW_AUTH_WPA_VERSION:
1310 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1311 priv->wpa_version = p->value;
1315 case IW_AUTH_CIPHER_PAIRWISE:
1316 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1317 priv->wpa_cipher_pair = p->value;
1321 case IW_AUTH_CIPHER_GROUP:
1322 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1323 priv->wpa_cipher_group = p->value;
1327 case IW_AUTH_KEY_MGMT:
1328 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1329 priv->wpa_keymgmt = p->value;
1333 case IW_AUTH_TKIP_COUNTERMEASURES:
1334 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1339 case IW_AUTH_DROP_UNENCRYPTED:
1340 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1344 case IW_AUTH_80211_AUTH_ALG:
1345 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1346 priv->wpa_authalg = p->value;
1350 case IW_AUTH_WPA_ENABLED:
1351 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1352 if (wrqu->param.value)
1353 deauthenticate(usbdev);
1357 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1358 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1363 case IW_AUTH_ROAMING_CONTROL:
1364 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1368 case IW_AUTH_PRIVACY_INVOKED:
1369 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1370 wrqu->param.flags & IW_AUTH_INDEX);
1374 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
1375 p->flags & IW_AUTH_INDEX, p->value);
1381 static int rndis_iw_get_auth(struct net_device *dev,
1382 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1384 struct iw_param *p = &wrqu->param;
1385 struct usbnet *usbdev = dev->priv;
1386 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1388 switch (p->flags & IW_AUTH_INDEX) {
1389 case IW_AUTH_WPA_VERSION:
1390 p->value = priv->wpa_version;
1392 case IW_AUTH_CIPHER_PAIRWISE:
1393 p->value = priv->wpa_cipher_pair;
1395 case IW_AUTH_CIPHER_GROUP:
1396 p->value = priv->wpa_cipher_group;
1398 case IW_AUTH_KEY_MGMT:
1399 p->value = priv->wpa_keymgmt;
1401 case IW_AUTH_80211_AUTH_ALG:
1402 p->value = priv->wpa_authalg;
1405 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1406 wrqu->param.flags & IW_AUTH_INDEX);
1413 static int rndis_iw_get_mode(struct net_device *dev,
1414 struct iw_request_info *info,
1415 union iwreq_data *wrqu, char *extra)
1417 struct usbnet *usbdev = dev->priv;
1418 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1420 switch (priv->infra_mode) {
1421 case ndis_80211_infra_adhoc:
1422 wrqu->mode = IW_MODE_ADHOC;
1424 case ndis_80211_infra_infra:
1425 wrqu->mode = IW_MODE_INFRA;
1427 /*case ndis_80211_infra_auto_unknown:*/
1429 wrqu->mode = IW_MODE_AUTO;
1432 devdbg(usbdev, "SIOCGIWMODE: %08x", wrqu->mode);
1437 static int rndis_iw_set_mode(struct net_device *dev,
1438 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1440 struct usbnet *usbdev = dev->priv;
1443 devdbg(usbdev, "SIOCSIWMODE: %08x", wrqu->mode);
1445 switch (wrqu->mode) {
1447 mode = ndis_80211_infra_adhoc;
1450 mode = ndis_80211_infra_infra;
1452 /*case IW_MODE_AUTO:*/
1454 mode = ndis_80211_infra_auto_unknown;
1458 return set_infra_mode(usbdev, mode);
1462 static int rndis_iw_set_encode(struct net_device *dev,
1463 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1465 struct usbnet *usbdev = dev->priv;
1466 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1467 int ret, index, key_len;
1470 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1472 /* iwconfig gives index as 1 - N */
1476 index = priv->encr_tx_key_index;
1478 if (index < 0 || index >= 4) {
1479 devwarn(usbdev, "encryption index out of range (%u)", index);
1483 /* remove key if disabled */
1484 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1485 if (remove_key(usbdev, index, NULL))
1491 /* global encryption state (for all keys) */
1492 if (wrqu->data.flags & IW_ENCODE_OPEN)
1493 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1494 IW_AUTH_ALG_OPEN_SYSTEM);
1495 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1496 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1497 IW_AUTH_ALG_SHARED_KEY);
1501 if (wrqu->data.length > 0) {
1502 key_len = wrqu->data.length;
1505 /* must be set as tx key */
1506 if (priv->encr_key_len[index] == 0)
1508 key_len = priv->encr_key_len[index];
1509 key = priv->encr_keys[index];
1510 priv->encr_tx_key_index = index;
1513 if (add_wep_key(usbdev, key, key_len, index) != 0)
1516 if (index == priv->encr_tx_key_index)
1517 /* ndis drivers want essid to be set after setting encr */
1518 set_essid(usbdev, &priv->essid);
1524 static int rndis_iw_set_encode_ext(struct net_device *dev,
1525 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1527 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1528 struct usbnet *usbdev = dev->priv;
1529 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1530 struct ndis_80211_key ndis_key;
1534 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1536 /* iwconfig gives index as 1 - N */
1540 keyidx = priv->encr_tx_key_index;
1542 if (keyidx < 0 || keyidx >= 4)
1545 if (ext->alg == WPA_ALG_WEP) {
1546 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1547 priv->encr_tx_key_index = keyidx;
1548 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1551 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1552 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1553 return remove_key(usbdev, keyidx, NULL);
1555 if (ext->key_len > sizeof(ndis_key.material))
1558 memset(&ndis_key, 0, sizeof(ndis_key));
1560 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1561 sizeof(ndis_key.material) + ext->key_len);
1562 ndis_key.length = cpu_to_le32(ext->key_len);
1563 ndis_key.index = cpu_to_le32(keyidx);
1565 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1566 memcpy(ndis_key.rsc, ext->rx_seq, 6);
1567 ndis_key.index |= cpu_to_le32(1 << 29);
1570 addr = ext->addr.sa_data;
1571 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1573 if (priv->infra_mode == ndis_80211_infra_adhoc)
1574 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1576 get_bssid(usbdev, ndis_key.bssid);
1579 ndis_key.index |= cpu_to_le32(1 << 30);
1580 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1583 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1584 ndis_key.index |= cpu_to_le32(1 << 31);
1586 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1587 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1588 * different order than NDIS spec, so swap the order here. */
1589 memcpy(ndis_key.material, ext->key, 16);
1590 memcpy(ndis_key.material + 16, ext->key + 24, 8);
1591 memcpy(ndis_key.material + 24, ext->key + 16, 8);
1593 memcpy(ndis_key.material, ext->key, ext->key_len);
1595 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1596 le32_to_cpu(ndis_key.size));
1597 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1601 priv->encr_key_len[keyidx] = ext->key_len;
1602 memcpy(&priv->encr_keys[keyidx], ndis_key.material, ext->key_len);
1603 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1604 priv->encr_tx_key_index = keyidx;
1610 static int rndis_iw_set_scan(struct net_device *dev,
1611 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1613 struct iw_param *param = &wrqu->param;
1614 struct usbnet *usbdev = dev->priv;
1615 union iwreq_data evt;
1619 devdbg(usbdev, "SIOCSIWSCAN");
1621 if (param->flags == 0) {
1623 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1626 evt.data.length = 0;
1627 wireless_send_event(dev, SIOCGIWSCAN, &evt, NULL);
1633 static char *rndis_translate_scan(struct net_device *dev,
1634 char *cev, char *end_buf, struct ndis_80211_bssid_ex *bssid)
1637 struct usbnet *usbdev = dev->priv;
1639 struct ieee80211_info_element *ie;
1641 int bssid_len, ie_len, i;
1643 struct iw_event iwe;
1644 unsigned char sbuf[32];
1645 DECLARE_MAC_BUF(mac);
1647 bssid_len = le32_to_cpu(bssid->length);
1649 devdbg(usbdev, "BSSID %s", print_mac(mac, bssid->mac));
1650 iwe.cmd = SIOCGIWAP;
1651 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1652 memcpy(iwe.u.ap_addr.sa_data, bssid->mac, ETH_ALEN);
1653 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_ADDR_LEN);
1655 devdbg(usbdev, "SSID(%d) %s", le32_to_cpu(bssid->ssid.length),
1657 iwe.cmd = SIOCGIWESSID;
1658 iwe.u.essid.length = le32_to_cpu(bssid->ssid.length);
1659 iwe.u.essid.flags = 1;
1660 cev = iwe_stream_add_point(cev, end_buf, &iwe, bssid->ssid.essid);
1662 devdbg(usbdev, "MODE %d", le32_to_cpu(bssid->net_infra));
1663 iwe.cmd = SIOCGIWMODE;
1664 switch (le32_to_cpu(bssid->net_infra)) {
1665 case ndis_80211_infra_adhoc:
1666 iwe.u.mode = IW_MODE_ADHOC;
1668 case ndis_80211_infra_infra:
1669 iwe.u.mode = IW_MODE_INFRA;
1671 /*case ndis_80211_infra_auto_unknown:*/
1673 iwe.u.mode = IW_MODE_AUTO;
1676 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_UINT_LEN);
1678 devdbg(usbdev, "FREQ %d kHz", le32_to_cpu(bssid->config.ds_config));
1679 iwe.cmd = SIOCGIWFREQ;
1680 dsconfig_to_freq(le32_to_cpu(bssid->config.ds_config), &iwe.u.freq);
1681 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_FREQ_LEN);
1683 devdbg(usbdev, "QUAL %d", le32_to_cpu(bssid->rssi));
1685 iwe.u.qual.qual = level_to_qual(le32_to_cpu(bssid->rssi));
1686 iwe.u.qual.level = le32_to_cpu(bssid->rssi);
1687 iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED
1688 | IW_QUAL_LEVEL_UPDATED
1689 | IW_QUAL_NOISE_INVALID;
1690 cev = iwe_stream_add_event(cev, end_buf, &iwe, IW_EV_QUAL_LEN);
1692 devdbg(usbdev, "ENCODE %d", le32_to_cpu(bssid->privacy));
1693 iwe.cmd = SIOCGIWENCODE;
1694 iwe.u.data.length = 0;
1695 if (le32_to_cpu(bssid->privacy) == ndis_80211_priv_accept_all)
1696 iwe.u.data.flags = IW_ENCODE_DISABLED;
1698 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1700 cev = iwe_stream_add_point(cev, end_buf, &iwe, NULL);
1702 devdbg(usbdev, "RATES:");
1703 current_val = cev + IW_EV_LCP_LEN;
1704 iwe.cmd = SIOCGIWRATE;
1705 for (i = 0; i < sizeof(bssid->rates); i++) {
1706 if (bssid->rates[i] & 0x7f) {
1707 iwe.u.bitrate.value =
1708 ((bssid->rates[i] & 0x7f) *
1710 devdbg(usbdev, " %d", iwe.u.bitrate.value);
1711 current_val = iwe_stream_add_value(cev,
1712 current_val, end_buf, &iwe,
1717 if ((current_val - cev) > IW_EV_LCP_LEN)
1720 beacon = le32_to_cpu(bssid->config.beacon_period);
1721 devdbg(usbdev, "BCN_INT %d", beacon);
1722 iwe.cmd = IWEVCUSTOM;
1723 snprintf(sbuf, sizeof(sbuf), "bcn_int=%d", beacon);
1724 iwe.u.data.length = strlen(sbuf);
1725 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1727 atim = le32_to_cpu(bssid->config.atim_window);
1728 devdbg(usbdev, "ATIM %d", atim);
1729 iwe.cmd = IWEVCUSTOM;
1730 snprintf(sbuf, sizeof(sbuf), "atim=%u", atim);
1731 iwe.u.data.length = strlen(sbuf);
1732 cev = iwe_stream_add_point(cev, end_buf, &iwe, sbuf);
1734 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1735 ie_len = min(bssid_len - (int)sizeof(*bssid),
1736 (int)le32_to_cpu(bssid->ie_length));
1737 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1738 while (ie_len >= sizeof(*ie) && sizeof(*ie) + ie->len <= ie_len) {
1739 if ((ie->id == MFIE_TYPE_GENERIC && ie->len >= 4 &&
1740 memcmp(ie->data, "\x00\x50\xf2\x01", 4) == 0) ||
1741 ie->id == MFIE_TYPE_RSN) {
1742 devdbg(usbdev, "IE: WPA%d",
1743 (ie->id == MFIE_TYPE_RSN) ? 2 : 1);
1744 iwe.cmd = IWEVGENIE;
1745 iwe.u.data.length = min(ie->len + 2, MAX_WPA_IE_LEN);
1746 cev = iwe_stream_add_point(cev, end_buf, &iwe,
1750 ie_len -= sizeof(*ie) + ie->len;
1751 ie = (struct ieee80211_info_element *)&ie->data[ie->len];
1758 static int rndis_iw_get_scan(struct net_device *dev,
1759 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1761 struct usbnet *usbdev = dev->priv;
1764 struct ndis_80211_bssid_list_ex *bssid_list;
1765 struct ndis_80211_bssid_ex *bssid;
1766 int ret = -EINVAL, len, count, bssid_len;
1768 devdbg(usbdev, "SIOCGIWSCAN");
1770 len = CONTROL_BUFFER_SIZE;
1771 buf = kmalloc(len, GFP_KERNEL);
1777 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1783 bssid = bssid_list->bssid;
1784 bssid_len = le32_to_cpu(bssid->length);
1785 count = le32_to_cpu(bssid_list->num_items);
1786 devdbg(usbdev, "SIOCGIWSCAN: %d BSSIDs found", count);
1788 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1789 cev = rndis_translate_scan(dev, cev, extra + IW_SCAN_MAX_DATA,
1791 bssid = (void *)bssid + bssid_len;
1792 bssid_len = le32_to_cpu(bssid->length);
1797 wrqu->data.length = cev - extra;
1798 wrqu->data.flags = 0;
1804 static int rndis_iw_set_genie(struct net_device *dev,
1805 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1807 struct usbnet *usbdev = dev->priv;
1808 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1814 for (j = 0; j < wrqu->data.length; j += 8)
1816 "SIOCSIWGENIE %04x - "
1817 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1818 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1819 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1821 /* clear existing IEs */
1822 if (priv->wpa_ie_len) {
1823 kfree(priv->wpa_ie);
1824 priv->wpa_ie_len = 0;
1828 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1830 priv->wpa_ie_len = wrqu->data.length;
1831 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1838 static int rndis_iw_get_genie(struct net_device *dev,
1839 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1841 struct usbnet *usbdev = dev->priv;
1842 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1844 devdbg(usbdev, "SIOCGIWGENIE");
1846 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1847 wrqu->data.length = 0;
1851 if (wrqu->data.length < priv->wpa_ie_len)
1854 wrqu->data.length = priv->wpa_ie_len;
1855 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1861 static int rndis_iw_set_rts(struct net_device *dev,
1862 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1864 struct usbnet *usbdev = dev->priv;
1866 devdbg(usbdev, "SIOCSIWRTS");
1868 tmp = cpu_to_le32(wrqu->rts.value);
1869 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1874 static int rndis_iw_get_rts(struct net_device *dev,
1875 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1877 struct usbnet *usbdev = dev->priv;
1882 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1884 wrqu->rts.value = le32_to_cpu(tmp);
1885 wrqu->rts.flags = 1;
1886 wrqu->rts.disabled = 0;
1889 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1895 static int rndis_iw_set_frag(struct net_device *dev,
1896 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1898 struct usbnet *usbdev = dev->priv;
1901 devdbg(usbdev, "SIOCSIWFRAG");
1903 tmp = cpu_to_le32(wrqu->frag.value);
1904 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1909 static int rndis_iw_get_frag(struct net_device *dev,
1910 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1912 struct usbnet *usbdev = dev->priv;
1917 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1920 wrqu->frag.value = le32_to_cpu(tmp);
1921 wrqu->frag.flags = 1;
1922 wrqu->frag.disabled = 0;
1924 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1929 static int rndis_iw_set_nick(struct net_device *dev,
1930 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1932 struct usbnet *usbdev = dev->priv;
1933 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1935 devdbg(usbdev, "SIOCSIWNICK");
1937 priv->nick_len = wrqu->data.length;
1938 if (priv->nick_len > 32)
1939 priv->nick_len = 32;
1941 memcpy(priv->nick, extra, priv->nick_len);
1946 static int rndis_iw_get_nick(struct net_device *dev,
1947 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1949 struct usbnet *usbdev = dev->priv;
1950 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1952 wrqu->data.flags = 1;
1953 wrqu->data.length = priv->nick_len;
1954 memcpy(extra, priv->nick, priv->nick_len);
1956 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1962 static int rndis_iw_set_freq(struct net_device *dev,
1963 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1965 struct usbnet *usbdev = dev->priv;
1966 struct ndis_80211_conf config;
1967 unsigned int dsconfig;
1970 /* this OID is valid only when not associated */
1971 if (is_associated(usbdev))
1975 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1978 len = sizeof(config);
1979 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1981 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1985 config.ds_config = cpu_to_le32(dsconfig);
1987 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1988 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1993 static int rndis_iw_get_freq(struct net_device *dev,
1994 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1996 struct usbnet *usbdev = dev->priv;
1997 struct ndis_80211_conf config;
2000 len = sizeof(config);
2001 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
2003 dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
2005 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
2010 static int rndis_iw_get_txpower(struct net_device *dev,
2011 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2013 struct usbnet *usbdev = dev->priv;
2014 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2018 if (priv->radio_on) {
2019 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2020 len = sizeof(tx_power);
2021 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2026 /* fake incase not supported */
2027 tx_power = cpu_to_le32(get_bcm4320_power(priv));
2029 wrqu->txpower.flags = IW_TXPOW_MWATT;
2030 wrqu->txpower.value = le32_to_cpu(tx_power);
2031 wrqu->txpower.disabled = 0;
2033 wrqu->txpower.flags = IW_TXPOW_MWATT;
2034 wrqu->txpower.value = 0;
2035 wrqu->txpower.disabled = 1;
2038 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
2044 static int rndis_iw_set_txpower(struct net_device *dev,
2045 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2047 struct usbnet *usbdev = dev->priv;
2048 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2049 __le32 tx_power = 0;
2052 if (!wrqu->txpower.disabled) {
2053 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
2054 tx_power = cpu_to_le32(wrqu->txpower.value);
2055 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
2056 if (wrqu->txpower.value > 20)
2057 tx_power = cpu_to_le32(128);
2058 else if (wrqu->txpower.value < -43)
2059 tx_power = cpu_to_le32(127);
2062 tmp = wrqu->txpower.value;
2065 tx_power = cpu_to_le32((unsigned char)tmp);
2070 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
2072 if (le32_to_cpu(tx_power) != 0) {
2073 if (priv->caps & CAP_SUPPORT_TXPOWER) {
2074 /* turn radio on first */
2075 if (!priv->radio_on)
2076 disassociate(usbdev, 1);
2078 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
2079 &tx_power, sizeof(tx_power));
2084 /* txpower unsupported, just turn radio on */
2085 if (!priv->radio_on)
2086 return disassociate(usbdev, 1);
2087 return 0; /* all ready on */
2091 /* tx_power == 0, turn off radio */
2092 return disassociate(usbdev, 0);
2096 static int rndis_iw_get_rate(struct net_device *dev,
2097 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2099 struct usbnet *usbdev = dev->priv;
2104 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2106 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2107 wrqu->bitrate.disabled = 0;
2108 wrqu->bitrate.flags = 1;
2114 static int rndis_iw_set_mlme(struct net_device *dev,
2115 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2117 struct usbnet *usbdev = dev->priv;
2118 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2119 struct iw_mlme *mlme = (struct iw_mlme *)extra;
2120 unsigned char bssid[ETH_ALEN];
2122 get_bssid(usbdev, bssid);
2124 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2127 switch (mlme->cmd) {
2128 case IW_MLME_DEAUTH:
2129 return deauthenticate(usbdev);
2130 case IW_MLME_DISASSOC:
2131 return disassociate(usbdev, priv->radio_on);
2140 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2142 struct usbnet *usbdev = dev->priv;
2143 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2144 unsigned long flags;
2146 spin_lock_irqsave(&priv->stats_lock, flags);
2147 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2148 spin_unlock_irqrestore(&priv->stats_lock, flags);
2150 return &priv->iwstats;
2154 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2155 static const iw_handler rndis_iw_handler[] =
2157 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2158 IW_IOCTL(SIOCGIWNAME) = rndis_iw_get_name,
2159 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2160 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2161 IW_IOCTL(SIOCSIWMODE) = rndis_iw_set_mode,
2162 IW_IOCTL(SIOCGIWMODE) = rndis_iw_get_mode,
2163 IW_IOCTL(SIOCGIWRANGE) = rndis_iw_get_range,
2164 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2165 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2166 IW_IOCTL(SIOCSIWSCAN) = rndis_iw_set_scan,
2167 IW_IOCTL(SIOCGIWSCAN) = rndis_iw_get_scan,
2168 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2169 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2170 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2171 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2172 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2173 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2174 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2175 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2176 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2177 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2178 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2179 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2180 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2181 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2182 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2183 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2184 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2185 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2188 static const iw_handler rndis_wext_private_handler[] = {
2191 static const struct iw_priv_args rndis_wext_private_args[] = {
2195 static const struct iw_handler_def rndis_iw_handlers = {
2196 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2197 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2198 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2199 .standard = (iw_handler *)rndis_iw_handler,
2200 .private = (iw_handler *)rndis_wext_private_handler,
2201 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2202 .get_wireless_stats = rndis_get_wireless_stats,
2206 static void rndis_wext_worker(struct work_struct *work)
2208 struct rndis_wext_private *priv =
2209 container_of(work, struct rndis_wext_private, work);
2210 struct usbnet *usbdev = priv->usbdev;
2211 union iwreq_data evt;
2212 unsigned char bssid[ETH_ALEN];
2213 struct ndis_80211_assoc_info *info;
2214 int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
2217 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
2218 netif_carrier_on(usbdev->net);
2220 info = kzalloc(assoc_size, GFP_KERNEL);
2224 /* Get association info IEs from device and send them back to
2226 ret = get_association_info(usbdev, info, assoc_size);
2228 evt.data.length = le32_to_cpu(info->req_ie_length);
2229 if (evt.data.length > 0) {
2230 offset = le32_to_cpu(info->offset_req_ies);
2231 wireless_send_event(usbdev->net,
2232 IWEVASSOCREQIE, &evt,
2233 (char *)info + offset);
2236 evt.data.length = le32_to_cpu(info->resp_ie_length);
2237 if (evt.data.length > 0) {
2238 offset = le32_to_cpu(info->offset_resp_ies);
2239 wireless_send_event(usbdev->net,
2240 IWEVASSOCRESPIE, &evt,
2241 (char *)info + offset);
2248 ret = get_bssid(usbdev, bssid);
2251 evt.data.length = 0;
2252 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2253 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2257 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
2258 netif_carrier_off(usbdev->net);
2261 evt.data.length = 0;
2262 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2263 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2266 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2267 set_multicast_list(usbdev);
2270 static void rndis_wext_set_multicast_list(struct net_device *dev)
2272 struct usbnet *usbdev = dev->priv;
2273 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2275 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2278 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2279 queue_work(priv->workqueue, &priv->work);
2282 static void rndis_wext_link_change(struct usbnet *dev, int state)
2284 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2286 /* queue work to avoid recursive calls into rndis_command */
2287 set_bit(state ? WORK_LINK_UP : WORK_LINK_DOWN, &priv->work_pending);
2288 queue_work(priv->workqueue, &priv->work);
2292 static int rndis_wext_get_caps(struct usbnet *dev)
2297 } networks_supported;
2298 int len, retval, i, n;
2300 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2302 /* determine if supports setting txpower */
2303 len = sizeof(tx_power);
2304 retval = rndis_query_oid(dev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2306 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2307 priv->caps |= CAP_SUPPORT_TXPOWER;
2309 /* determine supported modes */
2310 len = sizeof(networks_supported);
2311 retval = rndis_query_oid(dev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2312 &networks_supported, &len);
2314 n = le32_to_cpu(networks_supported.num_items);
2317 for (i = 0; i < n; i++) {
2318 switch (le32_to_cpu(networks_supported.items[i])) {
2319 case ndis_80211_type_freq_hop:
2320 case ndis_80211_type_direct_seq:
2321 priv->caps |= CAP_MODE_80211B;
2323 case ndis_80211_type_ofdm_a:
2324 priv->caps |= CAP_MODE_80211A;
2326 case ndis_80211_type_ofdm_g:
2327 priv->caps |= CAP_MODE_80211G;
2331 if (priv->caps & CAP_MODE_80211A)
2332 strcat(priv->name, "a");
2333 if (priv->caps & CAP_MODE_80211B)
2334 strcat(priv->name, "b");
2335 if (priv->caps & CAP_MODE_80211G)
2336 strcat(priv->name, "g");
2343 #define STATS_UPDATE_JIFFIES (HZ)
2344 static void rndis_update_wireless_stats(struct work_struct *work)
2346 struct rndis_wext_private *priv =
2347 container_of(work, struct rndis_wext_private, stats_work.work);
2348 struct usbnet *usbdev = priv->usbdev;
2349 struct iw_statistics iwstats;
2352 unsigned long flags;
2353 int update_jiffies = STATS_UPDATE_JIFFIES;
2356 spin_lock_irqsave(&priv->stats_lock, flags);
2357 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2358 spin_unlock_irqrestore(&priv->stats_lock, flags);
2360 /* only update stats when connected */
2361 if (!is_associated(usbdev)) {
2362 iwstats.qual.qual = 0;
2363 iwstats.qual.level = 0;
2364 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2365 | IW_QUAL_LEVEL_UPDATED
2366 | IW_QUAL_NOISE_INVALID
2367 | IW_QUAL_QUAL_INVALID
2368 | IW_QUAL_LEVEL_INVALID;
2373 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2375 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2378 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2379 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2380 iwstats.qual.level = le32_to_cpu(rssi);
2381 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2382 | IW_QUAL_LEVEL_UPDATED
2383 | IW_QUAL_NOISE_INVALID;
2386 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2389 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2391 iwstats.discard.misc += le32_to_cpu(tmp);
2394 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2396 iwstats.discard.misc += le32_to_cpu(tmp);
2399 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2401 iwstats.discard.misc += le32_to_cpu(tmp);
2403 /* Workaround transfer stalls on poor quality links.
2404 * TODO: find right way to fix these stalls (as stalls do not happen
2405 * with ndiswrapper/windows driver). */
2406 if (iwstats.qual.qual <= 25) {
2407 /* Decrease stats worker interval to catch stalls.
2408 * faster. Faster than 400-500ms causes packet loss,
2409 * Slower doesn't catch stalls fast enough.
2411 j = msecs_to_jiffies(priv->param_workaround_interval);
2412 if (j > STATS_UPDATE_JIFFIES)
2413 j = STATS_UPDATE_JIFFIES;
2418 /* Send scan OID. Use of both OIDs is required to get device
2422 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2425 len = CONTROL_BUFFER_SIZE;
2426 buf = kmalloc(len, GFP_KERNEL);
2430 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2434 spin_lock_irqsave(&priv->stats_lock, flags);
2435 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2436 spin_unlock_irqrestore(&priv->stats_lock, flags);
2438 if (update_jiffies >= HZ)
2439 update_jiffies = round_jiffies_relative(update_jiffies);
2441 j = round_jiffies_relative(update_jiffies);
2442 if (abs(j - update_jiffies) <= 10)
2446 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2450 static int bcm4320_early_init(struct usbnet *dev)
2452 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2455 /* Early initialization settings, setting these won't have effect
2456 * if called after generic_rndis_bind().
2459 priv->param_country[0] = modparam_country[0];
2460 priv->param_country[1] = modparam_country[1];
2461 priv->param_country[2] = 0;
2462 priv->param_frameburst = modparam_frameburst;
2463 priv->param_afterburner = modparam_afterburner;
2464 priv->param_power_save = modparam_power_save;
2465 priv->param_power_output = modparam_power_output;
2466 priv->param_roamtrigger = modparam_roamtrigger;
2467 priv->param_roamdelta = modparam_roamdelta;
2469 priv->param_country[0] = toupper(priv->param_country[0]);
2470 priv->param_country[1] = toupper(priv->param_country[1]);
2471 /* doesn't support EU as country code, use FI instead */
2472 if (!strcmp(priv->param_country, "EU"))
2473 strcpy(priv->param_country, "FI");
2475 if (priv->param_power_save < 0)
2476 priv->param_power_save = 0;
2477 else if (priv->param_power_save > 2)
2478 priv->param_power_save = 2;
2480 if (priv->param_power_output < 0)
2481 priv->param_power_output = 0;
2482 else if (priv->param_power_output > 3)
2483 priv->param_power_output = 3;
2485 if (priv->param_roamtrigger < -80)
2486 priv->param_roamtrigger = -80;
2487 else if (priv->param_roamtrigger > -60)
2488 priv->param_roamtrigger = -60;
2490 if (priv->param_roamdelta < 0)
2491 priv->param_roamdelta = 0;
2492 else if (priv->param_roamdelta > 2)
2493 priv->param_roamdelta = 2;
2495 if (modparam_workaround_interval < 0)
2496 priv->param_workaround_interval = 500;
2498 priv->param_workaround_interval = modparam_workaround_interval;
2500 rndis_set_config_parameter_str(dev, "Country", priv->param_country);
2501 rndis_set_config_parameter_str(dev, "FrameBursting",
2502 priv->param_frameburst ? "1" : "0");
2503 rndis_set_config_parameter_str(dev, "Afterburner",
2504 priv->param_afterburner ? "1" : "0");
2505 sprintf(buf, "%d", priv->param_power_save);
2506 rndis_set_config_parameter_str(dev, "PowerSaveMode", buf);
2507 sprintf(buf, "%d", priv->param_power_output);
2508 rndis_set_config_parameter_str(dev, "PwrOut", buf);
2509 sprintf(buf, "%d", priv->param_roamtrigger);
2510 rndis_set_config_parameter_str(dev, "RoamTrigger", buf);
2511 sprintf(buf, "%d", priv->param_roamdelta);
2512 rndis_set_config_parameter_str(dev, "RoamDelta", buf);
2518 static int rndis_wext_bind(struct usbnet *dev, struct usb_interface *intf)
2520 struct net_device *net = dev->net;
2521 struct rndis_wext_private *priv;
2525 /* allocate rndis private data */
2526 priv = kmalloc(sizeof(struct rndis_wext_private), GFP_KERNEL);
2530 /* These have to be initialized before calling generic_rndis_bind().
2531 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2533 dev->driver_priv = priv;
2534 memset(priv, 0, sizeof(*priv));
2535 memset(priv->name, 0, sizeof(priv->name));
2536 strcpy(priv->name, "IEEE802.11");
2537 net->wireless_handlers = &rndis_iw_handlers;
2540 mutex_init(&priv->command_lock);
2541 spin_lock_init(&priv->stats_lock);
2543 /* try bind rndis_host */
2544 retval = generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2548 /* generic_rndis_bind set packet filter to multicast_all+
2549 * promisc mode which doesn't work well for our devices (device
2550 * picks up rssi to closest station instead of to access point).
2552 * rndis_host wants to avoid all OID as much as possible
2553 * so do promisc/multicast handling in rndis_wext.
2555 dev->net->set_multicast_list = rndis_wext_set_multicast_list;
2556 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2557 retval = rndis_set_oid(dev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2561 retval = rndis_query_oid(dev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp, &len);
2562 priv->multicast_size = le32_to_cpu(tmp);
2563 if (retval < 0 || priv->multicast_size < 0)
2564 priv->multicast_size = 0;
2565 if (priv->multicast_size > 0)
2566 dev->net->flags |= IFF_MULTICAST;
2568 dev->net->flags &= ~IFF_MULTICAST;
2570 priv->iwstats.qual.qual = 0;
2571 priv->iwstats.qual.level = 0;
2572 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2573 | IW_QUAL_LEVEL_UPDATED
2574 | IW_QUAL_NOISE_INVALID
2575 | IW_QUAL_QUAL_INVALID
2576 | IW_QUAL_LEVEL_INVALID;
2578 rndis_wext_get_caps(dev);
2579 set_default_iw_params(dev);
2583 disassociate(dev, 1);
2584 netif_carrier_off(dev->net);
2586 /* because rndis_command() sleeps we need to use workqueue */
2587 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2588 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2589 queue_delayed_work(priv->workqueue, &priv->stats_work,
2590 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2591 INIT_WORK(&priv->work, rndis_wext_worker);
2601 static void rndis_wext_unbind(struct usbnet *dev, struct usb_interface *intf)
2603 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
2605 /* turn radio off */
2606 disassociate(dev, 0);
2608 cancel_delayed_work_sync(&priv->stats_work);
2609 cancel_work_sync(&priv->work);
2610 flush_workqueue(priv->workqueue);
2611 destroy_workqueue(priv->workqueue);
2613 if (priv && priv->wpa_ie_len)
2614 kfree(priv->wpa_ie);
2617 rndis_unbind(dev, intf);
2621 static int rndis_wext_reset(struct usbnet *dev)
2623 return deauthenticate(dev);
2627 static const struct driver_info bcm4320b_info = {
2628 .description = "Wireless RNDIS device, BCM4320b based",
2629 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2630 .bind = rndis_wext_bind,
2631 .unbind = rndis_wext_unbind,
2632 .status = rndis_status,
2633 .rx_fixup = rndis_rx_fixup,
2634 .tx_fixup = rndis_tx_fixup,
2635 .reset = rndis_wext_reset,
2636 .early_init = bcm4320_early_init,
2637 .link_change = rndis_wext_link_change,
2640 static const struct driver_info bcm4320a_info = {
2641 .description = "Wireless RNDIS device, BCM4320a based",
2642 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2643 .bind = rndis_wext_bind,
2644 .unbind = rndis_wext_unbind,
2645 .status = rndis_status,
2646 .rx_fixup = rndis_rx_fixup,
2647 .tx_fixup = rndis_tx_fixup,
2648 .reset = rndis_wext_reset,
2649 .early_init = bcm4320_early_init,
2650 .link_change = rndis_wext_link_change,
2653 static const struct driver_info rndis_wext_info = {
2654 .description = "Wireless RNDIS device",
2655 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2656 .bind = rndis_wext_bind,
2657 .unbind = rndis_wext_unbind,
2658 .status = rndis_status,
2659 .rx_fixup = rndis_rx_fixup,
2660 .tx_fixup = rndis_tx_fixup,
2661 .reset = rndis_wext_reset,
2662 .early_init = bcm4320_early_init,
2663 .link_change = rndis_wext_link_change,
2666 /*-------------------------------------------------------------------------*/
2668 static const struct usb_device_id products [] = {
2669 #define RNDIS_MASTER_INTERFACE \
2670 .bInterfaceClass = USB_CLASS_COMM, \
2671 .bInterfaceSubClass = 2 /* ACM */, \
2672 .bInterfaceProtocol = 0x0ff
2674 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2675 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2678 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2679 | USB_DEVICE_ID_MATCH_DEVICE,
2681 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2682 RNDIS_MASTER_INTERFACE,
2683 .driver_info = (unsigned long) &bcm4320b_info,
2685 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2686 | USB_DEVICE_ID_MATCH_DEVICE,
2688 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2689 RNDIS_MASTER_INTERFACE,
2690 .driver_info = (unsigned long) &bcm4320b_info,
2692 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2693 | USB_DEVICE_ID_MATCH_DEVICE,
2695 .idProduct = 0x011b, /* Belkin F5D7051 */
2696 RNDIS_MASTER_INTERFACE,
2697 .driver_info = (unsigned long) &bcm4320b_info,
2699 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2700 | USB_DEVICE_ID_MATCH_DEVICE,
2701 .idVendor = 0x1799, /* Belkin has two vendor ids */
2702 .idProduct = 0x011b, /* Belkin F5D7051 */
2703 RNDIS_MASTER_INTERFACE,
2704 .driver_info = (unsigned long) &bcm4320b_info,
2706 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2707 | USB_DEVICE_ID_MATCH_DEVICE,
2709 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2710 RNDIS_MASTER_INTERFACE,
2711 .driver_info = (unsigned long) &bcm4320b_info,
2713 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2714 | USB_DEVICE_ID_MATCH_DEVICE,
2716 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2717 RNDIS_MASTER_INTERFACE,
2718 .driver_info = (unsigned long) &bcm4320b_info,
2720 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2721 | USB_DEVICE_ID_MATCH_DEVICE,
2723 .idProduct = 0x1717, /* Asus WL169gE */
2724 RNDIS_MASTER_INTERFACE,
2725 .driver_info = (unsigned long) &bcm4320b_info,
2727 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2728 | USB_DEVICE_ID_MATCH_DEVICE,
2730 .idProduct = 0xd11b, /* Eminent EM4045 */
2731 RNDIS_MASTER_INTERFACE,
2732 .driver_info = (unsigned long) &bcm4320b_info,
2734 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2735 | USB_DEVICE_ID_MATCH_DEVICE,
2737 .idProduct = 0x0715, /* BT Voyager 1055 */
2738 RNDIS_MASTER_INTERFACE,
2739 .driver_info = (unsigned long) &bcm4320b_info,
2741 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2742 * parameters available, hardware probably contain older firmware version with
2743 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2746 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2747 | USB_DEVICE_ID_MATCH_DEVICE,
2749 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2750 RNDIS_MASTER_INTERFACE,
2751 .driver_info = (unsigned long) &bcm4320a_info,
2753 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2754 | USB_DEVICE_ID_MATCH_DEVICE,
2756 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2757 RNDIS_MASTER_INTERFACE,
2758 .driver_info = (unsigned long) &bcm4320a_info,
2760 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2761 | USB_DEVICE_ID_MATCH_DEVICE,
2763 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2764 RNDIS_MASTER_INTERFACE,
2765 .driver_info = (unsigned long) &bcm4320a_info,
2767 /* Generic Wireless RNDIS devices that we don't have exact
2768 * idVendor/idProduct/chip yet.
2771 /* RNDIS is MSFT's un-official variant of CDC ACM */
2772 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2773 .driver_info = (unsigned long) &rndis_wext_info,
2775 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2776 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2777 .driver_info = (unsigned long) &rndis_wext_info,
2781 MODULE_DEVICE_TABLE(usb, products);
2783 static struct usb_driver rndis_wlan_driver = {
2784 .name = "rndis_wlan",
2785 .id_table = products,
2786 .probe = usbnet_probe,
2787 .disconnect = usbnet_disconnect,
2788 .suspend = usbnet_suspend,
2789 .resume = usbnet_resume,
2792 static int __init rndis_wlan_init(void)
2794 return usb_register(&rndis_wlan_driver);
2796 module_init(rndis_wlan_init);
2798 static void __exit rndis_wlan_exit(void)
2800 usb_deregister(&rndis_wlan_driver);
2802 module_exit(rndis_wlan_exit);
2804 MODULE_AUTHOR("Bjorge Dijkstra");
2805 MODULE_AUTHOR("Jussi Kivilinna");
2806 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2807 MODULE_LICENSE("GPL");