2 * Driver for RNDIS based wireless USB devices.
4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
5 * Copyright (C) 2008-2009 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/cfg80211.h>
46 #include <linux/usb/usbnet.h>
47 #include <linux/usb/rndis_host.h>
50 /* NOTE: All these are settings for Broadcom chipset */
51 static char modparam_country[4] = "EU";
52 module_param_string(country, modparam_country, 4, 0444);
53 MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
55 static int modparam_frameburst = 1;
56 module_param_named(frameburst, modparam_frameburst, int, 0444);
57 MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
59 static int modparam_afterburner = 0;
60 module_param_named(afterburner, modparam_afterburner, int, 0444);
61 MODULE_PARM_DESC(afterburner,
62 "enable afterburner aka '125 High Speed Mode' (default: off)");
64 static int modparam_power_save = 0;
65 module_param_named(power_save, modparam_power_save, int, 0444);
66 MODULE_PARM_DESC(power_save,
67 "set power save mode: 0=off, 1=on, 2=fast (default: off)");
69 static int modparam_power_output = 3;
70 module_param_named(power_output, modparam_power_output, int, 0444);
71 MODULE_PARM_DESC(power_output,
72 "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
74 static int modparam_roamtrigger = -70;
75 module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
76 MODULE_PARM_DESC(roamtrigger,
77 "set roaming dBm trigger: -80=optimize for distance, "
78 "-60=bandwidth (default: -70)");
80 static int modparam_roamdelta = 1;
81 module_param_named(roamdelta, modparam_roamdelta, int, 0444);
82 MODULE_PARM_DESC(roamdelta,
83 "set roaming tendency: 0=aggressive, 1=moderate, "
84 "2=conservative (default: moderate)");
86 static int modparam_workaround_interval = 500;
87 module_param_named(workaround_interval, modparam_workaround_interval,
89 MODULE_PARM_DESC(workaround_interval,
90 "set stall workaround interval in msecs (default: 500)");
93 /* various RNDIS OID defs */
94 #define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
95 #define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
97 #define OID_GEN_XMIT_OK cpu_to_le32(0x00020101)
98 #define OID_GEN_RCV_OK cpu_to_le32(0x00020102)
99 #define OID_GEN_XMIT_ERROR cpu_to_le32(0x00020103)
100 #define OID_GEN_RCV_ERROR cpu_to_le32(0x00020104)
101 #define OID_GEN_RCV_NO_BUFFER cpu_to_le32(0x00020105)
103 #define OID_802_3_CURRENT_ADDRESS cpu_to_le32(0x01010102)
104 #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
105 #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
107 #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
108 #define OID_802_11_SSID cpu_to_le32(0x0d010102)
109 #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
110 #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
111 #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
112 #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
113 #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
114 #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
115 #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
116 #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
117 #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
118 #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
119 #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
120 #define OID_802_11_PMKID cpu_to_le32(0x0d010123)
121 #define OID_802_11_NETWORK_TYPES_SUPPORTED cpu_to_le32(0x0d010203)
122 #define OID_802_11_NETWORK_TYPE_IN_USE cpu_to_le32(0x0d010204)
123 #define OID_802_11_TX_POWER_LEVEL cpu_to_le32(0x0d010205)
124 #define OID_802_11_RSSI cpu_to_le32(0x0d010206)
125 #define OID_802_11_RSSI_TRIGGER cpu_to_le32(0x0d010207)
126 #define OID_802_11_FRAGMENTATION_THRESHOLD cpu_to_le32(0x0d010209)
127 #define OID_802_11_RTS_THRESHOLD cpu_to_le32(0x0d01020a)
128 #define OID_802_11_SUPPORTED_RATES cpu_to_le32(0x0d01020e)
129 #define OID_802_11_CONFIGURATION cpu_to_le32(0x0d010211)
130 #define OID_802_11_BSSID_LIST cpu_to_le32(0x0d010217)
133 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
134 #define WL_NOISE -96 /* typical noise level in dBm */
135 #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
138 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
139 * based on wireless rndis) has default txpower of 13dBm.
140 * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
141 * 100% : 20 mW ~ 13dBm
142 * 75% : 15 mW ~ 12dBm
143 * 50% : 10 mW ~ 10dBm
146 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
147 #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
148 #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
149 #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
152 /* codes for "status" field of completion messages */
153 #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
154 #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
157 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
158 * slightly modified for datatype endianess, etc
160 #define NDIS_802_11_LENGTH_SSID 32
161 #define NDIS_802_11_LENGTH_RATES 8
162 #define NDIS_802_11_LENGTH_RATES_EX 16
164 enum ndis_80211_net_type {
165 NDIS_80211_TYPE_FREQ_HOP,
166 NDIS_80211_TYPE_DIRECT_SEQ,
167 NDIS_80211_TYPE_OFDM_A,
168 NDIS_80211_TYPE_OFDM_G
171 enum ndis_80211_net_infra {
172 NDIS_80211_INFRA_ADHOC,
173 NDIS_80211_INFRA_INFRA,
174 NDIS_80211_INFRA_AUTO_UNKNOWN
177 enum ndis_80211_auth_mode {
178 NDIS_80211_AUTH_OPEN,
179 NDIS_80211_AUTH_SHARED,
180 NDIS_80211_AUTH_AUTO_SWITCH,
182 NDIS_80211_AUTH_WPA_PSK,
183 NDIS_80211_AUTH_WPA_NONE,
184 NDIS_80211_AUTH_WPA2,
185 NDIS_80211_AUTH_WPA2_PSK
188 enum ndis_80211_encr_status {
189 NDIS_80211_ENCR_WEP_ENABLED,
190 NDIS_80211_ENCR_DISABLED,
191 NDIS_80211_ENCR_WEP_KEY_ABSENT,
192 NDIS_80211_ENCR_NOT_SUPPORTED,
193 NDIS_80211_ENCR_TKIP_ENABLED,
194 NDIS_80211_ENCR_TKIP_KEY_ABSENT,
195 NDIS_80211_ENCR_CCMP_ENABLED,
196 NDIS_80211_ENCR_CCMP_KEY_ABSENT
199 enum ndis_80211_priv_filter {
200 NDIS_80211_PRIV_ACCEPT_ALL,
201 NDIS_80211_PRIV_8021X_WEP
204 enum ndis_80211_status_type {
205 NDIS_80211_STATUSTYPE_AUTHENTICATION,
206 NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
207 NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
208 NDIS_80211_STATUSTYPE_RADIOSTATE,
211 enum ndis_80211_media_stream_mode {
212 NDIS_80211_MEDIA_STREAM_OFF,
213 NDIS_80211_MEDIA_STREAM_ON
216 enum ndis_80211_radio_status {
217 NDIS_80211_RADIO_STATUS_ON,
218 NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
219 NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
222 enum ndis_80211_addkey_bits {
223 NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
224 NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
225 NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
226 NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
229 enum ndis_80211_addwep_bits {
230 NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
231 NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
234 struct ndis_80211_auth_request {
239 } __attribute__((packed));
241 struct ndis_80211_pmkid_candidate {
245 } __attribute__((packed));
247 struct ndis_80211_pmkid_cand_list {
249 __le32 num_candidates;
250 struct ndis_80211_pmkid_candidate candidate_list[0];
251 } __attribute__((packed));
253 struct ndis_80211_status_indication {
256 enum ndis_80211_media_stream_mode media_stream_mode;
257 enum ndis_80211_radio_status radio_status;
258 struct ndis_80211_auth_request auth_request[0];
259 struct ndis_80211_pmkid_cand_list cand_list;
261 } __attribute__((packed));
263 struct ndis_80211_ssid {
265 u8 essid[NDIS_802_11_LENGTH_SSID];
266 } __attribute__((packed));
268 struct ndis_80211_conf_freq_hop {
273 } __attribute__((packed));
275 struct ndis_80211_conf {
277 __le32 beacon_period;
280 struct ndis_80211_conf_freq_hop fh_config;
281 } __attribute__((packed));
283 struct ndis_80211_bssid_ex {
287 struct ndis_80211_ssid ssid;
291 struct ndis_80211_conf config;
293 u8 rates[NDIS_802_11_LENGTH_RATES_EX];
296 } __attribute__((packed));
298 struct ndis_80211_bssid_list_ex {
300 struct ndis_80211_bssid_ex bssid[0];
301 } __attribute__((packed));
303 struct ndis_80211_fixed_ies {
305 __le16 beacon_interval;
307 } __attribute__((packed));
309 struct ndis_80211_wep_key {
314 } __attribute__((packed));
316 struct ndis_80211_key {
324 } __attribute__((packed));
326 struct ndis_80211_remove_key {
331 } __attribute__((packed));
333 struct ndis_config_param {
339 } __attribute__((packed));
341 struct ndis_80211_assoc_info {
346 __le16 listen_interval;
347 u8 cur_ap_address[6];
349 __le32 req_ie_length;
350 __le32 offset_req_ies;
357 __le32 resp_ie_length;
358 __le32 offset_resp_ies;
359 } __attribute__((packed));
364 #define NET_TYPE_11FB 0
366 #define CAP_MODE_80211A 1
367 #define CAP_MODE_80211B 2
368 #define CAP_MODE_80211G 4
369 #define CAP_MODE_MASK 7
371 #define WORK_LINK_UP (1<<0)
372 #define WORK_LINK_DOWN (1<<1)
373 #define WORK_SET_MULTICAST_LIST (1<<2)
375 #define RNDIS_WLAN_ALG_NONE 0
376 #define RNDIS_WLAN_ALG_WEP (1<<0)
377 #define RNDIS_WLAN_ALG_TKIP (1<<1)
378 #define RNDIS_WLAN_ALG_CCMP (1<<2)
380 #define RNDIS_WLAN_KEY_MGMT_NONE 0
381 #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
382 #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
384 #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
386 static const struct ieee80211_channel rndis_channels[] = {
387 { .center_freq = 2412 },
388 { .center_freq = 2417 },
389 { .center_freq = 2422 },
390 { .center_freq = 2427 },
391 { .center_freq = 2432 },
392 { .center_freq = 2437 },
393 { .center_freq = 2442 },
394 { .center_freq = 2447 },
395 { .center_freq = 2452 },
396 { .center_freq = 2457 },
397 { .center_freq = 2462 },
398 { .center_freq = 2467 },
399 { .center_freq = 2472 },
400 { .center_freq = 2484 },
403 static const struct ieee80211_rate rndis_rates[] = {
405 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
406 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
407 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
418 static const u32 rndis_cipher_suites[] = {
419 WLAN_CIPHER_SUITE_WEP40,
420 WLAN_CIPHER_SUITE_WEP104,
421 WLAN_CIPHER_SUITE_TKIP,
422 WLAN_CIPHER_SUITE_CCMP,
425 struct rndis_wlan_encr_key {
434 /* RNDIS device private data */
435 struct rndis_wlan_private {
436 struct usbnet *usbdev;
438 struct wireless_dev wdev;
440 struct cfg80211_scan_request *scan_request;
442 struct workqueue_struct *workqueue;
443 struct delayed_work stats_work;
444 struct delayed_work scan_work;
445 struct work_struct work;
446 struct mutex command_lock;
447 unsigned long work_pending;
450 struct ieee80211_supported_band band;
451 struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
452 struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
453 u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
458 /* module parameters */
459 char param_country[4];
460 int param_frameburst;
461 int param_afterburner;
462 int param_power_save;
463 int param_power_output;
464 int param_roamtrigger;
466 u32 param_workaround_interval;
473 struct ndis_80211_ssid essid;
474 __le32 current_command_oid;
476 /* encryption stuff */
477 int encr_tx_key_index;
478 struct rndis_wlan_encr_key encr_keys[4];
479 enum nl80211_auth_type wpa_auth_type;
485 int wpa_cipher_group;
487 u8 command_buffer[COMMAND_BUFFER_SIZE];
493 static int rndis_change_virtual_intf(struct wiphy *wiphy,
494 struct net_device *dev,
495 enum nl80211_iftype type, u32 *flags,
496 struct vif_params *params);
498 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
499 struct cfg80211_scan_request *request);
501 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
503 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
505 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
507 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
508 struct cfg80211_connect_params *sme);
510 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
513 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
514 struct cfg80211_ibss_params *params);
516 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
518 static int rndis_set_channel(struct wiphy *wiphy,
519 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
521 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
522 u8 key_index, const u8 *mac_addr,
523 struct key_params *params);
525 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
526 u8 key_index, const u8 *mac_addr);
528 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
531 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
532 u8 *mac, struct station_info *sinfo);
534 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
535 int idx, u8 *mac, struct station_info *sinfo);
537 static struct cfg80211_ops rndis_config_ops = {
538 .change_virtual_intf = rndis_change_virtual_intf,
540 .set_wiphy_params = rndis_set_wiphy_params,
541 .set_tx_power = rndis_set_tx_power,
542 .get_tx_power = rndis_get_tx_power,
543 .connect = rndis_connect,
544 .disconnect = rndis_disconnect,
545 .join_ibss = rndis_join_ibss,
546 .leave_ibss = rndis_leave_ibss,
547 .set_channel = rndis_set_channel,
548 .add_key = rndis_add_key,
549 .del_key = rndis_del_key,
550 .set_default_key = rndis_set_default_key,
551 .get_station = rndis_get_station,
552 .dump_station = rndis_dump_station,
555 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
558 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
560 return (struct rndis_wlan_private *)dev->driver_priv;
564 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
566 switch (priv->param_power_output) {
569 return BCM4320_DEFAULT_TXPOWER_DBM_100;
571 return BCM4320_DEFAULT_TXPOWER_DBM_75;
573 return BCM4320_DEFAULT_TXPOWER_DBM_50;
575 return BCM4320_DEFAULT_TXPOWER_DBM_25;
580 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
582 int cipher = priv->encr_keys[idx].cipher;
584 return (cipher == WLAN_CIPHER_SUITE_CCMP ||
585 cipher == WLAN_CIPHER_SUITE_TKIP);
589 static int rndis_cipher_to_alg(u32 cipher)
593 return RNDIS_WLAN_ALG_NONE;
594 case WLAN_CIPHER_SUITE_WEP40:
595 case WLAN_CIPHER_SUITE_WEP104:
596 return RNDIS_WLAN_ALG_WEP;
597 case WLAN_CIPHER_SUITE_TKIP:
598 return RNDIS_WLAN_ALG_TKIP;
599 case WLAN_CIPHER_SUITE_CCMP:
600 return RNDIS_WLAN_ALG_CCMP;
604 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
608 return RNDIS_WLAN_KEY_MGMT_NONE;
609 case WLAN_AKM_SUITE_8021X:
610 return RNDIS_WLAN_KEY_MGMT_802_1X;
611 case WLAN_AKM_SUITE_PSK:
612 return RNDIS_WLAN_KEY_MGMT_PSK;
618 static const char *oid_to_string(__le32 oid)
621 #define OID_STR(oid) case oid: return(#oid)
622 /* from rndis_host.h */
623 OID_STR(OID_802_3_PERMANENT_ADDRESS);
624 OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
625 OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
626 OID_STR(OID_GEN_PHYSICAL_MEDIUM);
628 /* from rndis_wlan.c */
629 OID_STR(OID_GEN_LINK_SPEED);
630 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
632 OID_STR(OID_GEN_XMIT_OK);
633 OID_STR(OID_GEN_RCV_OK);
634 OID_STR(OID_GEN_XMIT_ERROR);
635 OID_STR(OID_GEN_RCV_ERROR);
636 OID_STR(OID_GEN_RCV_NO_BUFFER);
638 OID_STR(OID_802_3_CURRENT_ADDRESS);
639 OID_STR(OID_802_3_MULTICAST_LIST);
640 OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
642 OID_STR(OID_802_11_BSSID);
643 OID_STR(OID_802_11_SSID);
644 OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
645 OID_STR(OID_802_11_ADD_WEP);
646 OID_STR(OID_802_11_REMOVE_WEP);
647 OID_STR(OID_802_11_DISASSOCIATE);
648 OID_STR(OID_802_11_AUTHENTICATION_MODE);
649 OID_STR(OID_802_11_PRIVACY_FILTER);
650 OID_STR(OID_802_11_BSSID_LIST_SCAN);
651 OID_STR(OID_802_11_ENCRYPTION_STATUS);
652 OID_STR(OID_802_11_ADD_KEY);
653 OID_STR(OID_802_11_REMOVE_KEY);
654 OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
655 OID_STR(OID_802_11_PMKID);
656 OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
657 OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
658 OID_STR(OID_802_11_TX_POWER_LEVEL);
659 OID_STR(OID_802_11_RSSI);
660 OID_STR(OID_802_11_RSSI_TRIGGER);
661 OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
662 OID_STR(OID_802_11_RTS_THRESHOLD);
663 OID_STR(OID_802_11_SUPPORTED_RATES);
664 OID_STR(OID_802_11_CONFIGURATION);
665 OID_STR(OID_802_11_BSSID_LIST);
672 static const char *oid_to_string(__le32 oid)
679 /* translate error code */
680 static int rndis_error_status(__le32 rndis_status)
683 switch (rndis_status) {
684 case RNDIS_STATUS_SUCCESS:
687 case RNDIS_STATUS_FAILURE:
688 case RNDIS_STATUS_INVALID_DATA:
691 case RNDIS_STATUS_NOT_SUPPORTED:
694 case RNDIS_STATUS_ADAPTER_NOT_READY:
695 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
703 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
705 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
708 struct rndis_msg_hdr *header;
709 struct rndis_query *get;
710 struct rndis_query_c *get_c;
714 buflen = *len + sizeof(*u.get);
715 if (buflen < CONTROL_BUFFER_SIZE)
716 buflen = CONTROL_BUFFER_SIZE;
718 if (buflen > COMMAND_BUFFER_SIZE) {
719 u.buf = kmalloc(buflen, GFP_KERNEL);
723 u.buf = priv->command_buffer;
726 mutex_lock(&priv->command_lock);
728 memset(u.get, 0, sizeof *u.get);
729 u.get->msg_type = RNDIS_MSG_QUERY;
730 u.get->msg_len = cpu_to_le32(sizeof *u.get);
733 priv->current_command_oid = oid;
734 ret = rndis_command(dev, u.header, buflen);
735 priv->current_command_oid = 0;
737 devdbg(dev, "rndis_query_oid(%s): rndis_command() failed, %d "
738 "(%08x)", oid_to_string(oid), ret,
739 le32_to_cpu(u.get_c->status));
742 ret = le32_to_cpu(u.get_c->len);
745 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
746 ret = rndis_error_status(u.get_c->status);
749 devdbg(dev, "rndis_query_oid(%s): device returned "
750 "error, 0x%08x (%d)", oid_to_string(oid),
751 le32_to_cpu(u.get_c->status), ret);
754 mutex_unlock(&priv->command_lock);
756 if (u.buf != priv->command_buffer)
762 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
764 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
767 struct rndis_msg_hdr *header;
768 struct rndis_set *set;
769 struct rndis_set_c *set_c;
773 buflen = len + sizeof(*u.set);
774 if (buflen < CONTROL_BUFFER_SIZE)
775 buflen = CONTROL_BUFFER_SIZE;
777 if (buflen > COMMAND_BUFFER_SIZE) {
778 u.buf = kmalloc(buflen, GFP_KERNEL);
782 u.buf = priv->command_buffer;
785 mutex_lock(&priv->command_lock);
787 memset(u.set, 0, sizeof *u.set);
788 u.set->msg_type = RNDIS_MSG_SET;
789 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
791 u.set->len = cpu_to_le32(len);
792 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
793 u.set->handle = cpu_to_le32(0);
794 memcpy(u.buf + sizeof(*u.set), data, len);
796 priv->current_command_oid = oid;
797 ret = rndis_command(dev, u.header, buflen);
798 priv->current_command_oid = 0;
800 devdbg(dev, "rndis_set_oid(%s): rndis_command() failed, %d "
801 "(%08x)", oid_to_string(oid), ret,
802 le32_to_cpu(u.set_c->status));
805 ret = rndis_error_status(u.set_c->status);
808 devdbg(dev, "rndis_set_oid(%s): device returned error, "
809 "0x%08x (%d)", oid_to_string(oid),
810 le32_to_cpu(u.set_c->status), ret);
813 mutex_unlock(&priv->command_lock);
815 if (u.buf != priv->command_buffer)
821 static int rndis_reset(struct usbnet *usbdev)
823 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
824 struct rndis_reset *reset;
827 mutex_lock(&priv->command_lock);
829 reset = (void *)priv->command_buffer;
830 memset(reset, 0, sizeof(*reset));
831 reset->msg_type = RNDIS_MSG_RESET;
832 reset->msg_len = cpu_to_le32(sizeof(*reset));
833 priv->current_command_oid = 0;
834 ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
836 mutex_unlock(&priv->command_lock);
845 * Specs say that we can only set config parameters only soon after device
847 * value_type: 0 = u32, 2 = unicode string
849 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
850 int value_type, void *value)
852 struct ndis_config_param *infobuf;
853 int value_len, info_len, param_len, ret, i;
858 value_len = sizeof(__le32);
859 else if (value_type == 2)
860 value_len = strlen(value) * sizeof(__le16);
864 param_len = strlen(param) * sizeof(__le16);
865 info_len = sizeof(*infobuf) + param_len + value_len;
870 infobuf = kmalloc(info_len, GFP_KERNEL);
876 /* extra 12 bytes are for padding (debug output) */
877 memset(infobuf, 0xCC, info_len + 12);
881 devdbg(dev, "setting config parameter: %s, value: %s",
884 devdbg(dev, "setting config parameter: %s, value: %d",
885 param, *(u32 *)value);
887 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
888 infobuf->name_length = cpu_to_le32(param_len);
889 infobuf->type = cpu_to_le32(value_type);
890 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
891 infobuf->value_length = cpu_to_le32(value_len);
893 /* simple string to unicode string conversion */
894 unibuf = (void *)infobuf + sizeof(*infobuf);
895 for (i = 0; i < param_len / sizeof(__le16); i++)
896 unibuf[i] = cpu_to_le16(param[i]);
898 if (value_type == 2) {
899 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
900 for (i = 0; i < value_len / sizeof(__le16); i++)
901 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
903 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
904 *dst_value = cpu_to_le32(*(u32 *)value);
908 devdbg(dev, "info buffer (len: %d):", info_len);
909 for (i = 0; i < info_len; i += 12) {
910 u32 *tmp = (u32 *)((u8 *)infobuf + i);
911 devdbg(dev, "%08X:%08X:%08X",
914 cpu_to_be32(tmp[2]));
918 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
921 devdbg(dev, "setting rndis config parameter failed, %d.", ret);
927 static int rndis_set_config_parameter_str(struct usbnet *dev,
928 char *param, char *value)
930 return(rndis_set_config_parameter(dev, param, 2, value));
933 /*static int rndis_set_config_parameter_u32(struct usbnet *dev,
934 char *param, u32 value)
936 return(rndis_set_config_parameter(dev, param, 0, &value));
941 * data conversion functions
943 static int level_to_qual(int level)
945 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
946 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
953 static int set_infra_mode(struct usbnet *usbdev, int mode);
954 static void restore_keys(struct usbnet *usbdev);
955 static int rndis_check_bssid_list(struct usbnet *usbdev);
957 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
959 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
962 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
964 devwarn(usbdev, "setting SSID failed (%08X)", ret);
968 memcpy(&priv->essid, ssid, sizeof(priv->essid));
970 devdbg(usbdev, "set_essid: radio_on = 1");
976 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
980 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
982 devwarn(usbdev, "setting BSSID[%pM] failed (%08X)", bssid, ret);
989 static int clear_bssid(struct usbnet *usbdev)
991 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
993 return set_bssid(usbdev, broadcast_mac);
996 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1001 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1004 memset(bssid, 0, ETH_ALEN);
1009 static int get_association_info(struct usbnet *usbdev,
1010 struct ndis_80211_assoc_info *info, int len)
1012 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1016 static bool is_associated(struct usbnet *usbdev)
1018 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1022 if (!priv->radio_on)
1025 ret = get_bssid(usbdev, bssid);
1027 return (ret == 0 && !is_zero_ether_addr(bssid));
1031 static int disassociate(struct usbnet *usbdev, int reset_ssid)
1033 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1034 struct ndis_80211_ssid ssid;
1037 if (priv->radio_on) {
1038 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1041 devdbg(usbdev, "disassociate: radio_on = 0");
1048 /* disassociate causes radio to be turned off; if reset_ssid
1049 * is given, set random ssid to enable radio */
1051 /* Set device to infrastructure mode so we don't get ad-hoc
1052 * 'media connect' indications with the random ssid.
1054 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1056 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1057 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1058 ssid.essid[0] = 0x1;
1059 ssid.essid[1] = 0xff;
1060 for (i = 2; i < sizeof(ssid.essid); i++)
1061 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1062 ret = set_essid(usbdev, &ssid);
1068 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1069 enum nl80211_auth_type auth_type, int keymgmt)
1071 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1075 devdbg(usbdev, "set_auth_mode: wpa_version=0x%x authalg=0x%x "
1076 "keymgmt=0x%x", wpa_version, auth_type, keymgmt);
1078 if (wpa_version & NL80211_WPA_VERSION_2) {
1079 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1080 auth_mode = NDIS_80211_AUTH_WPA2;
1082 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1083 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1084 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1085 auth_mode = NDIS_80211_AUTH_WPA;
1086 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1087 auth_mode = NDIS_80211_AUTH_WPA_PSK;
1089 auth_mode = NDIS_80211_AUTH_WPA_NONE;
1090 } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1091 auth_mode = NDIS_80211_AUTH_SHARED;
1092 else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1093 auth_mode = NDIS_80211_AUTH_OPEN;
1097 tmp = cpu_to_le32(auth_mode);
1098 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1101 devwarn(usbdev, "setting auth mode failed (%08X)", ret);
1105 priv->wpa_version = wpa_version;
1106 priv->wpa_auth_type = auth_type;
1107 priv->wpa_keymgmt = keymgmt;
1113 static int set_priv_filter(struct usbnet *usbdev)
1115 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1118 devdbg(usbdev, "set_priv_filter: wpa_version=0x%x", priv->wpa_version);
1120 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1121 priv->wpa_version & NL80211_WPA_VERSION_1)
1122 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1124 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1126 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1131 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1133 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1137 devdbg(usbdev, "set_encr_mode: cipher_pair=0x%x cipher_group=0x%x",
1138 pairwise, groupwise);
1140 if (pairwise & RNDIS_WLAN_ALG_CCMP)
1141 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1142 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1143 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1144 else if (pairwise & RNDIS_WLAN_ALG_WEP)
1145 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1146 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1147 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1148 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1149 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1151 encr_mode = NDIS_80211_ENCR_DISABLED;
1153 tmp = cpu_to_le32(encr_mode);
1154 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1157 devwarn(usbdev, "setting encr mode failed (%08X)", ret);
1161 priv->wpa_cipher_pair = pairwise;
1162 priv->wpa_cipher_group = groupwise;
1167 static int set_infra_mode(struct usbnet *usbdev, int mode)
1169 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1173 devdbg(usbdev, "set_infra_mode: infra_mode=0x%x", priv->infra_mode);
1175 tmp = cpu_to_le32(mode);
1176 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1179 devwarn(usbdev, "setting infra mode failed (%08X)", ret);
1183 /* NDIS drivers clear keys when infrastructure mode is
1184 * changed. But Linux tools assume otherwise. So set the
1186 restore_keys(usbdev);
1188 priv->infra_mode = mode;
1193 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1197 devdbg(usbdev, "set_rts_threshold %i", rts_threshold);
1199 if (rts_threshold < 0 || rts_threshold > 2347)
1200 rts_threshold = 2347;
1202 tmp = cpu_to_le32(rts_threshold);
1203 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1208 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1212 devdbg(usbdev, "set_frag_threshold %i", frag_threshold);
1214 if (frag_threshold < 256 || frag_threshold > 2346)
1215 frag_threshold = 2346;
1217 tmp = cpu_to_le32(frag_threshold);
1218 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1223 static void set_default_iw_params(struct usbnet *usbdev)
1225 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1226 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1227 RNDIS_WLAN_KEY_MGMT_NONE);
1228 set_priv_filter(usbdev);
1229 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1233 static int deauthenticate(struct usbnet *usbdev)
1237 ret = disassociate(usbdev, 1);
1238 set_default_iw_params(usbdev);
1243 static int set_channel(struct usbnet *usbdev, int channel)
1245 struct ndis_80211_conf config;
1246 unsigned int dsconfig;
1249 devdbg(usbdev, "set_channel(%d)", channel);
1251 /* this OID is valid only when not associated */
1252 if (is_associated(usbdev))
1255 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1257 len = sizeof(config);
1258 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1260 devdbg(usbdev, "set_channel: querying configuration failed");
1264 config.ds_config = cpu_to_le32(dsconfig);
1265 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1268 devdbg(usbdev, "set_channel: %d -> %d", channel, ret);
1274 /* index must be 0 - N, as per NDIS */
1275 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1278 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1279 struct ndis_80211_wep_key ndis_key;
1283 devdbg(usbdev, "add_wep_key(idx: %d, len: %d)", index, key_len);
1285 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1289 cipher = WLAN_CIPHER_SUITE_WEP40;
1291 cipher = WLAN_CIPHER_SUITE_WEP104;
1293 memset(&ndis_key, 0, sizeof(ndis_key));
1295 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1296 ndis_key.length = cpu_to_le32(key_len);
1297 ndis_key.index = cpu_to_le32(index);
1298 memcpy(&ndis_key.material, key, key_len);
1300 if (index == priv->encr_tx_key_index) {
1301 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1302 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1303 RNDIS_WLAN_ALG_NONE);
1305 devwarn(usbdev, "encryption couldn't be enabled (%08X)",
1309 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1312 devwarn(usbdev, "adding encryption key %d failed (%08X)",
1317 priv->encr_keys[index].len = key_len;
1318 priv->encr_keys[index].cipher = cipher;
1319 memcpy(&priv->encr_keys[index].material, key, key_len);
1320 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1326 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1327 int index, const u8 *addr, const u8 *rx_seq,
1328 int seq_len, u32 cipher, int flags)
1330 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1331 struct ndis_80211_key ndis_key;
1335 if (index < 0 || index >= 4) {
1336 devdbg(usbdev, "add_wpa_key: index out of range (%i)", index);
1339 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1340 devdbg(usbdev, "add_wpa_key: key length out of range (%i)",
1344 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1345 if (!rx_seq || seq_len <= 0) {
1346 devdbg(usbdev, "add_wpa_key: recv seq flag without"
1350 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1351 devdbg(usbdev, "add_wpa_key: too big recv seq buffer");
1356 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1357 !is_broadcast_ether_addr(addr);
1358 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1359 devdbg(usbdev, "add_wpa_key: pairwise but bssid invalid (%pM)",
1364 devdbg(usbdev, "add_wpa_key(%i): flags:%i%i%i", index,
1365 !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1366 !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1367 !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1369 memset(&ndis_key, 0, sizeof(ndis_key));
1371 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1372 sizeof(ndis_key.material) + key_len);
1373 ndis_key.length = cpu_to_le32(key_len);
1374 ndis_key.index = cpu_to_le32(index) | flags;
1376 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1377 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1378 * different order than NDIS spec, so swap the order here. */
1379 memcpy(ndis_key.material, key, 16);
1380 memcpy(ndis_key.material + 16, key + 24, 8);
1381 memcpy(ndis_key.material + 24, key + 16, 8);
1383 memcpy(ndis_key.material, key, key_len);
1385 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1386 memcpy(ndis_key.rsc, rx_seq, seq_len);
1388 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1390 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1393 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1394 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1396 get_bssid(usbdev, ndis_key.bssid);
1399 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1400 le32_to_cpu(ndis_key.size));
1401 devdbg(usbdev, "add_wpa_key: OID_802_11_ADD_KEY -> %08X", ret);
1405 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1406 priv->encr_keys[index].len = key_len;
1407 priv->encr_keys[index].cipher = cipher;
1408 memcpy(&priv->encr_keys[index].material, key, key_len);
1409 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1410 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1412 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1414 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1415 priv->encr_tx_key_index = index;
1421 static int restore_key(struct usbnet *usbdev, int key_idx)
1423 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1424 struct rndis_wlan_encr_key key;
1426 if (is_wpa_key(priv, key_idx))
1429 key = priv->encr_keys[key_idx];
1431 devdbg(usbdev, "restore_key: %i:%i", key_idx, key.len);
1436 return add_wep_key(usbdev, key.material, key.len, key_idx);
1440 static void restore_keys(struct usbnet *usbdev)
1444 for (i = 0; i < 4; i++)
1445 restore_key(usbdev, i);
1449 static void clear_key(struct rndis_wlan_private *priv, int idx)
1451 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1455 /* remove_key is for both wep and wpa */
1456 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1458 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1459 struct ndis_80211_remove_key remove_key;
1464 if (priv->encr_keys[index].len == 0)
1467 is_wpa = is_wpa_key(priv, index);
1469 devdbg(usbdev, "remove_key: %i:%s:%i", index, is_wpa ? "wpa" : "wep",
1470 priv->encr_keys[index].len);
1472 clear_key(priv, index);
1475 remove_key.size = cpu_to_le32(sizeof(remove_key));
1476 remove_key.index = cpu_to_le32(index);
1479 if (!is_broadcast_ether_addr(bssid))
1481 NDIS_80211_ADDKEY_PAIRWISE_KEY;
1482 memcpy(remove_key.bssid, bssid,
1483 sizeof(remove_key.bssid));
1485 memset(remove_key.bssid, 0xff,
1486 sizeof(remove_key.bssid));
1488 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1489 sizeof(remove_key));
1493 keyindex = cpu_to_le32(index);
1494 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1498 "removing encryption key %d failed (%08X)",
1504 /* if it is transmit key, disable encryption */
1505 if (index == priv->encr_tx_key_index)
1506 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1512 static void set_multicast_list(struct usbnet *usbdev)
1514 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1515 struct dev_mc_list *mclist;
1520 filter = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
1522 if (usbdev->net->flags & IFF_PROMISC) {
1523 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1524 RNDIS_PACKET_TYPE_ALL_LOCAL;
1525 } else if (usbdev->net->flags & IFF_ALLMULTI ||
1526 usbdev->net->mc_count > priv->multicast_size) {
1527 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1528 } else if (usbdev->net->mc_count > 0) {
1529 size = min(priv->multicast_size, usbdev->net->mc_count);
1530 buf = kmalloc(size * ETH_ALEN, GFP_KERNEL);
1533 "couldn't alloc %d bytes of memory",
1538 mclist = usbdev->net->mc_list;
1539 for (i = 0; i < size && mclist; mclist = mclist->next) {
1540 if (mclist->dmi_addrlen != ETH_ALEN)
1543 memcpy(buf + i * ETH_ALEN, mclist->dmi_addr, ETH_ALEN);
1547 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, buf,
1549 if (ret == 0 && i > 0)
1550 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1552 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1554 devdbg(usbdev, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d",
1555 i, priv->multicast_size, ret);
1560 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1563 devwarn(usbdev, "couldn't set packet filter: %08x",
1564 le32_to_cpu(filter));
1567 devdbg(usbdev, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d",
1568 le32_to_cpu(filter), ret);
1575 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1576 struct net_device *dev,
1577 enum nl80211_iftype type, u32 *flags,
1578 struct vif_params *params)
1580 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1581 struct usbnet *usbdev = priv->usbdev;
1585 case NL80211_IFTYPE_ADHOC:
1586 mode = NDIS_80211_INFRA_ADHOC;
1588 case NL80211_IFTYPE_STATION:
1589 mode = NDIS_80211_INFRA_INFRA;
1595 priv->wdev.iftype = type;
1597 return set_infra_mode(usbdev, mode);
1601 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1603 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1604 struct usbnet *usbdev = priv->usbdev;
1607 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1608 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1613 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1614 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1623 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
1626 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1627 struct usbnet *usbdev = priv->usbdev;
1629 devdbg(usbdev, "rndis_set_tx_power type:0x%x dbm:%i", type, dbm);
1631 /* Device doesn't support changing txpower after initialization, only
1632 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1635 if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
1636 if (!priv->radio_on)
1637 disassociate(usbdev, 1); /* turn on radio */
1646 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1648 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1649 struct usbnet *usbdev = priv->usbdev;
1651 *dbm = get_bcm4320_power_dbm(priv);
1653 devdbg(usbdev, "rndis_get_tx_power dbm:%i", *dbm);
1659 #define SCAN_DELAY_JIFFIES (6 * HZ)
1660 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1661 struct cfg80211_scan_request *request)
1663 struct usbnet *usbdev = netdev_priv(dev);
1664 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1668 devdbg(usbdev, "cfg80211.scan");
1670 /* Get current bssid list from device before new scan, as new scan
1671 * clears internal bssid list.
1673 rndis_check_bssid_list(usbdev);
1678 if (priv->scan_request && priv->scan_request != request)
1681 priv->scan_request = request;
1683 tmp = cpu_to_le32(1);
1684 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1687 /* Wait before retrieving scan results from device */
1688 queue_delayed_work(priv->workqueue, &priv->scan_work,
1689 SCAN_DELAY_JIFFIES);
1696 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1697 struct ndis_80211_bssid_ex *bssid)
1699 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1700 struct ieee80211_channel *channel;
1704 u16 beacon_interval;
1705 struct ndis_80211_fixed_ies *fixed;
1706 int ie_len, bssid_len;
1709 devdbg(usbdev, " found bssid: '%.32s' [%pM]", bssid->ssid.essid,
1712 /* parse bssid structure */
1713 bssid_len = le32_to_cpu(bssid->length);
1715 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1716 sizeof(struct ndis_80211_fixed_ies))
1719 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1721 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1722 ie_len = min(bssid_len - (int)sizeof(*bssid),
1723 (int)le32_to_cpu(bssid->ie_length));
1724 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1728 /* extract data for cfg80211_inform_bss */
1729 channel = ieee80211_get_channel(priv->wdev.wiphy,
1730 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1734 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1735 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1736 capability = le16_to_cpu(fixed->capabilities);
1737 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1739 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1740 timestamp, capability, beacon_interval, ie, ie_len, signal,
1745 static int rndis_check_bssid_list(struct usbnet *usbdev)
1748 struct ndis_80211_bssid_list_ex *bssid_list;
1749 struct ndis_80211_bssid_ex *bssid;
1750 int ret = -EINVAL, len, count, bssid_len;
1751 bool resized = false;
1753 devdbg(usbdev, "check_bssid_list");
1755 len = CONTROL_BUFFER_SIZE;
1757 buf = kmalloc(len, GFP_KERNEL);
1763 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1767 if (!resized && len > CONTROL_BUFFER_SIZE) {
1774 bssid = bssid_list->bssid;
1775 bssid_len = le32_to_cpu(bssid->length);
1776 count = le32_to_cpu(bssid_list->num_items);
1777 devdbg(usbdev, "check_bssid_list: %d BSSIDs found (buflen: %d)", count,
1780 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1781 rndis_bss_info_update(usbdev, bssid);
1783 bssid = (void *)bssid + bssid_len;
1784 bssid_len = le32_to_cpu(bssid->length);
1794 static void rndis_get_scan_results(struct work_struct *work)
1796 struct rndis_wlan_private *priv =
1797 container_of(work, struct rndis_wlan_private, scan_work.work);
1798 struct usbnet *usbdev = priv->usbdev;
1801 devdbg(usbdev, "get_scan_results");
1803 if (!priv->scan_request)
1806 ret = rndis_check_bssid_list(usbdev);
1808 cfg80211_scan_done(priv->scan_request, ret < 0);
1810 priv->scan_request = NULL;
1813 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
1814 struct cfg80211_connect_params *sme)
1816 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1817 struct usbnet *usbdev = priv->usbdev;
1818 struct ieee80211_channel *channel = sme->channel;
1819 struct ndis_80211_ssid ssid;
1820 int pairwise = RNDIS_WLAN_ALG_NONE;
1821 int groupwise = RNDIS_WLAN_ALG_NONE;
1822 int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
1823 int length, i, ret, chan = -1;
1826 chan = ieee80211_frequency_to_channel(channel->center_freq);
1828 groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
1829 for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
1831 rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
1833 if (sme->crypto.n_ciphers_pairwise > 0 &&
1834 pairwise == RNDIS_WLAN_ALG_NONE) {
1835 deverr(usbdev, "Unsupported pairwise cipher");
1839 for (i = 0; i < sme->crypto.n_akm_suites; i++)
1841 rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
1843 if (sme->crypto.n_akm_suites > 0 &&
1844 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
1845 deverr(usbdev, "Invalid keymgmt");
1849 devdbg(usbdev, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:"
1850 "0x%x]:0x%x)", sme->ssid, sme->bssid, chan,
1851 sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
1852 groupwise, pairwise, keymgmt);
1854 if (is_associated(usbdev))
1855 disassociate(usbdev, false);
1857 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1859 devdbg(usbdev, "connect: set_infra_mode failed, %d", ret);
1860 goto err_turn_radio_on;
1863 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
1866 devdbg(usbdev, "connect: set_auth_mode failed, %d", ret);
1867 goto err_turn_radio_on;
1870 set_priv_filter(usbdev);
1872 ret = set_encr_mode(usbdev, pairwise, groupwise);
1874 devdbg(usbdev, "connect: set_encr_mode failed, %d", ret);
1875 goto err_turn_radio_on;
1879 ret = set_channel(usbdev, chan);
1881 devdbg(usbdev, "connect: set_channel failed, %d", ret);
1882 goto err_turn_radio_on;
1886 if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
1887 priv->encr_tx_key_index = sme->key_idx;
1888 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
1890 devdbg(usbdev, "connect: add_wep_key failed, %d "
1891 "(%d, %d)", ret, sme->key_len, sme->key_idx);
1892 goto err_turn_radio_on;
1896 if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
1897 !is_broadcast_ether_addr(sme->bssid)) {
1898 ret = set_bssid(usbdev, sme->bssid);
1900 devdbg(usbdev, "connect: set_bssid failed, %d", ret);
1901 goto err_turn_radio_on;
1904 clear_bssid(usbdev);
1906 length = sme->ssid_len;
1907 if (length > NDIS_802_11_LENGTH_SSID)
1908 length = NDIS_802_11_LENGTH_SSID;
1910 memset(&ssid, 0, sizeof(ssid));
1911 ssid.length = cpu_to_le32(length);
1912 memcpy(ssid.essid, sme->ssid, length);
1914 /* Pause and purge rx queue, so we don't pass packets before
1915 * 'media connect'-indication.
1917 usbnet_pause_rx(usbdev);
1918 usbnet_purge_paused_rxq(usbdev);
1920 ret = set_essid(usbdev, &ssid);
1922 devdbg(usbdev, "connect: set_essid failed, %d", ret);
1926 disassociate(usbdev, 1);
1931 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
1934 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1935 struct usbnet *usbdev = priv->usbdev;
1937 devdbg(usbdev, "cfg80211.disconnect(%d)", reason_code);
1939 priv->connected = false;
1940 memset(priv->bssid, 0, ETH_ALEN);
1942 return deauthenticate(usbdev);
1945 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1946 struct cfg80211_ibss_params *params)
1948 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1949 struct usbnet *usbdev = priv->usbdev;
1950 struct ieee80211_channel *channel = params->channel;
1951 struct ndis_80211_ssid ssid;
1952 enum nl80211_auth_type auth_type;
1953 int ret, alg, length, chan = -1;
1956 chan = ieee80211_frequency_to_channel(channel->center_freq);
1958 /* TODO: How to handle ad-hoc encryption?
1959 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
1960 * pre-shared for encryption (open/shared/wpa), is key set before
1961 * join_ibss? Which auth_type to use (not in params)? What about WPA?
1963 if (params->privacy) {
1964 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1965 alg = RNDIS_WLAN_ALG_WEP;
1967 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1968 alg = RNDIS_WLAN_ALG_NONE;
1971 devdbg(usbdev, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)", params->ssid,
1972 params->bssid, chan, params->privacy);
1974 if (is_associated(usbdev))
1975 disassociate(usbdev, false);
1977 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
1979 devdbg(usbdev, "join_ibss: set_infra_mode failed, %d", ret);
1980 goto err_turn_radio_on;
1983 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
1985 devdbg(usbdev, "join_ibss: set_auth_mode failed, %d", ret);
1986 goto err_turn_radio_on;
1989 set_priv_filter(usbdev);
1991 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
1993 devdbg(usbdev, "join_ibss: set_encr_mode failed, %d", ret);
1994 goto err_turn_radio_on;
1998 ret = set_channel(usbdev, chan);
2000 devdbg(usbdev, "join_ibss: set_channel failed, %d",
2002 goto err_turn_radio_on;
2006 if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2007 !is_broadcast_ether_addr(params->bssid)) {
2008 ret = set_bssid(usbdev, params->bssid);
2010 devdbg(usbdev, "join_ibss: set_bssid failed, %d", ret);
2011 goto err_turn_radio_on;
2014 clear_bssid(usbdev);
2016 length = params->ssid_len;
2017 if (length > NDIS_802_11_LENGTH_SSID)
2018 length = NDIS_802_11_LENGTH_SSID;
2020 memset(&ssid, 0, sizeof(ssid));
2021 ssid.length = cpu_to_le32(length);
2022 memcpy(ssid.essid, params->ssid, length);
2024 /* Don't need to pause rx queue for ad-hoc. */
2025 usbnet_purge_paused_rxq(usbdev);
2026 usbnet_resume_rx(usbdev);
2028 ret = set_essid(usbdev, &ssid);
2030 devdbg(usbdev, "join_ibss: set_essid failed, %d", ret);
2034 disassociate(usbdev, 1);
2039 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2041 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2042 struct usbnet *usbdev = priv->usbdev;
2044 devdbg(usbdev, "cfg80211.leave_ibss()");
2046 priv->connected = false;
2047 memset(priv->bssid, 0, ETH_ALEN);
2049 return deauthenticate(usbdev);
2052 static int rndis_set_channel(struct wiphy *wiphy,
2053 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2055 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2056 struct usbnet *usbdev = priv->usbdev;
2058 return set_channel(usbdev,
2059 ieee80211_frequency_to_channel(chan->center_freq));
2062 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2063 u8 key_index, const u8 *mac_addr,
2064 struct key_params *params)
2066 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2067 struct usbnet *usbdev = priv->usbdev;
2070 devdbg(usbdev, "rndis_add_key(%i, %pM, %08x)", key_index, mac_addr,
2073 switch (params->cipher) {
2074 case WLAN_CIPHER_SUITE_WEP40:
2075 case WLAN_CIPHER_SUITE_WEP104:
2076 return add_wep_key(usbdev, params->key, params->key_len,
2078 case WLAN_CIPHER_SUITE_TKIP:
2079 case WLAN_CIPHER_SUITE_CCMP:
2082 if (params->seq && params->seq_len > 0)
2083 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2085 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2086 NDIS_80211_ADDKEY_TRANSMIT_KEY;
2088 return add_wpa_key(usbdev, params->key, params->key_len,
2089 key_index, mac_addr, params->seq,
2090 params->seq_len, params->cipher, flags);
2092 devdbg(usbdev, "rndis_add_key: unsupported cipher %08x",
2098 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2099 u8 key_index, const u8 *mac_addr)
2101 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2102 struct usbnet *usbdev = priv->usbdev;
2104 devdbg(usbdev, "rndis_del_key(%i, %pM)", key_index, mac_addr);
2106 return remove_key(usbdev, key_index, mac_addr);
2109 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2112 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2113 struct usbnet *usbdev = priv->usbdev;
2114 struct rndis_wlan_encr_key key;
2116 devdbg(usbdev, "rndis_set_default_key(%i)", key_index);
2118 priv->encr_tx_key_index = key_index;
2120 key = priv->encr_keys[key_index];
2122 return add_wep_key(usbdev, key.material, key.len, key_index);
2125 static void rndis_fill_station_info(struct usbnet *usbdev,
2126 struct station_info *sinfo)
2128 __le32 linkspeed, rssi;
2131 memset(sinfo, 0, sizeof(*sinfo));
2133 len = sizeof(linkspeed);
2134 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2136 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2137 sinfo->filled |= STATION_INFO_TX_BITRATE;
2141 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2143 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2144 sinfo->filled |= STATION_INFO_SIGNAL;
2148 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2149 u8 *mac, struct station_info *sinfo)
2151 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2152 struct usbnet *usbdev = priv->usbdev;
2154 if (compare_ether_addr(priv->bssid, mac))
2157 rndis_fill_station_info(usbdev, sinfo);
2162 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2163 int idx, u8 *mac, struct station_info *sinfo)
2165 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2166 struct usbnet *usbdev = priv->usbdev;
2171 memcpy(mac, priv->bssid, ETH_ALEN);
2173 rndis_fill_station_info(usbdev, sinfo);
2179 * wireless extension handlers
2182 static int rndis_iw_commit(struct net_device *dev,
2183 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2190 /* Commented code out instead of removing to have more sane patch for review.
2191 * Will be removed later in the set.
2193 static int rndis_iw_set_essid(struct net_device *dev,
2194 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
2196 struct ndis_80211_ssid ssid;
2197 int length = wrqu->essid.length;
2198 struct usbnet *usbdev = netdev_priv(dev);
2200 devdbg(usbdev, "SIOCSIWESSID: [flags:%d,len:%d] '%.32s'",
2201 wrqu->essid.flags, wrqu->essid.length, essid);
2203 if (length > NDIS_802_11_LENGTH_SSID)
2204 length = NDIS_802_11_LENGTH_SSID;
2206 ssid.length = cpu_to_le32(length);
2208 memcpy(ssid.essid, essid, length);
2210 memset(ssid.essid, 0, NDIS_802_11_LENGTH_SSID);
2212 set_assoc_params(usbdev);
2214 if (!wrqu->essid.flags || length == 0)
2215 return disassociate(usbdev, 1);
2217 /* Pause and purge rx queue, so we don't pass packets before
2218 * 'media connect'-indication.
2220 usbnet_pause_rx(usbdev);
2221 usbnet_purge_paused_rxq(usbdev);
2223 return set_essid(usbdev, &ssid);
2228 static int rndis_iw_get_essid(struct net_device *dev,
2229 struct iw_request_info *info, union iwreq_data *wrqu, char *essid)
2231 struct ndis_80211_ssid ssid;
2232 struct usbnet *usbdev = netdev_priv(dev);
2235 ret = get_essid(usbdev, &ssid);
2237 if (ret == 0 && le32_to_cpu(ssid.length) > 0) {
2238 wrqu->essid.flags = 1;
2239 wrqu->essid.length = le32_to_cpu(ssid.length);
2240 memcpy(essid, ssid.essid, wrqu->essid.length);
2241 essid[wrqu->essid.length] = 0;
2243 memset(essid, 0, sizeof(NDIS_802_11_LENGTH_SSID));
2244 wrqu->essid.flags = 0;
2245 wrqu->essid.length = 0;
2247 devdbg(usbdev, "SIOCGIWESSID: %s", essid);
2252 static int rndis_iw_get_bssid(struct net_device *dev,
2253 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2255 struct usbnet *usbdev = netdev_priv(dev);
2256 unsigned char bssid[ETH_ALEN];
2259 ret = get_bssid(usbdev, bssid);
2262 devdbg(usbdev, "SIOCGIWAP: %pM", bssid);
2264 devdbg(usbdev, "SIOCGIWAP: <not associated>");
2266 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
2267 memcpy(wrqu->ap_addr.sa_data, bssid, ETH_ALEN);
2273 static int rndis_iw_set_bssid(struct net_device *dev,
2274 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2276 struct usbnet *usbdev = netdev_priv(dev);
2277 u8 *bssid = (u8 *)wrqu->ap_addr.sa_data;
2280 devdbg(usbdev, "SIOCSIWAP: %pM", bssid);
2282 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
2284 /* user apps may set ap's mac address, which is not required;
2285 * they may fail to work if this function fails, so return
2288 devwarn(usbdev, "setting AP mac address failed (%08X)", ret);
2294 static int rndis_iw_set_auth(struct net_device *dev,
2295 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2297 struct iw_param *p = &wrqu->param;
2298 struct usbnet *usbdev = netdev_priv(dev);
2299 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2300 int ret = -ENOTSUPP;
2302 switch (p->flags & IW_AUTH_INDEX) {
2303 case IW_AUTH_WPA_VERSION:
2304 devdbg(usbdev, "SIOCSIWAUTH: WPA_VERSION, %08x", p->value);
2305 priv->wpa_version = p->value;
2309 case IW_AUTH_CIPHER_PAIRWISE:
2310 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_PAIRWISE, %08x", p->value);
2311 priv->wpa_cipher_pair = p->value;
2315 case IW_AUTH_CIPHER_GROUP:
2316 devdbg(usbdev, "SIOCSIWAUTH: CIPHER_GROUP, %08x", p->value);
2317 priv->wpa_cipher_group = p->value;
2321 case IW_AUTH_KEY_MGMT:
2322 devdbg(usbdev, "SIOCSIWAUTH: KEY_MGMT, %08x", p->value);
2323 priv->wpa_keymgmt = p->value;
2327 case IW_AUTH_TKIP_COUNTERMEASURES:
2328 devdbg(usbdev, "SIOCSIWAUTH: TKIP_COUNTERMEASURES, %08x",
2333 case IW_AUTH_DROP_UNENCRYPTED:
2334 devdbg(usbdev, "SIOCSIWAUTH: DROP_UNENCRYPTED, %08x", p->value);
2338 case IW_AUTH_80211_AUTH_ALG:
2339 devdbg(usbdev, "SIOCSIWAUTH: 80211_AUTH_ALG, %08x", p->value);
2340 priv->wpa_authalg = p->value;
2344 case IW_AUTH_WPA_ENABLED:
2345 devdbg(usbdev, "SIOCSIWAUTH: WPA_ENABLED, %08x", p->value);
2346 if (wrqu->param.value)
2347 deauthenticate(usbdev);
2351 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
2352 devdbg(usbdev, "SIOCSIWAUTH: RX_UNENCRYPTED_EAPOL, %08x",
2357 case IW_AUTH_ROAMING_CONTROL:
2358 devdbg(usbdev, "SIOCSIWAUTH: ROAMING_CONTROL, %08x", p->value);
2362 case IW_AUTH_PRIVACY_INVOKED:
2363 devdbg(usbdev, "SIOCSIWAUTH: invalid cmd %d",
2364 wrqu->param.flags & IW_AUTH_INDEX);
2368 devdbg(usbdev, "SIOCSIWAUTH: UNKNOWN %08x, %08x",
2369 p->flags & IW_AUTH_INDEX, p->value);
2375 static int rndis_iw_get_auth(struct net_device *dev,
2376 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2378 struct iw_param *p = &wrqu->param;
2379 struct usbnet *usbdev = netdev_priv(dev);
2380 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2382 switch (p->flags & IW_AUTH_INDEX) {
2383 case IW_AUTH_WPA_VERSION:
2384 p->value = priv->wpa_version;
2386 case IW_AUTH_CIPHER_PAIRWISE:
2387 p->value = priv->wpa_cipher_pair;
2389 case IW_AUTH_CIPHER_GROUP:
2390 p->value = priv->wpa_cipher_group;
2392 case IW_AUTH_KEY_MGMT:
2393 p->value = priv->wpa_keymgmt;
2395 case IW_AUTH_80211_AUTH_ALG:
2396 p->value = priv->wpa_authalg;
2399 devdbg(usbdev, "SIOCGIWAUTH: invalid cmd %d",
2400 wrqu->param.flags & IW_AUTH_INDEX);
2407 static int rndis_iw_set_encode(struct net_device *dev,
2408 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2410 struct usbnet *usbdev = netdev_priv(dev);
2411 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2412 struct rndis_wlan_encr_key key;
2413 int ret, index, key_len;
2416 index = (wrqu->encoding.flags & IW_ENCODE_INDEX);
2418 /* iwconfig gives index as 1 - N */
2422 index = priv->encr_tx_key_index;
2424 if (index < 0 || index >= 4) {
2425 devwarn(usbdev, "encryption index out of range (%u)", index);
2429 /* remove key if disabled */
2430 if (wrqu->data.flags & IW_ENCODE_DISABLED) {
2431 if (remove_key(usbdev, index, NULL))
2437 /* global encryption state (for all keys) */
2438 if (wrqu->data.flags & IW_ENCODE_OPEN)
2439 ret = set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
2440 RNDIS_WLAN_KEY_MGMT_NONE);
2441 else /*if (wrqu->data.flags & IW_ENCODE_RESTRICTED)*/
2442 ret = set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_SHARED_KEY,
2443 RNDIS_WLAN_KEY_MGMT_NONE);
2447 if (wrqu->data.length > 0) {
2448 key_len = wrqu->data.length;
2451 /* must be set as tx key */
2452 if (priv->encr_keys[index].len == 0)
2454 key = priv->encr_keys[index];
2456 keybuf = key.material;
2457 priv->encr_tx_key_index = index;
2460 if (add_wep_key(usbdev, keybuf, key_len, index) != 0)
2463 if (index == priv->encr_tx_key_index)
2464 /* ndis drivers want essid to be set after setting encr */
2465 set_essid(usbdev, &priv->essid);
2471 static int rndis_iw_set_encode_ext(struct net_device *dev,
2472 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2474 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2475 struct usbnet *usbdev = netdev_priv(dev);
2476 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2477 int keyidx, flags, cipher;
2479 keyidx = wrqu->encoding.flags & IW_ENCODE_INDEX;
2481 /* iwconfig gives index as 1 - N */
2485 keyidx = priv->encr_tx_key_index;
2487 if (keyidx < 0 || keyidx >= 4) {
2488 devwarn(usbdev, "encryption index out of range (%u)", keyidx);
2492 if (ext->alg == IW_ENCODE_ALG_WEP) {
2493 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2494 priv->encr_tx_key_index = keyidx;
2495 return add_wep_key(usbdev, ext->key, ext->key_len, keyidx);
2499 if (ext->alg == IW_ENCODE_ALG_TKIP)
2500 cipher = WLAN_CIPHER_SUITE_TKIP;
2501 else if (ext->alg == IW_ENCODE_ALG_CCMP)
2502 cipher = WLAN_CIPHER_SUITE_CCMP;
2504 if ((wrqu->encoding.flags & IW_ENCODE_DISABLED) ||
2505 ext->alg == IW_ENCODE_ALG_NONE || ext->key_len == 0)
2506 return remove_key(usbdev, keyidx, NULL);
2512 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
2513 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2514 if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
2515 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY;
2516 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2517 flags |= NDIS_80211_ADDKEY_TRANSMIT_KEY;
2519 return add_wpa_key(usbdev, ext->key, ext->key_len, keyidx,
2520 (u8 *)&ext->addr.sa_data, ext->rx_seq, cipher,
2525 static int rndis_iw_get_rate(struct net_device *dev,
2526 struct iw_request_info *info, union iwreq_data *wrqu, char *extra)
2528 struct usbnet *usbdev = netdev_priv(dev);
2533 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &tmp, &len);
2535 wrqu->bitrate.value = le32_to_cpu(tmp) * 100;
2536 wrqu->bitrate.disabled = 0;
2537 wrqu->bitrate.flags = 1;
2543 static struct iw_statistics *rndis_get_wireless_stats(struct net_device *dev)
2545 struct usbnet *usbdev = netdev_priv(dev);
2546 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2547 unsigned long flags;
2549 spin_lock_irqsave(&priv->stats_lock, flags);
2550 memcpy(&priv->iwstats, &priv->privstats, sizeof(priv->iwstats));
2551 spin_unlock_irqrestore(&priv->stats_lock, flags);
2553 return &priv->iwstats;
2558 #define IW_IOCTL(x) [(x) - SIOCSIWCOMMIT]
2559 static const iw_handler rndis_iw_handler[] =
2561 IW_IOCTL(SIOCSIWCOMMIT) = rndis_iw_commit,
2562 IW_IOCTL(SIOCGIWNAME) = (iw_handler) cfg80211_wext_giwname,
2563 IW_IOCTL(SIOCSIWFREQ) = (iw_handler) cfg80211_wext_siwfreq,
2564 IW_IOCTL(SIOCGIWFREQ) = (iw_handler) cfg80211_wext_giwfreq,
2565 IW_IOCTL(SIOCSIWMODE) = (iw_handler) cfg80211_wext_siwmode,
2566 IW_IOCTL(SIOCGIWMODE) = (iw_handler) cfg80211_wext_giwmode,
2567 IW_IOCTL(SIOCGIWRANGE) = (iw_handler) cfg80211_wext_giwrange,
2568 IW_IOCTL(SIOCSIWAP) = (iw_handler) cfg80211_wext_siwap,
2569 IW_IOCTL(SIOCGIWAP) = (iw_handler) cfg80211_wext_giwap,
2570 IW_IOCTL(SIOCSIWSCAN) = (iw_handler) cfg80211_wext_siwscan,
2571 IW_IOCTL(SIOCGIWSCAN) = (iw_handler) cfg80211_wext_giwscan,
2572 IW_IOCTL(SIOCSIWESSID) = (iw_handler) cfg80211_wext_siwessid,
2573 IW_IOCTL(SIOCGIWESSID) = (iw_handler) cfg80211_wext_giwessid,
2574 IW_IOCTL(SIOCGIWRATE) = (iw_handler) cfg80211_wext_giwrate,
2575 IW_IOCTL(SIOCSIWRTS) = (iw_handler) cfg80211_wext_siwrts,
2576 IW_IOCTL(SIOCGIWRTS) = (iw_handler) cfg80211_wext_giwrts,
2577 IW_IOCTL(SIOCSIWFRAG) = (iw_handler) cfg80211_wext_siwfrag,
2578 IW_IOCTL(SIOCGIWFRAG) = (iw_handler) cfg80211_wext_giwfrag,
2579 IW_IOCTL(SIOCSIWTXPOW) = (iw_handler) cfg80211_wext_siwtxpower,
2580 IW_IOCTL(SIOCGIWTXPOW) = (iw_handler) cfg80211_wext_giwtxpower,
2581 IW_IOCTL(SIOCSIWENCODE) = (iw_handler) cfg80211_wext_siwencode,
2582 IW_IOCTL(SIOCSIWENCODEEXT) = (iw_handler) cfg80211_wext_siwencodeext,
2583 IW_IOCTL(SIOCSIWAUTH) = (iw_handler) cfg80211_wext_siwauth,
2584 IW_IOCTL(SIOCGIWAUTH) = (iw_handler) cfg80211_wext_giwauth,
2585 IW_IOCTL(SIOCSIWGENIE) = (iw_handler) cfg80211_wext_siwgenie,
2586 IW_IOCTL(SIOCSIWMLME) = (iw_handler) cfg80211_wext_siwmlme,
2589 static const iw_handler rndis_wlan_private_handler[] = {
2592 static const struct iw_priv_args rndis_wlan_private_args[] = {
2596 static const struct iw_handler_def rndis_iw_handlers = {
2597 .num_standard = ARRAY_SIZE(rndis_iw_handler),
2598 .num_private = ARRAY_SIZE(rndis_wlan_private_handler),
2599 .num_private_args = ARRAY_SIZE(rndis_wlan_private_args),
2600 .standard = (iw_handler *)rndis_iw_handler,
2601 .private = (iw_handler *)rndis_wlan_private_handler,
2602 .private_args = (struct iw_priv_args *)rndis_wlan_private_args,
2603 .get_wireless_stats = cfg80211_wireless_stats,
2607 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2609 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2610 struct ndis_80211_assoc_info *info;
2611 u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
2613 int resp_ie_len, req_ie_len;
2614 u8 *req_ie, *resp_ie;
2616 bool roamed = false;
2618 if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2619 /* received media connect indication while connected, either
2620 * device reassociated with same AP or roamed to new. */
2629 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2630 memset(assoc_buf, 0, sizeof(assoc_buf));
2631 info = (void *)assoc_buf;
2633 /* Get association info IEs from device and send them back to
2635 ret = get_association_info(usbdev, info, sizeof(assoc_buf));
2637 req_ie_len = le32_to_cpu(info->req_ie_length);
2638 if (req_ie_len > 0) {
2639 offset = le32_to_cpu(info->offset_req_ies);
2640 req_ie = (u8 *)info + offset;
2643 resp_ie_len = le32_to_cpu(info->resp_ie_length);
2644 if (resp_ie_len > 0) {
2645 offset = le32_to_cpu(info->offset_resp_ies);
2646 resp_ie = (u8 *)info + offset;
2649 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2652 ret = get_bssid(usbdev, bssid);
2654 memset(bssid, 0, sizeof(bssid));
2656 devdbg(usbdev, "link up work: [%pM] %s", bssid, roamed ? "roamed" : "");
2658 /* Internal bss list in device always contains at least the currently
2659 * connected bss and we can get it to cfg80211 with
2660 * rndis_check_bssid_list().
2661 * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2664 rndis_check_bssid_list(usbdev);
2666 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2668 cfg80211_connect_result(usbdev->net, bssid, req_ie,
2669 req_ie_len, resp_ie,
2670 resp_ie_len, 0, GFP_KERNEL);
2672 cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2673 resp_ie, resp_ie_len, GFP_KERNEL);
2674 } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2675 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2677 priv->connected = true;
2678 memcpy(priv->bssid, bssid, ETH_ALEN);
2680 usbnet_resume_rx(usbdev);
2681 netif_carrier_on(usbdev->net);
2684 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2686 union iwreq_data evt;
2688 netif_carrier_off(usbdev->net);
2691 evt.data.length = 0;
2692 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2693 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2696 static void rndis_wlan_worker(struct work_struct *work)
2698 struct rndis_wlan_private *priv =
2699 container_of(work, struct rndis_wlan_private, work);
2700 struct usbnet *usbdev = priv->usbdev;
2702 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2703 rndis_wlan_do_link_up_work(usbdev);
2705 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2706 rndis_wlan_do_link_down_work(usbdev);
2708 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2709 set_multicast_list(usbdev);
2712 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2714 struct usbnet *usbdev = netdev_priv(dev);
2715 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2717 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2720 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2721 queue_work(priv->workqueue, &priv->work);
2725 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2726 struct ndis_80211_status_indication *indication,
2732 bool pairwise_error, group_error;
2733 struct ndis_80211_auth_request *auth_req;
2735 /* must have at least one array entry */
2736 if (len < offsetof(struct ndis_80211_status_indication, u) +
2737 sizeof(struct ndis_80211_auth_request)) {
2738 devinfo(usbdev, "authentication indication: "
2739 "too short message (%i)", len);
2743 buf = (void *)&indication->u.auth_request[0];
2744 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2746 while (buflen >= sizeof(*auth_req)) {
2747 auth_req = (void *)buf;
2749 flags = le32_to_cpu(auth_req->flags);
2750 pairwise_error = false;
2751 group_error = false;
2754 type = "reauth request";
2756 type = "key update request";
2758 pairwise_error = true;
2759 type = "pairwise_error";
2763 type = "group_error";
2766 devinfo(usbdev, "authentication indication: %s (0x%08x)", type,
2767 le32_to_cpu(auth_req->flags));
2769 if (pairwise_error || group_error) {
2770 union iwreq_data wrqu;
2771 struct iw_michaelmicfailure micfailure;
2773 memset(&micfailure, 0, sizeof(micfailure));
2775 micfailure.flags |= IW_MICFAILURE_PAIRWISE;
2777 micfailure.flags |= IW_MICFAILURE_GROUP;
2779 memcpy(micfailure.src_addr.sa_data, auth_req->bssid,
2782 memset(&wrqu, 0, sizeof(wrqu));
2783 wrqu.data.length = sizeof(micfailure);
2784 wireless_send_event(usbdev->net, IWEVMICHAELMICFAILURE,
2785 &wrqu, (u8 *)&micfailure);
2788 buflen -= le32_to_cpu(auth_req->length);
2789 buf += le32_to_cpu(auth_req->length);
2793 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2794 struct ndis_80211_status_indication *indication,
2797 struct ndis_80211_pmkid_cand_list *cand_list;
2798 int list_len, expected_len, i;
2800 if (len < offsetof(struct ndis_80211_status_indication, u) +
2801 sizeof(struct ndis_80211_pmkid_cand_list)) {
2802 devinfo(usbdev, "pmkid candidate list indication: "
2803 "too short message (%i)", len);
2807 list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2808 sizeof(struct ndis_80211_pmkid_candidate);
2809 expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2810 offsetof(struct ndis_80211_status_indication, u);
2812 if (len < expected_len) {
2813 devinfo(usbdev, "pmkid candidate list indication: "
2814 "list larger than buffer (%i < %i)",
2819 cand_list = &indication->u.cand_list;
2821 devinfo(usbdev, "pmkid candidate list indication: "
2822 "version %i, candidates %i",
2823 le32_to_cpu(cand_list->version),
2824 le32_to_cpu(cand_list->num_candidates));
2826 if (le32_to_cpu(cand_list->version) != 1)
2829 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2830 struct iw_pmkid_cand pcand;
2831 union iwreq_data wrqu;
2832 struct ndis_80211_pmkid_candidate *cand =
2833 &cand_list->candidate_list[i];
2835 devdbg(usbdev, "cand[%i]: flags: 0x%08x, bssid: %pM",
2836 i, le32_to_cpu(cand->flags), cand->bssid);
2838 memset(&pcand, 0, sizeof(pcand));
2839 if (le32_to_cpu(cand->flags) & 0x01)
2840 pcand.flags |= IW_PMKID_CAND_PREAUTH;
2842 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2844 memset(&wrqu, 0, sizeof(wrqu));
2845 wrqu.data.length = sizeof(pcand);
2846 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2851 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2852 struct rndis_indicate *msg, int buflen)
2854 struct ndis_80211_status_indication *indication;
2857 offset = offsetof(struct rndis_indicate, status) +
2858 le32_to_cpu(msg->offset);
2859 len = le32_to_cpu(msg->length);
2862 devinfo(usbdev, "media specific indication, "
2863 "ignore too short message (%i < 8)", len);
2867 if (offset + len > buflen) {
2868 devinfo(usbdev, "media specific indication, "
2869 "too large to fit to buffer (%i > %i)",
2870 offset + len, buflen);
2874 indication = (void *)((u8 *)msg + offset);
2876 switch (le32_to_cpu(indication->status_type)) {
2877 case NDIS_80211_STATUSTYPE_RADIOSTATE:
2878 devinfo(usbdev, "radio state indication: %i",
2879 le32_to_cpu(indication->u.radio_status));
2882 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2883 devinfo(usbdev, "media stream mode indication: %i",
2884 le32_to_cpu(indication->u.media_stream_mode));
2887 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2888 rndis_wlan_auth_indication(usbdev, indication, len);
2891 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2892 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2896 devinfo(usbdev, "media specific indication: "
2897 "unknown status type 0x%08x",
2898 le32_to_cpu(indication->status_type));
2903 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2905 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2906 struct rndis_indicate *msg = ind;
2908 switch (msg->status) {
2909 case RNDIS_STATUS_MEDIA_CONNECT:
2910 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2911 /* OID_802_11_ADD_KEY causes sometimes extra
2912 * "media connect" indications which confuses driver
2913 * and userspace to think that device is
2914 * roaming/reassociating when it isn't.
2916 devdbg(usbdev, "ignored OID_802_11_ADD_KEY triggered "
2921 usbnet_pause_rx(usbdev);
2923 devinfo(usbdev, "media connect");
2925 /* queue work to avoid recursive calls into rndis_command */
2926 set_bit(WORK_LINK_UP, &priv->work_pending);
2927 queue_work(priv->workqueue, &priv->work);
2930 case RNDIS_STATUS_MEDIA_DISCONNECT:
2931 devinfo(usbdev, "media disconnect");
2933 /* queue work to avoid recursive calls into rndis_command */
2934 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2935 queue_work(priv->workqueue, &priv->work);
2938 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2939 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2943 devinfo(usbdev, "indication: 0x%08x",
2944 le32_to_cpu(msg->status));
2950 static int rndis_wlan_get_caps(struct usbnet *usbdev)
2955 } networks_supported;
2956 int len, retval, i, n;
2957 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2959 /* determine supported modes */
2960 len = sizeof(networks_supported);
2961 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2962 &networks_supported, &len);
2964 n = le32_to_cpu(networks_supported.num_items);
2967 for (i = 0; i < n; i++) {
2968 switch (le32_to_cpu(networks_supported.items[i])) {
2969 case NDIS_80211_TYPE_FREQ_HOP:
2970 case NDIS_80211_TYPE_DIRECT_SEQ:
2971 priv->caps |= CAP_MODE_80211B;
2973 case NDIS_80211_TYPE_OFDM_A:
2974 priv->caps |= CAP_MODE_80211A;
2976 case NDIS_80211_TYPE_OFDM_G:
2977 priv->caps |= CAP_MODE_80211G;
2987 #define STATS_UPDATE_JIFFIES (HZ)
2988 static void rndis_update_wireless_stats(struct work_struct *work)
2990 struct rndis_wlan_private *priv =
2991 container_of(work, struct rndis_wlan_private, stats_work.work);
2992 struct usbnet *usbdev = priv->usbdev;
2995 int update_jiffies = STATS_UPDATE_JIFFIES;
2998 /* Only check/do workaround when connected. Calling is_associated()
2999 * also polls device with rndis_command() and catches for media link
3002 if (!is_associated(usbdev))
3006 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
3008 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
3010 devdbg(usbdev, "stats: OID_802_11_RSSI -> %d, rssi:%d", ret,
3013 /* Workaround transfer stalls on poor quality links.
3014 * TODO: find right way to fix these stalls (as stalls do not happen
3015 * with ndiswrapper/windows driver). */
3016 if (priv->last_qual <= 25) {
3017 /* Decrease stats worker interval to catch stalls.
3018 * faster. Faster than 400-500ms causes packet loss,
3019 * Slower doesn't catch stalls fast enough.
3021 j = msecs_to_jiffies(priv->param_workaround_interval);
3022 if (j > STATS_UPDATE_JIFFIES)
3023 j = STATS_UPDATE_JIFFIES;
3028 /* Send scan OID. Use of both OIDs is required to get device
3031 tmp = cpu_to_le32(1);
3032 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
3035 len = CONTROL_BUFFER_SIZE;
3036 buf = kmalloc(len, GFP_KERNEL);
3040 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
3045 if (update_jiffies >= HZ)
3046 update_jiffies = round_jiffies_relative(update_jiffies);
3048 j = round_jiffies_relative(update_jiffies);
3049 if (abs(j - update_jiffies) <= 10)
3053 queue_delayed_work(priv->workqueue, &priv->stats_work, update_jiffies);
3057 static int bcm4320a_early_init(struct usbnet *usbdev)
3059 /* bcm4320a doesn't handle configuration parameters well. Try
3060 * set any and you get partially zeroed mac and broken device.
3067 static int bcm4320b_early_init(struct usbnet *usbdev)
3069 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3072 /* Early initialization settings, setting these won't have effect
3073 * if called after generic_rndis_bind().
3076 priv->param_country[0] = modparam_country[0];
3077 priv->param_country[1] = modparam_country[1];
3078 priv->param_country[2] = 0;
3079 priv->param_frameburst = modparam_frameburst;
3080 priv->param_afterburner = modparam_afterburner;
3081 priv->param_power_save = modparam_power_save;
3082 priv->param_power_output = modparam_power_output;
3083 priv->param_roamtrigger = modparam_roamtrigger;
3084 priv->param_roamdelta = modparam_roamdelta;
3086 priv->param_country[0] = toupper(priv->param_country[0]);
3087 priv->param_country[1] = toupper(priv->param_country[1]);
3088 /* doesn't support EU as country code, use FI instead */
3089 if (!strcmp(priv->param_country, "EU"))
3090 strcpy(priv->param_country, "FI");
3092 if (priv->param_power_save < 0)
3093 priv->param_power_save = 0;
3094 else if (priv->param_power_save > 2)
3095 priv->param_power_save = 2;
3097 if (priv->param_power_output < 0)
3098 priv->param_power_output = 0;
3099 else if (priv->param_power_output > 3)
3100 priv->param_power_output = 3;
3102 if (priv->param_roamtrigger < -80)
3103 priv->param_roamtrigger = -80;
3104 else if (priv->param_roamtrigger > -60)
3105 priv->param_roamtrigger = -60;
3107 if (priv->param_roamdelta < 0)
3108 priv->param_roamdelta = 0;
3109 else if (priv->param_roamdelta > 2)
3110 priv->param_roamdelta = 2;
3112 if (modparam_workaround_interval < 0)
3113 priv->param_workaround_interval = 500;
3115 priv->param_workaround_interval = modparam_workaround_interval;
3117 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3118 rndis_set_config_parameter_str(usbdev, "FrameBursting",
3119 priv->param_frameburst ? "1" : "0");
3120 rndis_set_config_parameter_str(usbdev, "Afterburner",
3121 priv->param_afterburner ? "1" : "0");
3122 sprintf(buf, "%d", priv->param_power_save);
3123 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3124 sprintf(buf, "%d", priv->param_power_output);
3125 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3126 sprintf(buf, "%d", priv->param_roamtrigger);
3127 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3128 sprintf(buf, "%d", priv->param_roamdelta);
3129 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3134 /* same as rndis_netdev_ops but with local multicast handler */
3135 static const struct net_device_ops rndis_wlan_netdev_ops = {
3136 .ndo_open = usbnet_open,
3137 .ndo_stop = usbnet_stop,
3138 .ndo_start_xmit = usbnet_start_xmit,
3139 .ndo_tx_timeout = usbnet_tx_timeout,
3140 .ndo_set_mac_address = eth_mac_addr,
3141 .ndo_validate_addr = eth_validate_addr,
3142 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3146 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3148 struct wiphy *wiphy;
3149 struct rndis_wlan_private *priv;
3153 /* allocate wiphy and rndis private data
3154 * NOTE: We only support a single virtual interface, so wiphy
3155 * and wireless_dev are somewhat synonymous for this device.
3157 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3161 priv = wiphy_priv(wiphy);
3162 usbdev->net->ieee80211_ptr = &priv->wdev;
3163 priv->wdev.wiphy = wiphy;
3164 priv->wdev.iftype = NL80211_IFTYPE_STATION;
3166 /* These have to be initialized before calling generic_rndis_bind().
3167 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3169 usbdev->driver_priv = priv;
3170 usbdev->net->wireless_handlers = &rndis_iw_handlers;
3171 priv->usbdev = usbdev;
3173 mutex_init(&priv->command_lock);
3175 /* because rndis_command() sleeps we need to use workqueue */
3176 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3177 INIT_WORK(&priv->work, rndis_wlan_worker);
3178 INIT_DELAYED_WORK(&priv->stats_work, rndis_update_wireless_stats);
3179 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3181 /* try bind rndis_host */
3182 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3186 /* generic_rndis_bind set packet filter to multicast_all+
3187 * promisc mode which doesn't work well for our devices (device
3188 * picks up rssi to closest station instead of to access point).
3190 * rndis_host wants to avoid all OID as much as possible
3191 * so do promisc/multicast handling in rndis_wlan.
3193 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3195 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3196 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3200 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3202 priv->multicast_size = le32_to_cpu(tmp);
3203 if (retval < 0 || priv->multicast_size < 0)
3204 priv->multicast_size = 0;
3205 if (priv->multicast_size > 0)
3206 usbdev->net->flags |= IFF_MULTICAST;
3208 usbdev->net->flags &= ~IFF_MULTICAST;
3210 /* fill-out wiphy structure and register w/ cfg80211 */
3211 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3212 wiphy->privid = rndis_wiphy_privid;
3213 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3214 | BIT(NL80211_IFTYPE_ADHOC);
3215 wiphy->max_scan_ssids = 1;
3217 /* TODO: fill-out band/encr information based on priv->caps */
3218 rndis_wlan_get_caps(usbdev);
3220 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3221 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3222 priv->band.channels = priv->channels;
3223 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3224 priv->band.bitrates = priv->rates;
3225 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3226 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3227 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3229 memcpy(priv->cipher_suites, rndis_cipher_suites,
3230 sizeof(rndis_cipher_suites));
3231 wiphy->cipher_suites = priv->cipher_suites;
3232 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3234 set_wiphy_dev(wiphy, &usbdev->udev->dev);
3236 if (wiphy_register(wiphy)) {
3241 set_default_iw_params(usbdev);
3243 /* set default rts/frag */
3244 rndis_set_wiphy_params(wiphy,
3245 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3249 disassociate(usbdev, 1);
3250 netif_carrier_off(usbdev->net);
3255 cancel_delayed_work_sync(&priv->stats_work);
3256 cancel_delayed_work_sync(&priv->scan_work);
3257 cancel_work_sync(&priv->work);
3258 flush_workqueue(priv->workqueue);
3259 destroy_workqueue(priv->workqueue);
3266 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3268 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3270 /* turn radio off */
3271 disassociate(usbdev, 0);
3273 cancel_delayed_work_sync(&priv->stats_work);
3274 cancel_delayed_work_sync(&priv->scan_work);
3275 cancel_work_sync(&priv->work);
3276 flush_workqueue(priv->workqueue);
3277 destroy_workqueue(priv->workqueue);
3279 if (priv && priv->wpa_ie_len)
3280 kfree(priv->wpa_ie);
3282 rndis_unbind(usbdev, intf);
3284 wiphy_unregister(priv->wdev.wiphy);
3285 wiphy_free(priv->wdev.wiphy);
3289 static int rndis_wlan_reset(struct usbnet *usbdev)
3291 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3294 devdbg(usbdev, "rndis_wlan_reset");
3296 retval = rndis_reset(usbdev);
3298 devwarn(usbdev, "rndis_reset() failed: %d", retval);
3300 /* rndis_reset cleared multicast list, so restore here.
3301 (set_multicast_list() also turns on current packet filter) */
3302 set_multicast_list(usbdev);
3304 queue_delayed_work(priv->workqueue, &priv->stats_work,
3305 round_jiffies_relative(STATS_UPDATE_JIFFIES));
3307 return deauthenticate(usbdev);
3311 static int rndis_wlan_stop(struct usbnet *usbdev)
3313 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3317 devdbg(usbdev, "rndis_wlan_stop");
3319 retval = disassociate(usbdev, 0);
3321 priv->work_pending = 0;
3322 cancel_delayed_work_sync(&priv->stats_work);
3323 cancel_delayed_work_sync(&priv->scan_work);
3324 cancel_work_sync(&priv->work);
3325 flush_workqueue(priv->workqueue);
3327 if (priv->scan_request) {
3328 cfg80211_scan_done(priv->scan_request, true);
3329 priv->scan_request = NULL;
3332 /* Set current packet filter zero to block receiving data packets from
3335 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3342 static const struct driver_info bcm4320b_info = {
3343 .description = "Wireless RNDIS device, BCM4320b based",
3344 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3345 FLAG_AVOID_UNLINK_URBS,
3346 .bind = rndis_wlan_bind,
3347 .unbind = rndis_wlan_unbind,
3348 .status = rndis_status,
3349 .rx_fixup = rndis_rx_fixup,
3350 .tx_fixup = rndis_tx_fixup,
3351 .reset = rndis_wlan_reset,
3352 .stop = rndis_wlan_stop,
3353 .early_init = bcm4320b_early_init,
3354 .indication = rndis_wlan_indication,
3357 static const struct driver_info bcm4320a_info = {
3358 .description = "Wireless RNDIS device, BCM4320a based",
3359 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3360 FLAG_AVOID_UNLINK_URBS,
3361 .bind = rndis_wlan_bind,
3362 .unbind = rndis_wlan_unbind,
3363 .status = rndis_status,
3364 .rx_fixup = rndis_rx_fixup,
3365 .tx_fixup = rndis_tx_fixup,
3366 .reset = rndis_wlan_reset,
3367 .stop = rndis_wlan_stop,
3368 .early_init = bcm4320a_early_init,
3369 .indication = rndis_wlan_indication,
3372 static const struct driver_info rndis_wlan_info = {
3373 .description = "Wireless RNDIS device",
3374 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3375 FLAG_AVOID_UNLINK_URBS,
3376 .bind = rndis_wlan_bind,
3377 .unbind = rndis_wlan_unbind,
3378 .status = rndis_status,
3379 .rx_fixup = rndis_rx_fixup,
3380 .tx_fixup = rndis_tx_fixup,
3381 .reset = rndis_wlan_reset,
3382 .stop = rndis_wlan_stop,
3383 .early_init = bcm4320a_early_init,
3384 .indication = rndis_wlan_indication,
3387 /*-------------------------------------------------------------------------*/
3389 static const struct usb_device_id products [] = {
3390 #define RNDIS_MASTER_INTERFACE \
3391 .bInterfaceClass = USB_CLASS_COMM, \
3392 .bInterfaceSubClass = 2 /* ACM */, \
3393 .bInterfaceProtocol = 0x0ff
3395 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3396 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3399 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3400 | USB_DEVICE_ID_MATCH_DEVICE,
3402 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
3403 RNDIS_MASTER_INTERFACE,
3404 .driver_info = (unsigned long) &bcm4320b_info,
3406 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3407 | USB_DEVICE_ID_MATCH_DEVICE,
3409 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
3410 RNDIS_MASTER_INTERFACE,
3411 .driver_info = (unsigned long) &bcm4320b_info,
3413 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3414 | USB_DEVICE_ID_MATCH_DEVICE,
3416 .idProduct = 0x011b, /* Belkin F5D7051 */
3417 RNDIS_MASTER_INTERFACE,
3418 .driver_info = (unsigned long) &bcm4320b_info,
3420 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3421 | USB_DEVICE_ID_MATCH_DEVICE,
3422 .idVendor = 0x1799, /* Belkin has two vendor ids */
3423 .idProduct = 0x011b, /* Belkin F5D7051 */
3424 RNDIS_MASTER_INTERFACE,
3425 .driver_info = (unsigned long) &bcm4320b_info,
3427 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3428 | USB_DEVICE_ID_MATCH_DEVICE,
3430 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
3431 RNDIS_MASTER_INTERFACE,
3432 .driver_info = (unsigned long) &bcm4320b_info,
3434 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3435 | USB_DEVICE_ID_MATCH_DEVICE,
3437 .idProduct = 0x0026, /* Linksys WUSB54GSC */
3438 RNDIS_MASTER_INTERFACE,
3439 .driver_info = (unsigned long) &bcm4320b_info,
3441 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3442 | USB_DEVICE_ID_MATCH_DEVICE,
3444 .idProduct = 0x1717, /* Asus WL169gE */
3445 RNDIS_MASTER_INTERFACE,
3446 .driver_info = (unsigned long) &bcm4320b_info,
3448 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3449 | USB_DEVICE_ID_MATCH_DEVICE,
3451 .idProduct = 0xd11b, /* Eminent EM4045 */
3452 RNDIS_MASTER_INTERFACE,
3453 .driver_info = (unsigned long) &bcm4320b_info,
3455 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3456 | USB_DEVICE_ID_MATCH_DEVICE,
3458 .idProduct = 0x0715, /* BT Voyager 1055 */
3459 RNDIS_MASTER_INTERFACE,
3460 .driver_info = (unsigned long) &bcm4320b_info,
3462 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3463 * parameters available, hardware probably contain older firmware version with
3464 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3467 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3468 | USB_DEVICE_ID_MATCH_DEVICE,
3470 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3471 RNDIS_MASTER_INTERFACE,
3472 .driver_info = (unsigned long) &bcm4320a_info,
3474 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3475 | USB_DEVICE_ID_MATCH_DEVICE,
3477 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3478 RNDIS_MASTER_INTERFACE,
3479 .driver_info = (unsigned long) &bcm4320a_info,
3481 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3482 | USB_DEVICE_ID_MATCH_DEVICE,
3484 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3485 RNDIS_MASTER_INTERFACE,
3486 .driver_info = (unsigned long) &bcm4320a_info,
3488 /* Generic Wireless RNDIS devices that we don't have exact
3489 * idVendor/idProduct/chip yet.
3492 /* RNDIS is MSFT's un-official variant of CDC ACM */
3493 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3494 .driver_info = (unsigned long) &rndis_wlan_info,
3496 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3497 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3498 .driver_info = (unsigned long) &rndis_wlan_info,
3502 MODULE_DEVICE_TABLE(usb, products);
3504 static struct usb_driver rndis_wlan_driver = {
3505 .name = "rndis_wlan",
3506 .id_table = products,
3507 .probe = usbnet_probe,
3508 .disconnect = usbnet_disconnect,
3509 .suspend = usbnet_suspend,
3510 .resume = usbnet_resume,
3513 static int __init rndis_wlan_init(void)
3515 return usb_register(&rndis_wlan_driver);
3517 module_init(rndis_wlan_init);
3519 static void __exit rndis_wlan_exit(void)
3521 usb_deregister(&rndis_wlan_driver);
3523 module_exit(rndis_wlan_exit);
3525 MODULE_AUTHOR("Bjorge Dijkstra");
3526 MODULE_AUTHOR("Jussi Kivilinna");
3527 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3528 MODULE_LICENSE("GPL");