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 <net/wireless.h>
46 #include <net/cfg80211.h>
47 #include <linux/usb/usbnet.h>
48 #include <linux/usb/rndis_host.h>
51 /* NOTE: All these are settings for Broadcom chipset */
52 static char modparam_country[4] = "EU";
53 module_param_string(country, modparam_country, 4, 0444);
54 MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
56 static int modparam_frameburst = 1;
57 module_param_named(frameburst, modparam_frameburst, int, 0444);
58 MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
60 static int modparam_afterburner = 0;
61 module_param_named(afterburner, modparam_afterburner, int, 0444);
62 MODULE_PARM_DESC(afterburner,
63 "enable afterburner aka '125 High Speed Mode' (default: off)");
65 static int modparam_power_save = 0;
66 module_param_named(power_save, modparam_power_save, int, 0444);
67 MODULE_PARM_DESC(power_save,
68 "set power save mode: 0=off, 1=on, 2=fast (default: off)");
70 static int modparam_power_output = 3;
71 module_param_named(power_output, modparam_power_output, int, 0444);
72 MODULE_PARM_DESC(power_output,
73 "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
75 static int modparam_roamtrigger = -70;
76 module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
77 MODULE_PARM_DESC(roamtrigger,
78 "set roaming dBm trigger: -80=optimize for distance, "
79 "-60=bandwidth (default: -70)");
81 static int modparam_roamdelta = 1;
82 module_param_named(roamdelta, modparam_roamdelta, int, 0444);
83 MODULE_PARM_DESC(roamdelta,
84 "set roaming tendency: 0=aggressive, 1=moderate, "
85 "2=conservative (default: moderate)");
87 static int modparam_workaround_interval = 500;
88 module_param_named(workaround_interval, modparam_workaround_interval,
90 MODULE_PARM_DESC(workaround_interval,
91 "set stall workaround interval in msecs (default: 500)");
94 /* various RNDIS OID defs */
95 #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
96 #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
98 #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
99 #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
100 #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
101 #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
102 #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
104 #define OID_802_3_PERMANENT_ADDRESS cpu_to_le32(0x01010101)
105 #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
106 #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
107 #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
109 #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
110 #define OID_802_11_SSID cpu_to_le32(0x0d010102)
111 #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
112 #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
113 #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
114 #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
115 #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
116 #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
117 #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
118 #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
119 #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
120 #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
121 #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
122 #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
123 #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
124 #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
125 #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
126 #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
127 #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
128 #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
129 #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
130 #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
131 #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
132 #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
135 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
136 #define WL_NOISE -96 /* typical noise level in dBm */
137 #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
140 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
141 * based on wireless rndis) has default txpower of 13dBm.
142 * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
145 #define BCM4320_DEFAULT_TXPOWER 20
148 /* codes for "status" field of completion messages */
149 #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
150 #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
153 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
154 * slightly modified for datatype endianess, etc
156 #define NDIS_802_11_LENGTH_SSID 32
157 #define NDIS_802_11_LENGTH_RATES 8
158 #define NDIS_802_11_LENGTH_RATES_EX 16
160 enum ndis_80211_net_type {
161 ndis_80211_type_freq_hop,
162 ndis_80211_type_direct_seq,
163 ndis_80211_type_ofdm_a,
164 ndis_80211_type_ofdm_g
167 enum ndis_80211_net_infra {
168 ndis_80211_infra_adhoc,
169 ndis_80211_infra_infra,
170 ndis_80211_infra_auto_unknown
173 enum ndis_80211_auth_mode {
174 ndis_80211_auth_open,
175 ndis_80211_auth_shared,
176 ndis_80211_auth_auto_switch,
178 ndis_80211_auth_wpa_psk,
179 ndis_80211_auth_wpa_none,
180 ndis_80211_auth_wpa2,
181 ndis_80211_auth_wpa2_psk
184 enum ndis_80211_encr_status {
185 ndis_80211_encr_wep_enabled,
186 ndis_80211_encr_disabled,
187 ndis_80211_encr_wep_key_absent,
188 ndis_80211_encr_not_supported,
189 ndis_80211_encr_tkip_enabled,
190 ndis_80211_encr_tkip_key_absent,
191 ndis_80211_encr_ccmp_enabled,
192 ndis_80211_encr_ccmp_key_absent
195 enum ndis_80211_priv_filter {
196 ndis_80211_priv_accept_all,
197 ndis_80211_priv_8021x_wep
200 struct ndis_80211_ssid {
202 u8 essid[NDIS_802_11_LENGTH_SSID];
203 } __attribute__((packed));
205 struct ndis_80211_conf_freq_hop {
210 } __attribute__((packed));
212 struct ndis_80211_conf {
214 __le32 beacon_period;
217 struct ndis_80211_conf_freq_hop fh_config;
218 } __attribute__((packed));
220 struct ndis_80211_bssid_ex {
224 struct ndis_80211_ssid ssid;
228 struct ndis_80211_conf config;
230 u8 rates[NDIS_802_11_LENGTH_RATES_EX];
233 } __attribute__((packed));
235 struct ndis_80211_bssid_list_ex {
237 struct ndis_80211_bssid_ex bssid[0];
238 } __attribute__((packed));
240 struct ndis_80211_fixed_ies {
242 __le16 beacon_interval;
244 } __attribute__((packed));
246 struct ndis_80211_wep_key {
251 } __attribute__((packed));
253 struct ndis_80211_key {
261 } __attribute__((packed));
263 struct ndis_80211_remove_key {
267 } __attribute__((packed));
269 struct ndis_config_param {
275 } __attribute__((packed));
277 struct ndis_80211_assoc_info {
282 __le16 listen_interval;
283 u8 cur_ap_address[6];
285 __le32 req_ie_length;
286 __le32 offset_req_ies;
293 __le32 resp_ie_length;
294 __le32 offset_resp_ies;
295 } __attribute__((packed));
297 /* these have to match what is in wpa_supplicant */
298 enum wpa_alg { WPA_ALG_NONE, WPA_ALG_WEP, WPA_ALG_TKIP, WPA_ALG_CCMP };
299 enum wpa_cipher { CIPHER_NONE, CIPHER_WEP40, CIPHER_TKIP, CIPHER_CCMP,
301 enum wpa_key_mgmt { KEY_MGMT_802_1X, KEY_MGMT_PSK, KEY_MGMT_NONE,
302 KEY_MGMT_802_1X_NO_WPA, KEY_MGMT_WPA_NONE };
307 #define NET_TYPE_11FB 0
309 #define CAP_MODE_80211A 1
310 #define CAP_MODE_80211B 2
311 #define CAP_MODE_80211G 4
312 #define CAP_MODE_MASK 7
313 #define CAP_SUPPORT_TXPOWER 8
315 #define WORK_LINK_UP (1<<0)
316 #define WORK_LINK_DOWN (1<<1)
317 #define WORK_SET_MULTICAST_LIST (1<<2)
319 #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
321 static const struct ieee80211_channel rndis_channels[] = {
322 { .center_freq = 2412 },
323 { .center_freq = 2417 },
324 { .center_freq = 2422 },
325 { .center_freq = 2427 },
326 { .center_freq = 2432 },
327 { .center_freq = 2437 },
328 { .center_freq = 2442 },
329 { .center_freq = 2447 },
330 { .center_freq = 2452 },
331 { .center_freq = 2457 },
332 { .center_freq = 2462 },
333 { .center_freq = 2467 },
334 { .center_freq = 2472 },
335 { .center_freq = 2484 },
338 static const struct ieee80211_rate rndis_rates[] = {
340 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
341 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
342 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
353 /* RNDIS device private data */
354 struct rndis_wext_private {
355 struct usbnet *usbdev;
357 struct wireless_dev wdev;
359 struct cfg80211_scan_request *scan_request;
361 struct workqueue_struct *workqueue;
362 struct delayed_work stats_work;
363 struct delayed_work scan_work;
364 struct work_struct work;
365 struct mutex command_lock;
366 spinlock_t stats_lock;
367 unsigned long work_pending;
369 struct ieee80211_supported_band band;
370 struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
371 struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
373 struct iw_statistics iwstats;
374 struct iw_statistics privstats;
382 /* module parameters */
383 char param_country[4];
384 int param_frameburst;
385 int param_afterburner;
386 int param_power_save;
387 int param_power_output;
388 int param_roamtrigger;
390 u32 param_workaround_interval;
395 struct ndis_80211_ssid essid;
397 /* encryption stuff */
398 int encr_tx_key_index;
399 char encr_keys[4][32];
407 int wpa_cipher_group;
409 u8 command_buffer[COMMAND_BUFFER_SIZE];
415 static int rndis_change_virtual_intf(struct wiphy *wiphy, int ifindex,
416 enum nl80211_iftype type, u32 *flags,
417 struct vif_params *params);
419 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
420 struct cfg80211_scan_request *request);
422 struct cfg80211_ops rndis_config_ops = {
423 .change_virtual_intf = rndis_change_virtual_intf,
427 void *rndis_wiphy_privid = &rndis_wiphy_privid;
429 static const int bcm4320_power_output[4] = { 25, 50, 75, 100 };
431 static const unsigned char zero_bssid[ETH_ALEN] = {0,};
432 static const unsigned char ffff_bssid[ETH_ALEN] = { 0xff, 0xff, 0xff,
436 static struct rndis_wext_private *get_rndis_wext_priv(struct usbnet *dev)
438 return (struct rndis_wext_private *)dev->driver_priv;
442 static u32 get_bcm4320_power(struct rndis_wext_private *priv)
444 return BCM4320_DEFAULT_TXPOWER *
445 bcm4320_power_output[priv->param_power_output] / 100;
449 /* translate error code */
450 static int rndis_error_status(__le32 rndis_status)
453 switch (rndis_status) {
454 case RNDIS_STATUS_SUCCESS:
457 case RNDIS_STATUS_FAILURE:
458 case RNDIS_STATUS_INVALID_DATA:
461 case RNDIS_STATUS_NOT_SUPPORTED:
464 case RNDIS_STATUS_ADAPTER_NOT_READY:
465 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
473 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
475 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
478 struct rndis_msg_hdr *header;
479 struct rndis_query *get;
480 struct rndis_query_c *get_c;
484 buflen = *len + sizeof(*u.get);
485 if (buflen < CONTROL_BUFFER_SIZE)
486 buflen = CONTROL_BUFFER_SIZE;
488 if (buflen > COMMAND_BUFFER_SIZE) {
489 u.buf = kmalloc(buflen, GFP_KERNEL);
493 u.buf = priv->command_buffer;
496 mutex_lock(&priv->command_lock);
498 memset(u.get, 0, sizeof *u.get);
499 u.get->msg_type = RNDIS_MSG_QUERY;
500 u.get->msg_len = cpu_to_le32(sizeof *u.get);
503 ret = rndis_command(dev, u.header, buflen);
505 ret = le32_to_cpu(u.get_c->len);
506 *len = (*len > ret) ? ret : *len;
507 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
508 ret = rndis_error_status(u.get_c->status);
511 mutex_unlock(&priv->command_lock);
513 if (u.buf != priv->command_buffer)
519 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
521 struct rndis_wext_private *priv = get_rndis_wext_priv(dev);
524 struct rndis_msg_hdr *header;
525 struct rndis_set *set;
526 struct rndis_set_c *set_c;
530 buflen = len + sizeof(*u.set);
531 if (buflen < CONTROL_BUFFER_SIZE)
532 buflen = CONTROL_BUFFER_SIZE;
534 if (buflen > COMMAND_BUFFER_SIZE) {
535 u.buf = kmalloc(buflen, GFP_KERNEL);
539 u.buf = priv->command_buffer;
542 mutex_lock(&priv->command_lock);
544 memset(u.set, 0, sizeof *u.set);
545 u.set->msg_type = RNDIS_MSG_SET;
546 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
548 u.set->len = cpu_to_le32(len);
549 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
550 u.set->handle = cpu_to_le32(0);
551 memcpy(u.buf + sizeof(*u.set), data, len);
553 ret = rndis_command(dev, u.header, buflen);
555 ret = rndis_error_status(u.set_c->status);
557 mutex_unlock(&priv->command_lock);
559 if (u.buf != priv->command_buffer)
566 * Specs say that we can only set config parameters only soon after device
568 * value_type: 0 = u32, 2 = unicode string
570 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
571 int value_type, void *value)
573 struct ndis_config_param *infobuf;
574 int value_len, info_len, param_len, ret, i;
579 value_len = sizeof(__le32);
580 else if (value_type == 2)
581 value_len = strlen(value) * sizeof(__le16);
585 param_len = strlen(param) * sizeof(__le16);
586 info_len = sizeof(*infobuf) + param_len + value_len;
591 infobuf = kmalloc(info_len, GFP_KERNEL);
597 /* extra 12 bytes are for padding (debug output) */
598 memset(infobuf, 0xCC, info_len + 12);
602 devdbg(dev, "setting config parameter: %s, value: %s",
605 devdbg(dev, "setting config parameter: %s, value: %d",
606 param, *(u32 *)value);
608 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
609 infobuf->name_length = cpu_to_le32(param_len);
610 infobuf->type = cpu_to_le32(value_type);
611 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
612 infobuf->value_length = cpu_to_le32(value_len);
614 /* simple string to unicode string conversion */
615 unibuf = (void *)infobuf + sizeof(*infobuf);
616 for (i = 0; i < param_len / sizeof(__le16); i++)
617 unibuf[i] = cpu_to_le16(param[i]);
619 if (value_type == 2) {
620 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
621 for (i = 0; i < value_len / sizeof(__le16); i++)
622 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
624 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
625 *dst_value = cpu_to_le32(*(u32 *)value);
629 devdbg(dev, "info buffer (len: %d):", info_len);
630 for (i = 0; i < info_len; i += 12) {
631 u32 *tmp = (u32 *)((u8 *)infobuf + i);
632 devdbg(dev, "%08X:%08X:%08X",
635 cpu_to_be32(tmp[2]));
639 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
642 devdbg(dev, "setting rndis config paramater failed, %d.", ret);
648 static int rndis_set_config_parameter_str(struct usbnet *dev,
649 char *param, char *value)
651 return(rndis_set_config_parameter(dev, param, 2, value));
654 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
655 char *param, u32 value)
657 return(rndis_set_config_parameter(dev, param, 0, &value));
662 * data conversion functions
664 static int level_to_qual(int level)
666 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
667 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
671 static void dsconfig_to_freq(unsigned int dsconfig, struct iw_freq *freq)
677 /* see comment in wireless.h above the "struct iw_freq"
678 * definition for an explanation of this if
679 * NOTE: 1000000 is due to the kHz
681 if (dsconfig > 1000000) {
682 freq->m = dsconfig / 10;
687 /* convert from kHz to Hz */
692 static int freq_to_dsconfig(struct iw_freq *freq, unsigned int *dsconfig)
694 if (freq->m < 1000 && freq->e == 0) {
695 if (freq->m >= 1 && freq->m <= 14)
696 *dsconfig = ieee80211_dsss_chan_to_freq(freq->m) * 1000;
702 for (i = freq->e; i > 0; i--)
715 add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index);
717 static int get_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
722 ret = rndis_query_oid(usbdev, OID_802_11_SSID, ssid, &len);
729 unsigned char tmp[NDIS_802_11_LENGTH_SSID + 1];
731 memcpy(tmp, ssid->essid, le32_to_cpu(ssid->length));
732 tmp[le32_to_cpu(ssid->length)] = 0;
733 devdbg(usbdev, "get_essid: '%s', ret: %d", tmp, ret);
740 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
742 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
745 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
747 memcpy(&priv->essid, ssid, sizeof(priv->essid));
749 devdbg(usbdev, "set_essid: radio_on = 1");
756 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
761 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
764 memset(bssid, 0, ETH_ALEN);
769 static int get_association_info(struct usbnet *usbdev,
770 struct ndis_80211_assoc_info *info, int len)
772 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
776 static int is_associated(struct usbnet *usbdev)
781 ret = get_bssid(usbdev, bssid);
783 return(ret == 0 && memcmp(bssid, zero_bssid, ETH_ALEN) != 0);
787 static int disassociate(struct usbnet *usbdev, int reset_ssid)
789 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
790 struct ndis_80211_ssid ssid;
793 if (priv->radio_on) {
794 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
797 devdbg(usbdev, "disassociate: radio_on = 0");
804 /* disassociate causes radio to be turned off; if reset_ssid
805 * is given, set random ssid to enable radio */
807 ssid.length = cpu_to_le32(sizeof(ssid.essid));
808 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
810 ssid.essid[1] = 0xff;
811 for (i = 2; i < sizeof(ssid.essid); i++)
812 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
813 ret = set_essid(usbdev, &ssid);
819 static int set_auth_mode(struct usbnet *usbdev, int wpa_version, int authalg)
821 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
825 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
826 "keymgmt=0x%x", wpa_version, authalg, priv->wpa_keymgmt);
828 if (wpa_version & IW_AUTH_WPA_VERSION_WPA2) {
829 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
830 auth_mode = ndis_80211_auth_wpa2;
832 auth_mode = ndis_80211_auth_wpa2_psk;
833 } else if (wpa_version & IW_AUTH_WPA_VERSION_WPA) {
834 if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_802_1X)
835 auth_mode = ndis_80211_auth_wpa;
836 else if (priv->wpa_keymgmt & IW_AUTH_KEY_MGMT_PSK)
837 auth_mode = ndis_80211_auth_wpa_psk;
839 auth_mode = ndis_80211_auth_wpa_none;
840 } else if (authalg & IW_AUTH_ALG_SHARED_KEY) {
841 if (authalg & IW_AUTH_ALG_OPEN_SYSTEM)
842 auth_mode = ndis_80211_auth_auto_switch;
844 auth_mode = ndis_80211_auth_shared;
846 auth_mode = ndis_80211_auth_open;
848 tmp = cpu_to_le32(auth_mode);
849 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
852 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
856 priv->wpa_version = wpa_version;
857 priv->wpa_authalg = authalg;
862 static int set_priv_filter(struct usbnet *usbdev)
864 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
867 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
869 if (priv->wpa_version & IW_AUTH_WPA_VERSION_WPA2 ||
870 priv->wpa_version & IW_AUTH_WPA_VERSION_WPA)
871 tmp = cpu_to_le32(ndis_80211_priv_8021x_wep);
873 tmp = cpu_to_le32(ndis_80211_priv_accept_all);
875 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
880 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
882 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
886 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
890 if (pairwise & IW_AUTH_CIPHER_CCMP)
891 encr_mode = ndis_80211_encr_ccmp_enabled;
892 else if (pairwise & IW_AUTH_CIPHER_TKIP)
893 encr_mode = ndis_80211_encr_tkip_enabled;
895 (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
896 encr_mode = ndis_80211_encr_wep_enabled;
897 else if (groupwise & IW_AUTH_CIPHER_CCMP)
898 encr_mode = ndis_80211_encr_ccmp_enabled;
899 else if (groupwise & IW_AUTH_CIPHER_TKIP)
900 encr_mode = ndis_80211_encr_tkip_enabled;
902 encr_mode = ndis_80211_encr_disabled;
904 tmp = cpu_to_le32(encr_mode);
905 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
908 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
912 priv->wpa_cipher_pair = pairwise;
913 priv->wpa_cipher_group = groupwise;
918 static int set_assoc_params(struct usbnet *usbdev)
920 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
922 set_auth_mode(usbdev, priv->wpa_version, priv->wpa_authalg);
923 set_priv_filter(usbdev);
924 set_encr_mode(usbdev, priv->wpa_cipher_pair, priv->wpa_cipher_group);
930 static int set_infra_mode(struct usbnet *usbdev, int mode)
932 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
936 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
938 tmp = cpu_to_le32(mode);
939 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
942 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
946 /* NDIS drivers clear keys when infrastructure mode is
947 * changed. But Linux tools assume otherwise. So set the
949 if (priv->wpa_keymgmt == 0 ||
950 priv->wpa_keymgmt == IW_AUTH_KEY_MGMT_802_1X) {
951 for (i = 0; i < 4; i++) {
952 if (priv->encr_key_len[i] > 0)
953 add_wep_key(usbdev, priv->encr_keys[i],
954 priv->encr_key_len[i], i);
958 priv->infra_mode = mode;
963 static void set_default_iw_params(struct usbnet *usbdev)
965 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
967 priv->wpa_keymgmt = 0;
968 priv->wpa_version = 0;
970 set_infra_mode(usbdev, ndis_80211_infra_infra);
971 set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
972 IW_AUTH_ALG_OPEN_SYSTEM);
973 set_priv_filter(usbdev);
974 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
978 static int deauthenticate(struct usbnet *usbdev)
982 ret = disassociate(usbdev, 1);
983 set_default_iw_params(usbdev);
988 /* index must be 0 - N, as per NDIS */
989 static int add_wep_key(struct usbnet *usbdev, char *key, int key_len, int index)
991 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
992 struct ndis_80211_wep_key ndis_key;
995 if (key_len <= 0 || key_len > 32 || index < 0 || index >= 4)
998 memset(&ndis_key, 0, sizeof(ndis_key));
1000 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1001 ndis_key.length = cpu_to_le32(key_len);
1002 ndis_key.index = cpu_to_le32(index);
1003 memcpy(&ndis_key.material, key, key_len);
1005 if (index == priv->encr_tx_key_index) {
1006 ndis_key.index |= cpu_to_le32(1 << 31);
1007 ret = set_encr_mode(usbdev, IW_AUTH_CIPHER_WEP104,
1008 IW_AUTH_CIPHER_NONE);
1010 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
1014 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1017 devwarn(usbdev, "adding encryption key %d failed (%08X)",
1022 priv->encr_key_len[index] = key_len;
1023 memcpy(&priv->encr_keys[index], key, key_len);
1029 /* remove_key is for both wep and wpa */
1030 static int remove_key(struct usbnet *usbdev, int index, u8 bssid[ETH_ALEN])
1032 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1033 struct ndis_80211_remove_key remove_key;
1037 if (priv->encr_key_len[index] == 0)
1040 priv->encr_key_len[index] = 0;
1041 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1043 if (priv->wpa_cipher_pair == IW_AUTH_CIPHER_TKIP ||
1044 priv->wpa_cipher_pair == IW_AUTH_CIPHER_CCMP ||
1045 priv->wpa_cipher_group == IW_AUTH_CIPHER_TKIP ||
1046 priv->wpa_cipher_group == IW_AUTH_CIPHER_CCMP) {
1047 remove_key.size = cpu_to_le32(sizeof(remove_key));
1048 remove_key.index = cpu_to_le32(index);
1051 if (memcmp(bssid, ffff_bssid, ETH_ALEN) != 0)
1052 remove_key.index |= cpu_to_le32(1 << 30);
1053 memcpy(remove_key.bssid, bssid,
1054 sizeof(remove_key.bssid));
1056 memset(remove_key.bssid, 0xff,
1057 sizeof(remove_key.bssid));
1059 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1060 sizeof(remove_key));
1064 keyindex = cpu_to_le32(index);
1065 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1069 "removing encryption key %d failed (%08X)",
1075 /* if it is transmit key, disable encryption */
1076 if (index == priv->encr_tx_key_index)
1077 set_encr_mode(usbdev, IW_AUTH_CIPHER_NONE, IW_AUTH_CIPHER_NONE);
1083 static void set_multicast_list(struct usbnet *usbdev)
1085 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1086 struct dev_mc_list *mclist;
1091 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1093 if (usbdev->net->flags & IFF_PROMISC) {
1094 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1095 RNDIS_PACKET_TYPE_ALL_LOCAL;
1096 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1097 usbdev->net->mc_count > priv->multicast_size) {
1098 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1099 } else if (usbdev->net->mc_count > 0) {
1100 size = min(priv->multicast_size, usbdev->net->mc_count);
1101 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1104 "couldn't alloc %d bytes of memory",
1109 mclist = usbdev->net->mc_list;
1110 for (i = 0; i < size && mclist; mclist = mclist->next) {
1111 if (mclist->dmi_addrlen != ETH_ALEN)
1114 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1118 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1120 if (ret == 0 && i > 0)
1121 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1123 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1125 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1126 i, priv->multicast_size, ret);
1131 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1134 devwarn(usbdev, "couldn't set packet filter: %08x",
1135 le32_to_cpu(filter));
1138 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1139 le32_to_cpu(filter), ret);
1146 static int rndis_change_virtual_intf(struct wiphy *wiphy, int ifindex,
1147 enum nl80211_iftype type, u32 *flags,
1148 struct vif_params *params)
1150 struct net_device *dev;
1151 struct usbnet *usbdev;
1154 /* we're under RTNL */
1155 dev = __dev_get_by_index(&init_net, ifindex);
1158 usbdev = netdev_priv(dev);
1161 case NL80211_IFTYPE_ADHOC:
1162 mode = ndis_80211_infra_adhoc;
1164 case NL80211_IFTYPE_STATION:
1165 mode = ndis_80211_infra_infra;
1171 return set_infra_mode(usbdev, mode);
1175 #define SCAN_DELAY_JIFFIES (HZ)
1176 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1177 struct cfg80211_scan_request *request)
1179 struct usbnet *usbdev = netdev_priv(dev);
1180 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1184 devdbg(usbdev, "cfg80211.scan");
1189 if (priv->scan_request && priv->scan_request != request)
1192 priv->scan_request = request;
1194 tmp = cpu_to_le32(1);
1195 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1198 /* Wait before retrieving scan results from device */
1199 queue_delayed_work(priv->workqueue, &priv->scan_work,
1200 SCAN_DELAY_JIFFIES);
1207 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1208 struct ndis_80211_bssid_ex *bssid)
1210 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1211 struct ieee80211_channel *channel;
1215 u16 beacon_interval;
1216 struct ndis_80211_fixed_ies *fixed;
1217 int ie_len, bssid_len;
1220 /* parse bssid structure */
1221 bssid_len = le32_to_cpu(bssid->length);
1223 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1224 sizeof(struct ndis_80211_fixed_ies))
1227 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1229 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1230 ie_len = min(bssid_len - (int)sizeof(*bssid),
1231 (int)le32_to_cpu(bssid->ie_length));
1232 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1236 /* extract data for cfg80211_inform_bss */
1237 channel = ieee80211_get_channel(priv->wdev.wiphy,
1238 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1242 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1243 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1244 capability = le16_to_cpu(fixed->capabilities);
1245 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1247 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1248 timestamp, capability, beacon_interval, ie, ie_len, signal,
1253 static int rndis_check_bssid_list(struct usbnet *usbdev)
1256 struct ndis_80211_bssid_list_ex *bssid_list;
1257 struct ndis_80211_bssid_ex *bssid;
1258 int ret = -EINVAL, len, count, bssid_len;
1260 devdbg(usbdev, "check_bssid_list");
1262 len = CONTROL_BUFFER_SIZE;
1263 buf = kmalloc(len, GFP_KERNEL);
1269 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1274 bssid = bssid_list->bssid;
1275 bssid_len = le32_to_cpu(bssid->length);
1276 count = le32_to_cpu(bssid_list->num_items);
1277 devdbg(usbdev, "check_bssid_list: %d BSSIDs found", count);
1279 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1280 rndis_bss_info_update(usbdev, bssid);
1282 bssid = (void *)bssid + bssid_len;
1283 bssid_len = le32_to_cpu(bssid->length);
1293 static void rndis_get_scan_results(struct work_struct *work)
1295 struct rndis_wext_private *priv =
1296 container_of(work, struct rndis_wext_private, scan_work.work);
1297 struct usbnet *usbdev = priv->usbdev;
1300 devdbg(usbdev, "get_scan_results");
1302 ret = rndis_check_bssid_list(usbdev);
1304 cfg80211_scan_done(priv->scan_request, ret < 0);
1306 priv->scan_request = NULL;
1311 * wireless extension handlers
1314 static int rndis_iw_commit(struct net_device *dev,
1315 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1322 static int rndis_iw_set_essid(struct net_device *dev,
1323 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1325 struct ndis_80211_ssid ssid;
1326 int length = wrqu->essid.length;
1327 struct usbnet *usbdev = netdev_priv(dev);
1329 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
1330 wrqu->essid.flags, wrqu->essid.length, essid);
1332 if (length > NDIS_802_11_LENGTH_SSID)
1333 length = NDIS_802_11_LENGTH_SSID;
1335 ssid.length = cpu_to_le32(length);
1337 memcpy(ssid.essid, essid, length);
1339 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
1341 set_assoc_params(usbdev);
1343 if (!wrqu->essid.flags || length == 0)
1344 return disassociate(usbdev, 1);
1346 return set_essid(usbdev, &ssid);
1350 static int rndis_iw_get_essid(struct net_device *dev,
1351 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
1353 struct ndis_80211_ssid ssid;
1354 struct usbnet *usbdev = netdev_priv(dev);
1357 ret = get_essid(usbdev, &ssid);
1359 if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
1360 wrqu->essid.flags = 1;
1361 wrqu->essid.length = le32_to_cpu(ssid.length);
1362 memcpy(essid, ssid.essid, wrqu->essid.length);
1363 essid[wrqu->essid.length] = 0;
1365 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
1366 wrqu->essid.flags = 0;
1367 wrqu->essid.length = 0;
1369 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
1374 static int rndis_iw_get_bssid(struct net_device *dev,
1375 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1377 struct usbnet *usbdev = netdev_priv(dev);
1378 unsigned char bssid[ETH_ALEN];
1381 ret = get_bssid(usbdev, bssid);
1384 devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
1386 devdbg(usbdev, "SIOCGIWAP: <not associated>");
1388 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1389 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
1395 static int rndis_iw_set_bssid(struct net_device *dev,
1396 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1398 struct usbnet *usbdev = netdev_priv(dev);
1399 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
1402 devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
1404 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1406 /* user apps may set ap's mac address, which is not required;
1407 * they may fail to work if this function fails, so return
1410 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
1416 static int rndis_iw_set_auth(struct net_device *dev,
1417 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1419 struct iw_param *p = &wrqu->param;
1420 struct usbnet *usbdev = netdev_priv(dev);
1421 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1422 int ret = -ENOTSUPP;
1424 switch (p->flags & IW_AUTH_INDEX) {
1425 case IW_AUTH_WPA_VERSION:
1426 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
1427 priv->wpa_version = p->value;
1431 case IW_AUTH_CIPHER_PAIRWISE:
1432 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
1433 priv->wpa_cipher_pair = p->value;
1437 case IW_AUTH_CIPHER_GROUP:
1438 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
1439 priv->wpa_cipher_group = p->value;
1443 case IW_AUTH_KEY_MGMT:
1444 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
1445 priv->wpa_keymgmt = p->value;
1449 case IW_AUTH_TKIP_COUNTERMEASURES:
1450 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
1455 case IW_AUTH_DROP_UNENCRYPTED:
1456 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
1460 case IW_AUTH_80211_AUTH_ALG:
1461 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
1462 priv->wpa_authalg = p->value;
1466 case IW_AUTH_WPA_ENABLED:
1467 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
1468 if (wrqu->param.value)
1469 deauthenticate(usbdev);
1473 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1474 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
1479 case IW_AUTH_ROAMING_CONTROL:
1480 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
1484 case IW_AUTH_PRIVACY_INVOKED:
1485 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
1486 wrqu->param.flags & IW_AUTH_INDEX);
1490 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
1491 p->flags & IW_AUTH_INDEX, p->value);
1497 static int rndis_iw_get_auth(struct net_device *dev,
1498 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1500 struct iw_param *p = &wrqu->param;
1501 struct usbnet *usbdev = netdev_priv(dev);
1502 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1504 switch (p->flags & IW_AUTH_INDEX) {
1505 case IW_AUTH_WPA_VERSION:
1506 p->value = priv->wpa_version;
1508 case IW_AUTH_CIPHER_PAIRWISE:
1509 p->value = priv->wpa_cipher_pair;
1511 case IW_AUTH_CIPHER_GROUP:
1512 p->value = priv->wpa_cipher_group;
1514 case IW_AUTH_KEY_MGMT:
1515 p->value = priv->wpa_keymgmt;
1517 case IW_AUTH_80211_AUTH_ALG:
1518 p->value = priv->wpa_authalg;
1521 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
1522 wrqu->param.flags & IW_AUTH_INDEX);
1529 static int rndis_iw_set_encode(struct net_device *dev,
1530 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1532 struct usbnet *usbdev = netdev_priv(dev);
1533 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1534 int ret, index, key_len;
1537 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
1539 /* iwconfig gives index as 1 - N */
1543 index = priv->encr_tx_key_index;
1545 if (index < 0 || index >= 4) {
1546 devwarn(usbdev, "encryption index out of range (%u)", index);
1550 /* remove key if disabled */
1551 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
1552 if (remove_key(usbdev, index, NULL))
1558 /* global encryption state (for all keys) */
1559 if (wrqu->data.flags & IW_ENCODE_OPEN)
1560 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1561 IW_AUTH_ALG_OPEN_SYSTEM);
1562 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
1563 ret = set_auth_mode(usbdev, IW_AUTH_WPA_VERSION_DISABLED,
1564 IW_AUTH_ALG_SHARED_KEY);
1568 if (wrqu->data.length > 0) {
1569 key_len = wrqu->data.length;
1572 /* must be set as tx key */
1573 if (priv->encr_key_len[index] == 0)
1575 key_len = priv->encr_key_len[index];
1576 key = priv->encr_keys[index];
1577 priv->encr_tx_key_index = index;
1580 if (add_wep_key(usbdev, key, key_len, index) != 0)
1583 if (index == priv->encr_tx_key_index)
1584 /* ndis drivers want essid to be set after setting encr */
1585 set_essid(usbdev, &priv->essid);
1591 static int rndis_iw_set_encode_ext(struct net_device *dev,
1592 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1594 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1595 struct usbnet *usbdev = netdev_priv(dev);
1596 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1597 struct ndis_80211_key ndis_key;
1601 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
1603 /* iwconfig gives index as 1 - N */
1607 keyidx = priv->encr_tx_key_index;
1609 if (keyidx < 0 || keyidx >= 4)
1612 if (ext->alg == WPA_ALG_WEP) {
1613 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1614 priv->encr_tx_key_index = keyidx;
1615 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
1618 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
1619 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
1620 return remove_key(usbdev, keyidx, NULL);
1622 if (ext->key_len > sizeof(ndis_key.material))
1625 memset(&ndis_key, 0, sizeof(ndis_key));
1627 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1628 sizeof(ndis_key.material) + ext->key_len);
1629 ndis_key.length = cpu_to_le32(ext->key_len);
1630 ndis_key.index = cpu_to_le32(keyidx);
1632 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1633 memcpy(ndis_key.rsc, ext->rx_seq, 6);
1634 ndis_key.index |= cpu_to_le32(1 << 29);
1637 addr = ext->addr.sa_data;
1638 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1640 if (priv->infra_mode == ndis_80211_infra_adhoc)
1641 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1643 get_bssid(usbdev, ndis_key.bssid);
1646 ndis_key.index |= cpu_to_le32(1 << 30);
1647 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1650 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1651 ndis_key.index |= cpu_to_le32(1 << 31);
1653 if (ext->alg == IW_ENCODE_ALG_TKIP && ext->key_len == 32) {
1654 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1655 * different order than NDIS spec, so swap the order here. */
1656 memcpy(ndis_key.material, ext->key, 16);
1657 memcpy(ndis_key.material + 16, ext->key + 24, 8);
1658 memcpy(ndis_key.material + 24, ext->key + 16, 8);
1660 memcpy(ndis_key.material, ext->key, ext->key_len);
1662 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1663 le32_to_cpu(ndis_key.size));
1664 devdbg(usbdev, "SIOCSIWENCODEEXT: OID_802_11_ADD_KEY -> %08X", ret);
1668 priv->encr_key_len[keyidx] = ext->key_len;
1669 memcpy(&priv->encr_keys[keyidx], ndis_key.material, ext->key_len);
1670 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1671 priv->encr_tx_key_index = keyidx;
1677 static int rndis_iw_set_genie(struct net_device *dev,
1678 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1680 struct usbnet *usbdev = netdev_priv(dev);
1681 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1687 for (j = 0; j < wrqu->data.length; j += 8)
1689 "SIOCSIWGENIE %04x - "
1690 "%02x %02x %02x %02x %02x %02x %02x %02x", j,
1691 gie[j + 0], gie[j + 1], gie[j + 2], gie[j + 3],
1692 gie[j + 4], gie[j + 5], gie[j + 6], gie[j + 7]);
1694 /* clear existing IEs */
1695 if (priv->wpa_ie_len) {
1696 kfree(priv->wpa_ie);
1697 priv->wpa_ie_len = 0;
1701 priv->wpa_ie = kmalloc(wrqu->data.length, GFP_KERNEL);
1703 priv->wpa_ie_len = wrqu->data.length;
1704 memcpy(priv->wpa_ie, extra, priv->wpa_ie_len);
1711 static int rndis_iw_get_genie(struct net_device *dev,
1712 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1714 struct usbnet *usbdev = netdev_priv(dev);
1715 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1717 devdbg(usbdev, "SIOCGIWGENIE");
1719 if (priv->wpa_ie_len == 0 || priv->wpa_ie == NULL) {
1720 wrqu->data.length = 0;
1724 if (wrqu->data.length < priv->wpa_ie_len)
1727 wrqu->data.length = priv->wpa_ie_len;
1728 memcpy(extra, priv->wpa_ie, priv->wpa_ie_len);
1734 static int rndis_iw_set_rts(struct net_device *dev,
1735 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1737 struct usbnet *usbdev = netdev_priv(dev);
1739 devdbg(usbdev, "SIOCSIWRTS");
1741 tmp = cpu_to_le32(wrqu->rts.value);
1742 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1747 static int rndis_iw_get_rts(struct net_device *dev,
1748 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1750 struct usbnet *usbdev = netdev_priv(dev);
1755 ret = rndis_query_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp, &len);
1757 wrqu->rts.value = le32_to_cpu(tmp);
1758 wrqu->rts.flags = 1;
1759 wrqu->rts.disabled = 0;
1762 devdbg(usbdev, "SIOCGIWRTS: %d", wrqu->rts.value);
1768 static int rndis_iw_set_frag(struct net_device *dev,
1769 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1771 struct usbnet *usbdev = netdev_priv(dev);
1774 devdbg(usbdev, "SIOCSIWFRAG");
1776 tmp = cpu_to_le32(wrqu->frag.value);
1777 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1782 static int rndis_iw_get_frag(struct net_device *dev,
1783 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1785 struct usbnet *usbdev = netdev_priv(dev);
1790 ret = rndis_query_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1793 wrqu->frag.value = le32_to_cpu(tmp);
1794 wrqu->frag.flags = 1;
1795 wrqu->frag.disabled = 0;
1797 devdbg(usbdev, "SIOCGIWFRAG: %d", wrqu->frag.value);
1802 static int rndis_iw_set_nick(struct net_device *dev,
1803 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1805 struct usbnet *usbdev = netdev_priv(dev);
1806 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1808 devdbg(usbdev, "SIOCSIWNICK");
1810 priv->nick_len = wrqu->data.length;
1811 if (priv->nick_len > 32)
1812 priv->nick_len = 32;
1814 memcpy(priv->nick, extra, priv->nick_len);
1819 static int rndis_iw_get_nick(struct net_device *dev,
1820 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1822 struct usbnet *usbdev = netdev_priv(dev);
1823 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1825 wrqu->data.flags = 1;
1826 wrqu->data.length = priv->nick_len;
1827 memcpy(extra, priv->nick, priv->nick_len);
1829 devdbg(usbdev, "SIOCGIWNICK: '%s'", priv->nick);
1835 static int rndis_iw_set_freq(struct net_device *dev,
1836 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1838 struct usbnet *usbdev = netdev_priv(dev);
1839 struct ndis_80211_conf config;
1840 unsigned int dsconfig;
1843 /* this OID is valid only when not associated */
1844 if (is_associated(usbdev))
1848 if (freq_to_dsconfig(&wrqu->freq, &dsconfig))
1851 len = sizeof(config);
1852 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1854 devdbg(usbdev, "SIOCSIWFREQ: querying configuration failed");
1858 config.ds_config = cpu_to_le32(dsconfig);
1860 devdbg(usbdev, "SIOCSIWFREQ: %d * 10^%d", wrqu->freq.m, wrqu->freq.e);
1861 return rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1866 static int rndis_iw_get_freq(struct net_device *dev,
1867 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1869 struct usbnet *usbdev = netdev_priv(dev);
1870 struct ndis_80211_conf config;
1873 len = sizeof(config);
1874 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1876 dsconfig_to_freq(le32_to_cpu(config.ds_config), &wrqu->freq);
1878 devdbg(usbdev, "SIOCGIWFREQ: %d", wrqu->freq.m);
1883 static int rndis_iw_get_txpower(struct net_device *dev,
1884 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1886 struct usbnet *usbdev = netdev_priv(dev);
1887 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1891 if (priv->radio_on) {
1892 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1893 len = sizeof(tx_power);
1894 ret = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1899 /* fake incase not supported */
1900 tx_power = cpu_to_le32(get_bcm4320_power(priv));
1902 wrqu->txpower.flags = IW_TXPOW_MWATT;
1903 wrqu->txpower.value = le32_to_cpu(tx_power);
1904 wrqu->txpower.disabled = 0;
1906 wrqu->txpower.flags = IW_TXPOW_MWATT;
1907 wrqu->txpower.value = 0;
1908 wrqu->txpower.disabled = 1;
1911 devdbg(usbdev, "SIOCGIWTXPOW: %d", wrqu->txpower.value);
1917 static int rndis_iw_set_txpower(struct net_device *dev,
1918 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1920 struct usbnet *usbdev = netdev_priv(dev);
1921 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1922 __le32 tx_power = 0;
1925 if (!wrqu->txpower.disabled) {
1926 if (wrqu->txpower.flags == IW_TXPOW_MWATT)
1927 tx_power = cpu_to_le32(wrqu->txpower.value);
1928 else { /* wrqu->txpower.flags == IW_TXPOW_DBM */
1929 if (wrqu->txpower.value > 20)
1930 tx_power = cpu_to_le32(128);
1931 else if (wrqu->txpower.value < -43)
1932 tx_power = cpu_to_le32(127);
1935 tmp = wrqu->txpower.value;
1938 tx_power = cpu_to_le32((unsigned char)tmp);
1943 devdbg(usbdev, "SIOCSIWTXPOW: %d", le32_to_cpu(tx_power));
1945 if (le32_to_cpu(tx_power) != 0) {
1946 if (priv->caps & CAP_SUPPORT_TXPOWER) {
1947 /* turn radio on first */
1948 if (!priv->radio_on)
1949 disassociate(usbdev, 1);
1951 ret = rndis_set_oid(usbdev, OID_802_11_TX_POWER_LEVEL,
1952 &tx_power, sizeof(tx_power));
1957 /* txpower unsupported, just turn radio on */
1958 if (!priv->radio_on)
1959 return disassociate(usbdev, 1);
1960 return 0; /* all ready on */
1964 /* tx_power == 0, turn off radio */
1965 return disassociate(usbdev, 0);
1969 static int rndis_iw_get_rate(struct net_device *dev,
1970 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1972 struct usbnet *usbdev = netdev_priv(dev);
1977 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
1979 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
1980 wrqu->bitrate.disabled = 0;
1981 wrqu->bitrate.flags = 1;
1987 static int rndis_iw_set_mlme(struct net_device *dev,
1988 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
1990 struct usbnet *usbdev = netdev_priv(dev);
1991 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
1992 struct iw_mlme *mlme = (struct iw_mlme *)extra;
1993 unsigned char bssid[ETH_ALEN];
1995 get_bssid(usbdev, bssid);
1997 if (memcmp(bssid, mlme->addr.sa_data, ETH_ALEN))
2000 switch (mlme->cmd) {
2001 case IW_MLME_DEAUTH:
2002 return deauthenticate(usbdev);
2003 case IW_MLME_DISASSOC:
2004 return disassociate(usbdev, priv->radio_on);
2013 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2015 struct usbnet *usbdev = netdev_priv(dev);
2016 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2017 unsigned long flags;
2019 spin_lock_irqsave(&priv->stats_lock, flags);
2020 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2021 spin_unlock_irqrestore(&priv->stats_lock, flags);
2023 return &priv->iwstats;
2027 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2028 static const iw_handler rndis_iw_handler[] =
2030 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2031 IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
2032 IW_IOCTL(SIOCSIWFREQ) = rndis_iw_set_freq,
2033 IW_IOCTL(SIOCGIWFREQ) = rndis_iw_get_freq,
2034 IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
2035 IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
2036 IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
2037 IW_IOCTL(SIOCSIWAP) = rndis_iw_set_bssid,
2038 IW_IOCTL(SIOCGIWAP) = rndis_iw_get_bssid,
2039 IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
2040 IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
2041 IW_IOCTL(SIOCSIWESSID) = rndis_iw_set_essid,
2042 IW_IOCTL(SIOCGIWESSID) = rndis_iw_get_essid,
2043 IW_IOCTL(SIOCSIWNICKN) = rndis_iw_set_nick,
2044 IW_IOCTL(SIOCGIWNICKN) = rndis_iw_get_nick,
2045 IW_IOCTL(SIOCGIWRATE) = rndis_iw_get_rate,
2046 IW_IOCTL(SIOCSIWRTS) = rndis_iw_set_rts,
2047 IW_IOCTL(SIOCGIWRTS) = rndis_iw_get_rts,
2048 IW_IOCTL(SIOCSIWFRAG) = rndis_iw_set_frag,
2049 IW_IOCTL(SIOCGIWFRAG) = rndis_iw_get_frag,
2050 IW_IOCTL(SIOCSIWTXPOW) = rndis_iw_set_txpower,
2051 IW_IOCTL(SIOCGIWTXPOW) = rndis_iw_get_txpower,
2052 IW_IOCTL(SIOCSIWENCODE) = rndis_iw_set_encode,
2053 IW_IOCTL(SIOCSIWENCODEEXT) = rndis_iw_set_encode_ext,
2054 IW_IOCTL(SIOCSIWAUTH) = rndis_iw_set_auth,
2055 IW_IOCTL(SIOCGIWAUTH) = rndis_iw_get_auth,
2056 IW_IOCTL(SIOCSIWGENIE) = rndis_iw_set_genie,
2057 IW_IOCTL(SIOCGIWGENIE) = rndis_iw_get_genie,
2058 IW_IOCTL(SIOCSIWMLME) = rndis_iw_set_mlme,
2061 static const iw_handler rndis_wext_private_handler[] = {
2064 static const struct iw_priv_args rndis_wext_private_args[] = {
2068 static const struct iw_handler_def rndis_iw_handlers = {
2069 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2070 .num_private = ARRAY_SIZE(rndis_wext_private_handler),
2071 .num_private_args = ARRAY_SIZE(rndis_wext_private_args),
2072 .standard = (iw_handler *)rndis_iw_handler,
2073 .private = (iw_handler *)rndis_wext_private_handler,
2074 .private_args = (struct iw_priv_args *)rndis_wext_private_args,
2075 .get_wireless_stats = rndis_get_wireless_stats,
2079 static void rndis_wext_worker(struct work_struct *work)
2081 struct rndis_wext_private *priv =
2082 container_of(work, struct rndis_wext_private, work);
2083 struct usbnet *usbdev = priv->usbdev;
2084 union iwreq_data evt;
2085 unsigned char bssid[ETH_ALEN];
2086 struct ndis_80211_assoc_info *info;
2087 int assoc_size = sizeof(*info) + IW_CUSTOM_MAX + 32;
2090 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending)) {
2091 netif_carrier_on(usbdev->net);
2093 info = kzalloc(assoc_size, GFP_KERNEL);
2097 /* Get association info IEs from device and send them back to
2099 ret = get_association_info(usbdev, info, assoc_size);
2101 evt.data.length = le32_to_cpu(info->req_ie_length);
2102 if (evt.data.length > 0) {
2103 offset = le32_to_cpu(info->offset_req_ies);
2104 wireless_send_event(usbdev->net,
2105 IWEVASSOCREQIE, &evt,
2106 (char *)info + offset);
2109 evt.data.length = le32_to_cpu(info->resp_ie_length);
2110 if (evt.data.length > 0) {
2111 offset = le32_to_cpu(info->offset_resp_ies);
2112 wireless_send_event(usbdev->net,
2113 IWEVASSOCRESPIE, &evt,
2114 (char *)info + offset);
2121 ret = get_bssid(usbdev, bssid);
2124 evt.data.length = 0;
2125 memcpy(evt.ap_addr.sa_data, bssid, ETH_ALEN);
2126 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2130 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending)) {
2131 netif_carrier_off(usbdev->net);
2134 evt.data.length = 0;
2135 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2136 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2139 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2140 set_multicast_list(usbdev);
2143 static void rndis_wext_set_multicast_list(struct net_device *dev)
2145 struct usbnet *usbdev = netdev_priv(dev);
2146 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2148 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2151 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2152 queue_work(priv->workqueue, &priv->work);
2155 static void rndis_wext_link_change(struct usbnet *usbdev, int state)
2157 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2159 /* queue work to avoid recursive calls into rndis_command */
2160 set_bit(state ? WORK_LINK_UP : WORK_LINK_DOWN, &priv->work_pending);
2161 queue_work(priv->workqueue, &priv->work);
2165 static int rndis_wext_get_caps(struct usbnet *usbdev)
2170 } networks_supported;
2171 int len, retval, i, n;
2173 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2175 /* determine if supports setting txpower */
2176 len = sizeof(tx_power);
2177 retval = rndis_query_oid(usbdev, OID_802_11_TX_POWER_LEVEL, &tx_power,
2179 if (retval == 0 && le32_to_cpu(tx_power) != 0xFF)
2180 priv->caps |= CAP_SUPPORT_TXPOWER;
2182 /* determine supported modes */
2183 len = sizeof(networks_supported);
2184 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2185 &networks_supported, &len);
2187 n = le32_to_cpu(networks_supported.num_items);
2190 for (i = 0; i < n; i++) {
2191 switch (le32_to_cpu(networks_supported.items[i])) {
2192 case ndis_80211_type_freq_hop:
2193 case ndis_80211_type_direct_seq:
2194 priv->caps |= CAP_MODE_80211B;
2196 case ndis_80211_type_ofdm_a:
2197 priv->caps |= CAP_MODE_80211A;
2199 case ndis_80211_type_ofdm_g:
2200 priv->caps |= CAP_MODE_80211G;
2210 #define STATS_UPDATE_JIFFIES (HZ)
2211 static void rndis_update_wireless_stats(struct work_struct *work)
2213 struct rndis_wext_private *priv =
2214 container_of(work, struct rndis_wext_private, stats_work.work);
2215 struct usbnet *usbdev = priv->usbdev;
2216 struct iw_statistics iwstats;
2219 unsigned long flags;
2220 int update_jiffies = STATS_UPDATE_JIFFIES;
2223 spin_lock_irqsave(&priv->stats_lock, flags);
2224 memcpy(&iwstats, &priv->privstats, sizeof(iwstats));
2225 spin_unlock_irqrestore(&priv->stats_lock, flags);
2227 /* only update stats when connected */
2228 if (!is_associated(usbdev)) {
2229 iwstats.qual.qual = 0;
2230 iwstats.qual.level = 0;
2231 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2232 | IW_QUAL_LEVEL_UPDATED
2233 | IW_QUAL_NOISE_INVALID
2234 | IW_QUAL_QUAL_INVALID
2235 | IW_QUAL_LEVEL_INVALID;
2240 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2242 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
2245 memset(&iwstats.qual, 0, sizeof(iwstats.qual));
2246 iwstats.qual.qual = level_to_qual(le32_to_cpu(rssi));
2247 iwstats.qual.level = level_to_qual(le32_to_cpu(rssi));
2248 iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2249 | IW_QUAL_LEVEL_UPDATED
2250 | IW_QUAL_NOISE_INVALID;
2253 memset(&iwstats.discard, 0, sizeof(iwstats.discard));
2256 ret = rndis_query_oid(usbdev, OID_GEN_XMIT_ERROR, &tmp, &len);
2258 iwstats.discard.misc += le32_to_cpu(tmp);
2261 ret = rndis_query_oid(usbdev, OID_GEN_RCV_ERROR, &tmp, &len);
2263 iwstats.discard.misc += le32_to_cpu(tmp);
2266 ret = rndis_query_oid(usbdev, OID_GEN_RCV_NO_BUFFER, &tmp, &len);
2268 iwstats.discard.misc += le32_to_cpu(tmp);
2270 /* Workaround transfer stalls on poor quality links.
2271 * TODO: find right way to fix these stalls (as stalls do not happen
2272 * with ndiswrapper/windows driver). */
2273 if (iwstats.qual.qual <= 25) {
2274 /* Decrease stats worker interval to catch stalls.
2275 * faster. Faster than 400-500ms causes packet loss,
2276 * Slower doesn't catch stalls fast enough.
2278 j = msecs_to_jiffies(priv->param_workaround_interval);
2279 if (j > STATS_UPDATE_JIFFIES)
2280 j = STATS_UPDATE_JIFFIES;
2285 /* Send scan OID. Use of both OIDs is required to get device
2288 tmp = cpu_to_le32(1);
2289 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2292 len = CONTROL_BUFFER_SIZE;
2293 buf = kmalloc(len, GFP_KERNEL);
2297 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2301 spin_lock_irqsave(&priv->stats_lock, flags);
2302 memcpy(&priv->privstats, &iwstats, sizeof(iwstats));
2303 spin_unlock_irqrestore(&priv->stats_lock, flags);
2305 if (update_jiffies >= HZ)
2306 update_jiffies = round_jiffies_relative(update_jiffies);
2308 j = round_jiffies_relative(update_jiffies);
2309 if (abs(j - update_jiffies) <= 10)
2313 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
2317 static int bcm4320_early_init(struct usbnet *usbdev)
2319 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2322 /* Early initialization settings, setting these won't have effect
2323 * if called after generic_rndis_bind().
2326 priv->param_country[0] = modparam_country[0];
2327 priv->param_country[1] = modparam_country[1];
2328 priv->param_country[2] = 0;
2329 priv->param_frameburst = modparam_frameburst;
2330 priv->param_afterburner = modparam_afterburner;
2331 priv->param_power_save = modparam_power_save;
2332 priv->param_power_output = modparam_power_output;
2333 priv->param_roamtrigger = modparam_roamtrigger;
2334 priv->param_roamdelta = modparam_roamdelta;
2336 priv->param_country[0] = toupper(priv->param_country[0]);
2337 priv->param_country[1] = toupper(priv->param_country[1]);
2338 /* doesn't support EU as country code, use FI instead */
2339 if (!strcmp(priv->param_country, "EU"))
2340 strcpy(priv->param_country, "FI");
2342 if (priv->param_power_save < 0)
2343 priv->param_power_save = 0;
2344 else if (priv->param_power_save > 2)
2345 priv->param_power_save = 2;
2347 if (priv->param_power_output < 0)
2348 priv->param_power_output = 0;
2349 else if (priv->param_power_output > 3)
2350 priv->param_power_output = 3;
2352 if (priv->param_roamtrigger < -80)
2353 priv->param_roamtrigger = -80;
2354 else if (priv->param_roamtrigger > -60)
2355 priv->param_roamtrigger = -60;
2357 if (priv->param_roamdelta < 0)
2358 priv->param_roamdelta = 0;
2359 else if (priv->param_roamdelta > 2)
2360 priv->param_roamdelta = 2;
2362 if (modparam_workaround_interval < 0)
2363 priv->param_workaround_interval = 500;
2365 priv->param_workaround_interval = modparam_workaround_interval;
2367 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
2368 rndis_set_config_parameter_str(usbdev, "FrameBursting",
2369 priv->param_frameburst ? "1" : "0");
2370 rndis_set_config_parameter_str(usbdev, "Afterburner",
2371 priv->param_afterburner ? "1" : "0");
2372 sprintf(buf, "%d", priv->param_power_save);
2373 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
2374 sprintf(buf, "%d", priv->param_power_output);
2375 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
2376 sprintf(buf, "%d", priv->param_roamtrigger);
2377 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
2378 sprintf(buf, "%d", priv->param_roamdelta);
2379 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
2384 /* same as rndis_netdev_ops but with local multicast handler */
2385 static const struct net_device_ops rndis_wext_netdev_ops = {
2386 .ndo_open = usbnet_open,
2387 .ndo_stop = usbnet_stop,
2388 .ndo_start_xmit = usbnet_start_xmit,
2389 .ndo_tx_timeout = usbnet_tx_timeout,
2390 .ndo_set_mac_address = eth_mac_addr,
2391 .ndo_validate_addr = eth_validate_addr,
2392 .ndo_set_multicast_list = rndis_wext_set_multicast_list,
2396 static int rndis_wext_bind(struct usbnet *usbdev, struct usb_interface *intf)
2398 struct wiphy *wiphy;
2399 struct rndis_wext_private *priv;
2403 /* allocate wiphy and rndis private data
2404 * NOTE: We only support a single virtual interface, so wiphy
2405 * and wireless_dev are somewhat synonymous for this device.
2407 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wext_private));
2411 priv = wiphy_priv(wiphy);
2412 usbdev->net->ieee80211_ptr = &priv->wdev;
2413 priv->wdev.wiphy = wiphy;
2414 priv->wdev.iftype = NL80211_IFTYPE_STATION;
2416 /* These have to be initialized before calling generic_rndis_bind().
2417 * Otherwise we'll be in big trouble in rndis_wext_early_init().
2419 usbdev->driver_priv = priv;
2420 usbdev->net->wireless_handlers = &rndis_iw_handlers;
2421 priv->usbdev = usbdev;
2423 mutex_init(&priv->command_lock);
2424 spin_lock_init(&priv->stats_lock);
2426 /* try bind rndis_host */
2427 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
2431 /* generic_rndis_bind set packet filter to multicast_all+
2432 * promisc mode which doesn't work well for our devices (device
2433 * picks up rssi to closest station instead of to access point).
2435 * rndis_host wants to avoid all OID as much as possible
2436 * so do promisc/multicast handling in rndis_wext.
2438 usbdev->net->netdev_ops = &rndis_wext_netdev_ops;
2440 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
2441 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
2445 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
2447 priv->multicast_size = le32_to_cpu(tmp);
2448 if (retval < 0 || priv->multicast_size < 0)
2449 priv->multicast_size = 0;
2450 if (priv->multicast_size > 0)
2451 usbdev->net->flags |= IFF_MULTICAST;
2453 usbdev->net->flags &= ~IFF_MULTICAST;
2455 priv->iwstats.qual.qual = 0;
2456 priv->iwstats.qual.level = 0;
2457 priv->iwstats.qual.updated = IW_QUAL_QUAL_UPDATED
2458 | IW_QUAL_LEVEL_UPDATED
2459 | IW_QUAL_NOISE_INVALID
2460 | IW_QUAL_QUAL_INVALID
2461 | IW_QUAL_LEVEL_INVALID;
2463 /* fill-out wiphy structure and register w/ cfg80211 */
2464 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
2465 wiphy->privid = rndis_wiphy_privid;
2466 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
2467 | BIT(NL80211_IFTYPE_ADHOC);
2468 wiphy->max_scan_ssids = 1;
2470 /* TODO: fill-out band information based on priv->caps */
2471 rndis_wext_get_caps(usbdev);
2473 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
2474 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
2475 priv->band.channels = priv->channels;
2476 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
2477 priv->band.bitrates = priv->rates;
2478 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
2479 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
2480 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2482 set_wiphy_dev(wiphy, &usbdev->udev->dev);
2484 if (wiphy_register(wiphy)) {
2489 set_default_iw_params(usbdev);
2493 disassociate(usbdev, 1);
2494 netif_carrier_off(usbdev->net);
2496 /* because rndis_command() sleeps we need to use workqueue */
2497 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
2498 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
2499 queue_delayed_work(priv->workqueue, &priv->stats_work,
2500 round_jiffies_relative(STATS_UPDATE_JIFFIES));
2501 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
2502 INIT_WORK(&priv->work, rndis_wext_worker);
2512 static void rndis_wext_unbind(struct usbnet *usbdev, struct usb_interface *intf)
2514 struct rndis_wext_private *priv = get_rndis_wext_priv(usbdev);
2516 /* turn radio off */
2517 disassociate(usbdev, 0);
2519 cancel_delayed_work_sync(&priv->stats_work);
2520 cancel_delayed_work_sync(&priv->scan_work);
2521 cancel_work_sync(&priv->work);
2522 flush_workqueue(priv->workqueue);
2523 destroy_workqueue(priv->workqueue);
2525 if (priv && priv->wpa_ie_len)
2526 kfree(priv->wpa_ie);
2528 rndis_unbind(usbdev, intf);
2530 wiphy_unregister(priv->wdev.wiphy);
2531 wiphy_free(priv->wdev.wiphy);
2535 static int rndis_wext_reset(struct usbnet *usbdev)
2537 return deauthenticate(usbdev);
2541 static const struct driver_info bcm4320b_info = {
2542 .description = "Wireless RNDIS device, BCM4320b based",
2543 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2544 .bind = rndis_wext_bind,
2545 .unbind = rndis_wext_unbind,
2546 .status = rndis_status,
2547 .rx_fixup = rndis_rx_fixup,
2548 .tx_fixup = rndis_tx_fixup,
2549 .reset = rndis_wext_reset,
2550 .early_init = bcm4320_early_init,
2551 .link_change = rndis_wext_link_change,
2554 static const struct driver_info bcm4320a_info = {
2555 .description = "Wireless RNDIS device, BCM4320a based",
2556 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2557 .bind = rndis_wext_bind,
2558 .unbind = rndis_wext_unbind,
2559 .status = rndis_status,
2560 .rx_fixup = rndis_rx_fixup,
2561 .tx_fixup = rndis_tx_fixup,
2562 .reset = rndis_wext_reset,
2563 .early_init = bcm4320_early_init,
2564 .link_change = rndis_wext_link_change,
2567 static const struct driver_info rndis_wext_info = {
2568 .description = "Wireless RNDIS device",
2569 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT,
2570 .bind = rndis_wext_bind,
2571 .unbind = rndis_wext_unbind,
2572 .status = rndis_status,
2573 .rx_fixup = rndis_rx_fixup,
2574 .tx_fixup = rndis_tx_fixup,
2575 .reset = rndis_wext_reset,
2576 .early_init = bcm4320_early_init,
2577 .link_change = rndis_wext_link_change,
2580 /*-------------------------------------------------------------------------*/
2582 static const struct usb_device_id products [] = {
2583 #define RNDIS_MASTER_INTERFACE \
2584 .bInterfaceClass = USB_CLASS_COMM, \
2585 .bInterfaceSubClass = 2 /* ACM */, \
2586 .bInterfaceProtocol = 0x0ff
2588 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2589 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2592 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2593 | USB_DEVICE_ID_MATCH_DEVICE,
2595 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2596 RNDIS_MASTER_INTERFACE,
2597 .driver_info = (unsigned long) &bcm4320b_info,
2599 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2600 | USB_DEVICE_ID_MATCH_DEVICE,
2602 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2603 RNDIS_MASTER_INTERFACE,
2604 .driver_info = (unsigned long) &bcm4320b_info,
2606 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2607 | USB_DEVICE_ID_MATCH_DEVICE,
2609 .idProduct = 0x011b, /* Belkin F5D7051 */
2610 RNDIS_MASTER_INTERFACE,
2611 .driver_info = (unsigned long) &bcm4320b_info,
2613 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2614 | USB_DEVICE_ID_MATCH_DEVICE,
2615 .idVendor = 0x1799, /* Belkin has two vendor ids */
2616 .idProduct = 0x011b, /* Belkin F5D7051 */
2617 RNDIS_MASTER_INTERFACE,
2618 .driver_info = (unsigned long) &bcm4320b_info,
2620 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2621 | USB_DEVICE_ID_MATCH_DEVICE,
2623 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
2624 RNDIS_MASTER_INTERFACE,
2625 .driver_info = (unsigned long) &bcm4320b_info,
2627 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2628 | USB_DEVICE_ID_MATCH_DEVICE,
2630 .idProduct = 0x0026, /* Linksys WUSB54GSC */
2631 RNDIS_MASTER_INTERFACE,
2632 .driver_info = (unsigned long) &bcm4320b_info,
2634 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2635 | USB_DEVICE_ID_MATCH_DEVICE,
2637 .idProduct = 0x1717, /* Asus WL169gE */
2638 RNDIS_MASTER_INTERFACE,
2639 .driver_info = (unsigned long) &bcm4320b_info,
2641 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2642 | USB_DEVICE_ID_MATCH_DEVICE,
2644 .idProduct = 0xd11b, /* Eminent EM4045 */
2645 RNDIS_MASTER_INTERFACE,
2646 .driver_info = (unsigned long) &bcm4320b_info,
2648 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2649 | USB_DEVICE_ID_MATCH_DEVICE,
2651 .idProduct = 0x0715, /* BT Voyager 1055 */
2652 RNDIS_MASTER_INTERFACE,
2653 .driver_info = (unsigned long) &bcm4320b_info,
2655 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
2656 * parameters available, hardware probably contain older firmware version with
2657 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
2660 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2661 | USB_DEVICE_ID_MATCH_DEVICE,
2663 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
2664 RNDIS_MASTER_INTERFACE,
2665 .driver_info = (unsigned long) &bcm4320a_info,
2667 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2668 | USB_DEVICE_ID_MATCH_DEVICE,
2670 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
2671 RNDIS_MASTER_INTERFACE,
2672 .driver_info = (unsigned long) &bcm4320a_info,
2674 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2675 | USB_DEVICE_ID_MATCH_DEVICE,
2677 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
2678 RNDIS_MASTER_INTERFACE,
2679 .driver_info = (unsigned long) &bcm4320a_info,
2681 /* Generic Wireless RNDIS devices that we don't have exact
2682 * idVendor/idProduct/chip yet.
2685 /* RNDIS is MSFT's un-official variant of CDC ACM */
2686 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2687 .driver_info = (unsigned long) &rndis_wext_info,
2689 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
2690 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
2691 .driver_info = (unsigned long) &rndis_wext_info,
2695 MODULE_DEVICE_TABLE(usb, products);
2697 static struct usb_driver rndis_wlan_driver = {
2698 .name = "rndis_wlan",
2699 .id_table = products,
2700 .probe = usbnet_probe,
2701 .disconnect = usbnet_disconnect,
2702 .suspend = usbnet_suspend,
2703 .resume = usbnet_resume,
2706 static int __init rndis_wlan_init(void)
2708 return usb_register(&rndis_wlan_driver);
2710 module_init(rndis_wlan_init);
2712 static void __exit rndis_wlan_exit(void)
2714 usb_deregister(&rndis_wlan_driver);
2716 module_exit(rndis_wlan_exit);
2718 MODULE_AUTHOR("Bjorge Dijkstra");
2719 MODULE_AUTHOR("Jussi Kivilinna");
2720 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
2721 MODULE_LICENSE("GPL");