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 <linux/slab.h>
45 #include <net/iw_handler.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;
88 module_param_named(workaround_interval, modparam_workaround_interval,
90 MODULE_PARM_DESC(workaround_interval,
91 "set stall workaround interval in msecs (0=disabled) (default: 0)");
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_CURRENT_ADDRESS cpu_to_le32(0x01010102)
105 #define OID_802_3_MULTICAST_LIST cpu_to_le32(0x01010103)
106 #define OID_802_3_MAXIMUM_LIST_SIZE cpu_to_le32(0x01010104)
108 #define OID_802_11_BSSID cpu_to_le32(0x0d010101)
109 #define OID_802_11_SSID cpu_to_le32(0x0d010102)
110 #define OID_802_11_INFRASTRUCTURE_MODE cpu_to_le32(0x0d010108)
111 #define OID_802_11_ADD_WEP cpu_to_le32(0x0d010113)
112 #define OID_802_11_REMOVE_WEP cpu_to_le32(0x0d010114)
113 #define OID_802_11_DISASSOCIATE cpu_to_le32(0x0d010115)
114 #define OID_802_11_AUTHENTICATION_MODE cpu_to_le32(0x0d010118)
115 #define OID_802_11_PRIVACY_FILTER cpu_to_le32(0x0d010119)
116 #define OID_802_11_BSSID_LIST_SCAN cpu_to_le32(0x0d01011a)
117 #define OID_802_11_ENCRYPTION_STATUS cpu_to_le32(0x0d01011b)
118 #define OID_802_11_ADD_KEY cpu_to_le32(0x0d01011d)
119 #define OID_802_11_REMOVE_KEY cpu_to_le32(0x0d01011e)
120 #define OID_802_11_ASSOCIATION_INFORMATION cpu_to_le32(0x0d01011f)
121 #define OID_802_11_CAPABILITY cpu_to_le32(0x0d010122)
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.
143 * 100% : 20 mW ~ 13dBm
144 * 75% : 15 mW ~ 12dBm
145 * 50% : 10 mW ~ 10dBm
148 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
149 #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
150 #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
151 #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
154 /* codes for "status" field of completion messages */
155 #define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
156 #define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
159 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
160 * slightly modified for datatype endianess, etc
162 #define NDIS_802_11_LENGTH_SSID 32
163 #define NDIS_802_11_LENGTH_RATES 8
164 #define NDIS_802_11_LENGTH_RATES_EX 16
166 enum ndis_80211_net_type {
167 NDIS_80211_TYPE_FREQ_HOP,
168 NDIS_80211_TYPE_DIRECT_SEQ,
169 NDIS_80211_TYPE_OFDM_A,
170 NDIS_80211_TYPE_OFDM_G
173 enum ndis_80211_net_infra {
174 NDIS_80211_INFRA_ADHOC,
175 NDIS_80211_INFRA_INFRA,
176 NDIS_80211_INFRA_AUTO_UNKNOWN
179 enum ndis_80211_auth_mode {
180 NDIS_80211_AUTH_OPEN,
181 NDIS_80211_AUTH_SHARED,
182 NDIS_80211_AUTH_AUTO_SWITCH,
184 NDIS_80211_AUTH_WPA_PSK,
185 NDIS_80211_AUTH_WPA_NONE,
186 NDIS_80211_AUTH_WPA2,
187 NDIS_80211_AUTH_WPA2_PSK
190 enum ndis_80211_encr_status {
191 NDIS_80211_ENCR_WEP_ENABLED,
192 NDIS_80211_ENCR_DISABLED,
193 NDIS_80211_ENCR_WEP_KEY_ABSENT,
194 NDIS_80211_ENCR_NOT_SUPPORTED,
195 NDIS_80211_ENCR_TKIP_ENABLED,
196 NDIS_80211_ENCR_TKIP_KEY_ABSENT,
197 NDIS_80211_ENCR_CCMP_ENABLED,
198 NDIS_80211_ENCR_CCMP_KEY_ABSENT
201 enum ndis_80211_priv_filter {
202 NDIS_80211_PRIV_ACCEPT_ALL,
203 NDIS_80211_PRIV_8021X_WEP
206 enum ndis_80211_status_type {
207 NDIS_80211_STATUSTYPE_AUTHENTICATION,
208 NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
209 NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
210 NDIS_80211_STATUSTYPE_RADIOSTATE,
213 enum ndis_80211_media_stream_mode {
214 NDIS_80211_MEDIA_STREAM_OFF,
215 NDIS_80211_MEDIA_STREAM_ON
218 enum ndis_80211_radio_status {
219 NDIS_80211_RADIO_STATUS_ON,
220 NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
221 NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
224 enum ndis_80211_addkey_bits {
225 NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
226 NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
227 NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
228 NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
231 enum ndis_80211_addwep_bits {
232 NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
233 NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
236 struct ndis_80211_auth_request {
241 } __attribute__((packed));
243 struct ndis_80211_pmkid_candidate {
247 } __attribute__((packed));
249 struct ndis_80211_pmkid_cand_list {
251 __le32 num_candidates;
252 struct ndis_80211_pmkid_candidate candidate_list[0];
253 } __attribute__((packed));
255 struct ndis_80211_status_indication {
258 __le32 media_stream_mode;
260 struct ndis_80211_auth_request auth_request[0];
261 struct ndis_80211_pmkid_cand_list cand_list;
263 } __attribute__((packed));
265 struct ndis_80211_ssid {
267 u8 essid[NDIS_802_11_LENGTH_SSID];
268 } __attribute__((packed));
270 struct ndis_80211_conf_freq_hop {
275 } __attribute__((packed));
277 struct ndis_80211_conf {
279 __le32 beacon_period;
282 struct ndis_80211_conf_freq_hop fh_config;
283 } __attribute__((packed));
285 struct ndis_80211_bssid_ex {
289 struct ndis_80211_ssid ssid;
293 struct ndis_80211_conf config;
295 u8 rates[NDIS_802_11_LENGTH_RATES_EX];
298 } __attribute__((packed));
300 struct ndis_80211_bssid_list_ex {
302 struct ndis_80211_bssid_ex bssid[0];
303 } __attribute__((packed));
305 struct ndis_80211_fixed_ies {
307 __le16 beacon_interval;
309 } __attribute__((packed));
311 struct ndis_80211_wep_key {
316 } __attribute__((packed));
318 struct ndis_80211_key {
326 } __attribute__((packed));
328 struct ndis_80211_remove_key {
333 } __attribute__((packed));
335 struct ndis_config_param {
341 } __attribute__((packed));
343 struct ndis_80211_assoc_info {
348 __le16 listen_interval;
349 u8 cur_ap_address[6];
351 __le32 req_ie_length;
352 __le32 offset_req_ies;
359 __le32 resp_ie_length;
360 __le32 offset_resp_ies;
361 } __attribute__((packed));
363 struct ndis_80211_auth_encr_pair {
366 } __attribute__((packed));
368 struct ndis_80211_capability {
372 __le32 num_auth_encr_pair;
373 struct ndis_80211_auth_encr_pair auth_encr_pair[0];
374 } __attribute__((packed));
376 struct ndis_80211_bssid_info {
381 struct ndis_80211_pmkid {
383 __le32 bssid_info_count;
384 struct ndis_80211_bssid_info bssid_info[0];
390 #define NET_TYPE_11FB 0
392 #define CAP_MODE_80211A 1
393 #define CAP_MODE_80211B 2
394 #define CAP_MODE_80211G 4
395 #define CAP_MODE_MASK 7
397 #define WORK_LINK_UP (1<<0)
398 #define WORK_LINK_DOWN (1<<1)
399 #define WORK_SET_MULTICAST_LIST (1<<2)
401 #define RNDIS_WLAN_ALG_NONE 0
402 #define RNDIS_WLAN_ALG_WEP (1<<0)
403 #define RNDIS_WLAN_ALG_TKIP (1<<1)
404 #define RNDIS_WLAN_ALG_CCMP (1<<2)
406 #define RNDIS_WLAN_KEY_MGMT_NONE 0
407 #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
408 #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
410 #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
412 static const struct ieee80211_channel rndis_channels[] = {
413 { .center_freq = 2412 },
414 { .center_freq = 2417 },
415 { .center_freq = 2422 },
416 { .center_freq = 2427 },
417 { .center_freq = 2432 },
418 { .center_freq = 2437 },
419 { .center_freq = 2442 },
420 { .center_freq = 2447 },
421 { .center_freq = 2452 },
422 { .center_freq = 2457 },
423 { .center_freq = 2462 },
424 { .center_freq = 2467 },
425 { .center_freq = 2472 },
426 { .center_freq = 2484 },
429 static const struct ieee80211_rate rndis_rates[] = {
431 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
432 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
433 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
444 static const u32 rndis_cipher_suites[] = {
445 WLAN_CIPHER_SUITE_WEP40,
446 WLAN_CIPHER_SUITE_WEP104,
447 WLAN_CIPHER_SUITE_TKIP,
448 WLAN_CIPHER_SUITE_CCMP,
451 struct rndis_wlan_encr_key {
460 /* RNDIS device private data */
461 struct rndis_wlan_private {
462 struct usbnet *usbdev;
464 struct wireless_dev wdev;
466 struct cfg80211_scan_request *scan_request;
468 struct workqueue_struct *workqueue;
469 struct delayed_work dev_poller_work;
470 struct delayed_work scan_work;
471 struct work_struct work;
472 struct mutex command_lock;
473 unsigned long work_pending;
476 struct ieee80211_supported_band band;
477 struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
478 struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
479 u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
484 /* module parameters */
485 char param_country[4];
486 int param_frameburst;
487 int param_afterburner;
488 int param_power_save;
489 int param_power_output;
490 int param_roamtrigger;
492 u32 param_workaround_interval;
499 struct ndis_80211_ssid essid;
500 __le32 current_command_oid;
502 /* encryption stuff */
503 int encr_tx_key_index;
504 struct rndis_wlan_encr_key encr_keys[4];
507 u8 command_buffer[COMMAND_BUFFER_SIZE];
513 static int rndis_change_virtual_intf(struct wiphy *wiphy,
514 struct net_device *dev,
515 enum nl80211_iftype type, u32 *flags,
516 struct vif_params *params);
518 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
519 struct cfg80211_scan_request *request);
521 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
523 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
525 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
527 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
528 struct cfg80211_connect_params *sme);
530 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
533 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
534 struct cfg80211_ibss_params *params);
536 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
538 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
539 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
541 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
542 u8 key_index, const u8 *mac_addr,
543 struct key_params *params);
545 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
546 u8 key_index, const u8 *mac_addr);
548 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
551 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
552 u8 *mac, struct station_info *sinfo);
554 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
555 int idx, u8 *mac, struct station_info *sinfo);
557 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
558 struct cfg80211_pmksa *pmksa);
560 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
561 struct cfg80211_pmksa *pmksa);
563 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
565 static struct cfg80211_ops rndis_config_ops = {
566 .change_virtual_intf = rndis_change_virtual_intf,
568 .set_wiphy_params = rndis_set_wiphy_params,
569 .set_tx_power = rndis_set_tx_power,
570 .get_tx_power = rndis_get_tx_power,
571 .connect = rndis_connect,
572 .disconnect = rndis_disconnect,
573 .join_ibss = rndis_join_ibss,
574 .leave_ibss = rndis_leave_ibss,
575 .set_channel = rndis_set_channel,
576 .add_key = rndis_add_key,
577 .del_key = rndis_del_key,
578 .set_default_key = rndis_set_default_key,
579 .get_station = rndis_get_station,
580 .dump_station = rndis_dump_station,
581 .set_pmksa = rndis_set_pmksa,
582 .del_pmksa = rndis_del_pmksa,
583 .flush_pmksa = rndis_flush_pmksa,
586 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
589 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
591 return (struct rndis_wlan_private *)dev->driver_priv;
594 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
596 switch (priv->param_power_output) {
599 return BCM4320_DEFAULT_TXPOWER_DBM_100;
601 return BCM4320_DEFAULT_TXPOWER_DBM_75;
603 return BCM4320_DEFAULT_TXPOWER_DBM_50;
605 return BCM4320_DEFAULT_TXPOWER_DBM_25;
609 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
611 int cipher = priv->encr_keys[idx].cipher;
613 return (cipher == WLAN_CIPHER_SUITE_CCMP ||
614 cipher == WLAN_CIPHER_SUITE_TKIP);
617 static int rndis_cipher_to_alg(u32 cipher)
621 return RNDIS_WLAN_ALG_NONE;
622 case WLAN_CIPHER_SUITE_WEP40:
623 case WLAN_CIPHER_SUITE_WEP104:
624 return RNDIS_WLAN_ALG_WEP;
625 case WLAN_CIPHER_SUITE_TKIP:
626 return RNDIS_WLAN_ALG_TKIP;
627 case WLAN_CIPHER_SUITE_CCMP:
628 return RNDIS_WLAN_ALG_CCMP;
632 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
636 return RNDIS_WLAN_KEY_MGMT_NONE;
637 case WLAN_AKM_SUITE_8021X:
638 return RNDIS_WLAN_KEY_MGMT_802_1X;
639 case WLAN_AKM_SUITE_PSK:
640 return RNDIS_WLAN_KEY_MGMT_PSK;
645 static const char *oid_to_string(__le32 oid)
648 #define OID_STR(oid) case oid: return(#oid)
649 /* from rndis_host.h */
650 OID_STR(OID_802_3_PERMANENT_ADDRESS);
651 OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
652 OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
653 OID_STR(OID_GEN_PHYSICAL_MEDIUM);
655 /* from rndis_wlan.c */
656 OID_STR(OID_GEN_LINK_SPEED);
657 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
659 OID_STR(OID_GEN_XMIT_OK);
660 OID_STR(OID_GEN_RCV_OK);
661 OID_STR(OID_GEN_XMIT_ERROR);
662 OID_STR(OID_GEN_RCV_ERROR);
663 OID_STR(OID_GEN_RCV_NO_BUFFER);
665 OID_STR(OID_802_3_CURRENT_ADDRESS);
666 OID_STR(OID_802_3_MULTICAST_LIST);
667 OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
669 OID_STR(OID_802_11_BSSID);
670 OID_STR(OID_802_11_SSID);
671 OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
672 OID_STR(OID_802_11_ADD_WEP);
673 OID_STR(OID_802_11_REMOVE_WEP);
674 OID_STR(OID_802_11_DISASSOCIATE);
675 OID_STR(OID_802_11_AUTHENTICATION_MODE);
676 OID_STR(OID_802_11_PRIVACY_FILTER);
677 OID_STR(OID_802_11_BSSID_LIST_SCAN);
678 OID_STR(OID_802_11_ENCRYPTION_STATUS);
679 OID_STR(OID_802_11_ADD_KEY);
680 OID_STR(OID_802_11_REMOVE_KEY);
681 OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
682 OID_STR(OID_802_11_PMKID);
683 OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
684 OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
685 OID_STR(OID_802_11_TX_POWER_LEVEL);
686 OID_STR(OID_802_11_RSSI);
687 OID_STR(OID_802_11_RSSI_TRIGGER);
688 OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
689 OID_STR(OID_802_11_RTS_THRESHOLD);
690 OID_STR(OID_802_11_SUPPORTED_RATES);
691 OID_STR(OID_802_11_CONFIGURATION);
692 OID_STR(OID_802_11_BSSID_LIST);
699 static const char *oid_to_string(__le32 oid)
705 /* translate error code */
706 static int rndis_error_status(__le32 rndis_status)
709 switch (rndis_status) {
710 case RNDIS_STATUS_SUCCESS:
713 case RNDIS_STATUS_FAILURE:
714 case RNDIS_STATUS_INVALID_DATA:
717 case RNDIS_STATUS_NOT_SUPPORTED:
720 case RNDIS_STATUS_ADAPTER_NOT_READY:
721 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
728 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
730 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
733 struct rndis_msg_hdr *header;
734 struct rndis_query *get;
735 struct rndis_query_c *get_c;
738 int resplen, respoffs, copylen;
740 buflen = *len + sizeof(*u.get);
741 if (buflen < CONTROL_BUFFER_SIZE)
742 buflen = CONTROL_BUFFER_SIZE;
744 if (buflen > COMMAND_BUFFER_SIZE) {
745 u.buf = kmalloc(buflen, GFP_KERNEL);
749 u.buf = priv->command_buffer;
752 mutex_lock(&priv->command_lock);
754 memset(u.get, 0, sizeof *u.get);
755 u.get->msg_type = RNDIS_MSG_QUERY;
756 u.get->msg_len = cpu_to_le32(sizeof *u.get);
759 priv->current_command_oid = oid;
760 ret = rndis_command(dev, u.header, buflen);
761 priv->current_command_oid = 0;
763 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
764 __func__, oid_to_string(oid), ret,
765 le32_to_cpu(u.get_c->status));
768 resplen = le32_to_cpu(u.get_c->len);
769 respoffs = le32_to_cpu(u.get_c->offset) + 8;
771 if (respoffs > buflen) {
772 /* Device returned data offset outside buffer, error. */
773 netdev_dbg(dev->net, "%s(%s): received invalid "
774 "data offset: %d > %d\n", __func__,
775 oid_to_string(oid), respoffs, buflen);
781 if ((resplen + respoffs) > buflen) {
782 /* Device would have returned more data if buffer would
783 * have been big enough. Copy just the bits that we got.
785 copylen = buflen - respoffs;
793 memcpy(data, u.buf + respoffs, copylen);
797 ret = rndis_error_status(u.get_c->status);
799 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
800 __func__, oid_to_string(oid),
801 le32_to_cpu(u.get_c->status), ret);
805 mutex_unlock(&priv->command_lock);
807 if (u.buf != priv->command_buffer)
812 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
814 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
817 struct rndis_msg_hdr *header;
818 struct rndis_set *set;
819 struct rndis_set_c *set_c;
823 buflen = len + sizeof(*u.set);
824 if (buflen < CONTROL_BUFFER_SIZE)
825 buflen = CONTROL_BUFFER_SIZE;
827 if (buflen > COMMAND_BUFFER_SIZE) {
828 u.buf = kmalloc(buflen, GFP_KERNEL);
832 u.buf = priv->command_buffer;
835 mutex_lock(&priv->command_lock);
837 memset(u.set, 0, sizeof *u.set);
838 u.set->msg_type = RNDIS_MSG_SET;
839 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
841 u.set->len = cpu_to_le32(len);
842 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
843 u.set->handle = cpu_to_le32(0);
844 memcpy(u.buf + sizeof(*u.set), data, len);
846 priv->current_command_oid = oid;
847 ret = rndis_command(dev, u.header, buflen);
848 priv->current_command_oid = 0;
850 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
851 __func__, oid_to_string(oid), ret,
852 le32_to_cpu(u.set_c->status));
855 ret = rndis_error_status(u.set_c->status);
858 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
859 __func__, oid_to_string(oid),
860 le32_to_cpu(u.set_c->status), ret);
863 mutex_unlock(&priv->command_lock);
865 if (u.buf != priv->command_buffer)
870 static int rndis_reset(struct usbnet *usbdev)
872 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
873 struct rndis_reset *reset;
876 mutex_lock(&priv->command_lock);
878 reset = (void *)priv->command_buffer;
879 memset(reset, 0, sizeof(*reset));
880 reset->msg_type = RNDIS_MSG_RESET;
881 reset->msg_len = cpu_to_le32(sizeof(*reset));
882 priv->current_command_oid = 0;
883 ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
885 mutex_unlock(&priv->command_lock);
893 * Specs say that we can only set config parameters only soon after device
895 * value_type: 0 = u32, 2 = unicode string
897 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
898 int value_type, void *value)
900 struct ndis_config_param *infobuf;
901 int value_len, info_len, param_len, ret, i;
906 value_len = sizeof(__le32);
907 else if (value_type == 2)
908 value_len = strlen(value) * sizeof(__le16);
912 param_len = strlen(param) * sizeof(__le16);
913 info_len = sizeof(*infobuf) + param_len + value_len;
918 infobuf = kmalloc(info_len, GFP_KERNEL);
924 /* extra 12 bytes are for padding (debug output) */
925 memset(infobuf, 0xCC, info_len + 12);
929 netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
932 netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
933 param, *(u32 *)value);
935 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
936 infobuf->name_length = cpu_to_le32(param_len);
937 infobuf->type = cpu_to_le32(value_type);
938 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
939 infobuf->value_length = cpu_to_le32(value_len);
941 /* simple string to unicode string conversion */
942 unibuf = (void *)infobuf + sizeof(*infobuf);
943 for (i = 0; i < param_len / sizeof(__le16); i++)
944 unibuf[i] = cpu_to_le16(param[i]);
946 if (value_type == 2) {
947 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
948 for (i = 0; i < value_len / sizeof(__le16); i++)
949 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
951 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
952 *dst_value = cpu_to_le32(*(u32 *)value);
956 netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
957 for (i = 0; i < info_len; i += 12) {
958 u32 *tmp = (u32 *)((u8 *)infobuf + i);
959 netdev_dbg(dev->net, "%08X:%08X:%08X\n",
962 cpu_to_be32(tmp[2]));
966 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
969 netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
976 static int rndis_set_config_parameter_str(struct usbnet *dev,
977 char *param, char *value)
979 return rndis_set_config_parameter(dev, param, 2, value);
983 * data conversion functions
985 static int level_to_qual(int level)
987 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
988 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
994 static int set_infra_mode(struct usbnet *usbdev, int mode);
995 static void restore_keys(struct usbnet *usbdev);
996 static int rndis_check_bssid_list(struct usbnet *usbdev);
998 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
1000 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1003 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
1005 netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
1009 memcpy(&priv->essid, ssid, sizeof(priv->essid));
1010 priv->radio_on = true;
1011 netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
1017 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1021 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1023 netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1031 static int clear_bssid(struct usbnet *usbdev)
1033 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1035 return set_bssid(usbdev, broadcast_mac);
1038 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1043 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1046 memset(bssid, 0, ETH_ALEN);
1051 static int get_association_info(struct usbnet *usbdev,
1052 struct ndis_80211_assoc_info *info, int len)
1054 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1058 static bool is_associated(struct usbnet *usbdev)
1060 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1064 if (!priv->radio_on)
1067 ret = get_bssid(usbdev, bssid);
1069 return (ret == 0 && !is_zero_ether_addr(bssid));
1072 static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1074 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1075 struct ndis_80211_ssid ssid;
1078 if (priv->radio_on) {
1079 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1081 priv->radio_on = false;
1082 netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1090 /* disassociate causes radio to be turned off; if reset_ssid
1091 * is given, set random ssid to enable radio */
1093 /* Set device to infrastructure mode so we don't get ad-hoc
1094 * 'media connect' indications with the random ssid.
1096 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1098 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1099 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1100 ssid.essid[0] = 0x1;
1101 ssid.essid[1] = 0xff;
1102 for (i = 2; i < sizeof(ssid.essid); i++)
1103 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1104 ret = set_essid(usbdev, &ssid);
1109 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1110 enum nl80211_auth_type auth_type, int keymgmt)
1112 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1116 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1117 __func__, wpa_version, auth_type, keymgmt);
1119 if (wpa_version & NL80211_WPA_VERSION_2) {
1120 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1121 auth_mode = NDIS_80211_AUTH_WPA2;
1123 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1124 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1125 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1126 auth_mode = NDIS_80211_AUTH_WPA;
1127 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1128 auth_mode = NDIS_80211_AUTH_WPA_PSK;
1130 auth_mode = NDIS_80211_AUTH_WPA_NONE;
1131 } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1132 auth_mode = NDIS_80211_AUTH_SHARED;
1133 else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1134 auth_mode = NDIS_80211_AUTH_OPEN;
1135 else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1136 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1140 tmp = cpu_to_le32(auth_mode);
1141 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1144 netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1149 priv->wpa_version = wpa_version;
1154 static int set_priv_filter(struct usbnet *usbdev)
1156 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1159 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1160 __func__, priv->wpa_version);
1162 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1163 priv->wpa_version & NL80211_WPA_VERSION_1)
1164 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1166 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1168 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1172 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1177 netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1178 __func__, pairwise, groupwise);
1180 if (pairwise & RNDIS_WLAN_ALG_CCMP)
1181 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1182 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1183 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1184 else if (pairwise & RNDIS_WLAN_ALG_WEP)
1185 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1186 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1187 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1188 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1189 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1191 encr_mode = NDIS_80211_ENCR_DISABLED;
1193 tmp = cpu_to_le32(encr_mode);
1194 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1197 netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1205 static int set_infra_mode(struct usbnet *usbdev, int mode)
1207 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1211 netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1212 __func__, priv->infra_mode);
1214 tmp = cpu_to_le32(mode);
1215 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1218 netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1223 /* NDIS drivers clear keys when infrastructure mode is
1224 * changed. But Linux tools assume otherwise. So set the
1226 restore_keys(usbdev);
1228 priv->infra_mode = mode;
1232 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1236 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1238 if (rts_threshold < 0 || rts_threshold > 2347)
1239 rts_threshold = 2347;
1241 tmp = cpu_to_le32(rts_threshold);
1242 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1246 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1250 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1252 if (frag_threshold < 256 || frag_threshold > 2346)
1253 frag_threshold = 2346;
1255 tmp = cpu_to_le32(frag_threshold);
1256 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1260 static void set_default_iw_params(struct usbnet *usbdev)
1262 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1263 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1264 RNDIS_WLAN_KEY_MGMT_NONE);
1265 set_priv_filter(usbdev);
1266 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1269 static int deauthenticate(struct usbnet *usbdev)
1273 ret = disassociate(usbdev, true);
1274 set_default_iw_params(usbdev);
1278 static int set_channel(struct usbnet *usbdev, int channel)
1280 struct ndis_80211_conf config;
1281 unsigned int dsconfig;
1284 netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1286 /* this OID is valid only when not associated */
1287 if (is_associated(usbdev))
1290 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1292 len = sizeof(config);
1293 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1295 netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1300 config.ds_config = cpu_to_le32(dsconfig);
1301 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1304 netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1309 /* index must be 0 - N, as per NDIS */
1310 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1313 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1314 struct ndis_80211_wep_key ndis_key;
1318 netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1319 __func__, index, key_len);
1321 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1325 cipher = WLAN_CIPHER_SUITE_WEP40;
1327 cipher = WLAN_CIPHER_SUITE_WEP104;
1329 memset(&ndis_key, 0, sizeof(ndis_key));
1331 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1332 ndis_key.length = cpu_to_le32(key_len);
1333 ndis_key.index = cpu_to_le32(index);
1334 memcpy(&ndis_key.material, key, key_len);
1336 if (index == priv->encr_tx_key_index) {
1337 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1338 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1339 RNDIS_WLAN_ALG_NONE);
1341 netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1345 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1348 netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1353 priv->encr_keys[index].len = key_len;
1354 priv->encr_keys[index].cipher = cipher;
1355 memcpy(&priv->encr_keys[index].material, key, key_len);
1356 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1361 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1362 int index, const u8 *addr, const u8 *rx_seq,
1363 int seq_len, u32 cipher, __le32 flags)
1365 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1366 struct ndis_80211_key ndis_key;
1370 if (index < 0 || index >= 4) {
1371 netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1375 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1376 netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1380 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1381 if (!rx_seq || seq_len <= 0) {
1382 netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1386 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1387 netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1392 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1393 !is_broadcast_ether_addr(addr);
1394 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1395 netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1400 netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1402 !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1403 !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1404 !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1406 memset(&ndis_key, 0, sizeof(ndis_key));
1408 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1409 sizeof(ndis_key.material) + key_len);
1410 ndis_key.length = cpu_to_le32(key_len);
1411 ndis_key.index = cpu_to_le32(index) | flags;
1413 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1414 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1415 * different order than NDIS spec, so swap the order here. */
1416 memcpy(ndis_key.material, key, 16);
1417 memcpy(ndis_key.material + 16, key + 24, 8);
1418 memcpy(ndis_key.material + 24, key + 16, 8);
1420 memcpy(ndis_key.material, key, key_len);
1422 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1423 memcpy(ndis_key.rsc, rx_seq, seq_len);
1425 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1427 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1430 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1431 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1433 get_bssid(usbdev, ndis_key.bssid);
1436 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1437 le32_to_cpu(ndis_key.size));
1438 netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1443 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1444 priv->encr_keys[index].len = key_len;
1445 priv->encr_keys[index].cipher = cipher;
1446 memcpy(&priv->encr_keys[index].material, key, key_len);
1447 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1448 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1450 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1452 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1453 priv->encr_tx_key_index = index;
1458 static int restore_key(struct usbnet *usbdev, int key_idx)
1460 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1461 struct rndis_wlan_encr_key key;
1463 if (is_wpa_key(priv, key_idx))
1466 key = priv->encr_keys[key_idx];
1468 netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1473 return add_wep_key(usbdev, key.material, key.len, key_idx);
1476 static void restore_keys(struct usbnet *usbdev)
1480 for (i = 0; i < 4; i++)
1481 restore_key(usbdev, i);
1484 static void clear_key(struct rndis_wlan_private *priv, int idx)
1486 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1489 /* remove_key is for both wep and wpa */
1490 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1492 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1493 struct ndis_80211_remove_key remove_key;
1498 if (priv->encr_keys[index].len == 0)
1501 is_wpa = is_wpa_key(priv, index);
1503 netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1504 __func__, index, is_wpa ? "wpa" : "wep",
1505 priv->encr_keys[index].len);
1507 clear_key(priv, index);
1510 remove_key.size = cpu_to_le32(sizeof(remove_key));
1511 remove_key.index = cpu_to_le32(index);
1514 if (!is_broadcast_ether_addr(bssid))
1516 NDIS_80211_ADDKEY_PAIRWISE_KEY;
1517 memcpy(remove_key.bssid, bssid,
1518 sizeof(remove_key.bssid));
1520 memset(remove_key.bssid, 0xff,
1521 sizeof(remove_key.bssid));
1523 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1524 sizeof(remove_key));
1528 keyindex = cpu_to_le32(index);
1529 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1532 netdev_warn(usbdev->net,
1533 "removing encryption key %d failed (%08X)\n",
1539 /* if it is transmit key, disable encryption */
1540 if (index == priv->encr_tx_key_index)
1541 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1546 static void set_multicast_list(struct usbnet *usbdev)
1548 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1549 struct netdev_hw_addr *ha;
1550 __le32 filter, basefilter;
1552 char *mc_addrs = NULL;
1555 basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1556 RNDIS_PACKET_TYPE_BROADCAST;
1558 if (usbdev->net->flags & IFF_PROMISC) {
1559 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1560 RNDIS_PACKET_TYPE_ALL_LOCAL;
1561 } else if (usbdev->net->flags & IFF_ALLMULTI) {
1562 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1565 if (filter != basefilter)
1569 * mc_list should be accessed holding the lock, so copy addresses to
1570 * local buffer first.
1572 netif_addr_lock_bh(usbdev->net);
1573 mc_count = netdev_mc_count(usbdev->net);
1574 if (mc_count > priv->multicast_size) {
1575 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1576 } else if (mc_count) {
1579 mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1581 netdev_warn(usbdev->net,
1582 "couldn't alloc %d bytes of memory\n",
1583 mc_count * ETH_ALEN);
1584 netif_addr_unlock_bh(usbdev->net);
1588 netdev_for_each_mc_addr(ha, usbdev->net)
1589 memcpy(mc_addrs + i++ * ETH_ALEN,
1590 ha->addr, ETH_ALEN);
1592 netif_addr_unlock_bh(usbdev->net);
1594 if (filter != basefilter)
1598 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
1599 mc_count * ETH_ALEN);
1602 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1604 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1606 netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1607 mc_count, priv->multicast_size, ret);
1611 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1614 netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1615 le32_to_cpu(filter));
1618 netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1619 le32_to_cpu(filter), ret);
1623 static void debug_print_pmkids(struct usbnet *usbdev,
1624 struct ndis_80211_pmkid *pmkids,
1625 const char *func_str)
1627 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1628 int i, len, count, max_pmkids, entry_len;
1630 max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1631 len = le32_to_cpu(pmkids->length);
1632 count = le32_to_cpu(pmkids->bssid_info_count);
1634 entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1636 netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1637 "%d)\n", func_str, count, len, entry_len);
1639 if (count > max_pmkids)
1642 for (i = 0; i < count; i++) {
1643 u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1645 netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
1646 "pmkid: %08X:%08X:%08X:%08X\n",
1647 func_str, pmkids->bssid_info[i].bssid,
1648 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1649 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1653 static void debug_print_pmkids(struct usbnet *usbdev,
1654 struct ndis_80211_pmkid *pmkids,
1655 const char *func_str)
1661 static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1663 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1664 struct ndis_80211_pmkid *pmkids;
1665 int len, ret, max_pmkids;
1667 max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1668 len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
1670 pmkids = kzalloc(len, GFP_KERNEL);
1672 return ERR_PTR(-ENOMEM);
1674 pmkids->length = cpu_to_le32(len);
1675 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1677 ret = rndis_query_oid(usbdev, OID_802_11_PMKID, pmkids, &len);
1679 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d)"
1680 " -> %d\n", __func__, len, max_pmkids, ret);
1683 return ERR_PTR(ret);
1686 if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1687 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1689 debug_print_pmkids(usbdev, pmkids, __func__);
1694 static int set_device_pmkids(struct usbnet *usbdev,
1695 struct ndis_80211_pmkid *pmkids)
1697 int ret, len, num_pmkids;
1699 num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1700 len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
1701 pmkids->length = cpu_to_le32(len);
1703 debug_print_pmkids(usbdev, pmkids, __func__);
1705 ret = rndis_set_oid(usbdev, OID_802_11_PMKID, pmkids,
1706 le32_to_cpu(pmkids->length));
1708 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d) -> %d"
1709 "\n", __func__, len, num_pmkids, ret);
1716 static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1717 struct ndis_80211_pmkid *pmkids,
1718 struct cfg80211_pmksa *pmksa,
1721 int i, len, count, newlen, err;
1723 len = le32_to_cpu(pmkids->length);
1724 count = le32_to_cpu(pmkids->bssid_info_count);
1726 if (count > max_pmkids)
1729 for (i = 0; i < count; i++)
1730 if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
1734 /* pmkid not found */
1736 netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1737 __func__, pmksa->bssid);
1742 for (; i + 1 < count; i++)
1743 pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1746 newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
1748 pmkids->length = cpu_to_le32(newlen);
1749 pmkids->bssid_info_count = cpu_to_le32(count);
1754 return ERR_PTR(err);
1757 static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1758 struct ndis_80211_pmkid *pmkids,
1759 struct cfg80211_pmksa *pmksa,
1762 int i, err, len, count, newlen;
1764 len = le32_to_cpu(pmkids->length);
1765 count = le32_to_cpu(pmkids->bssid_info_count);
1767 if (count > max_pmkids)
1770 /* update with new pmkid */
1771 for (i = 0; i < count; i++) {
1772 if (compare_ether_addr(pmkids->bssid_info[i].bssid,
1776 memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1782 /* out of space, return error */
1783 if (i == max_pmkids) {
1784 netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1790 newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
1792 pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1798 pmkids->length = cpu_to_le32(newlen);
1799 pmkids->bssid_info_count = cpu_to_le32(count + 1);
1801 memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1802 memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1807 return ERR_PTR(err);
1813 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1814 struct net_device *dev,
1815 enum nl80211_iftype type, u32 *flags,
1816 struct vif_params *params)
1818 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1819 struct usbnet *usbdev = priv->usbdev;
1823 case NL80211_IFTYPE_ADHOC:
1824 mode = NDIS_80211_INFRA_ADHOC;
1826 case NL80211_IFTYPE_STATION:
1827 mode = NDIS_80211_INFRA_INFRA;
1833 priv->wdev.iftype = type;
1835 return set_infra_mode(usbdev, mode);
1838 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1840 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1841 struct usbnet *usbdev = priv->usbdev;
1844 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1845 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1850 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1851 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1859 static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
1862 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1863 struct usbnet *usbdev = priv->usbdev;
1865 netdev_dbg(usbdev->net, "%s(): type:0x%x dbm:%i\n",
1866 __func__, type, dbm);
1868 /* Device doesn't support changing txpower after initialization, only
1869 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1872 if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
1873 if (!priv->radio_on)
1874 disassociate(usbdev, true); /* turn on radio */
1882 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1884 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1885 struct usbnet *usbdev = priv->usbdev;
1887 *dbm = get_bcm4320_power_dbm(priv);
1889 netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1894 #define SCAN_DELAY_JIFFIES (6 * HZ)
1895 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1896 struct cfg80211_scan_request *request)
1898 struct usbnet *usbdev = netdev_priv(dev);
1899 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1903 netdev_dbg(usbdev->net, "cfg80211.scan\n");
1905 /* Get current bssid list from device before new scan, as new scan
1906 * clears internal bssid list.
1908 rndis_check_bssid_list(usbdev);
1913 if (priv->scan_request && priv->scan_request != request)
1916 priv->scan_request = request;
1918 tmp = cpu_to_le32(1);
1919 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1922 /* Wait before retrieving scan results from device */
1923 queue_delayed_work(priv->workqueue, &priv->scan_work,
1924 SCAN_DELAY_JIFFIES);
1930 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1931 struct ndis_80211_bssid_ex *bssid)
1933 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1934 struct ieee80211_channel *channel;
1938 u16 beacon_interval;
1939 struct ndis_80211_fixed_ies *fixed;
1940 int ie_len, bssid_len;
1943 netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
1944 bssid->ssid.essid, bssid->mac);
1946 /* parse bssid structure */
1947 bssid_len = le32_to_cpu(bssid->length);
1949 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1950 sizeof(struct ndis_80211_fixed_ies))
1953 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1955 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1956 ie_len = min(bssid_len - (int)sizeof(*bssid),
1957 (int)le32_to_cpu(bssid->ie_length));
1958 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1962 /* extract data for cfg80211_inform_bss */
1963 channel = ieee80211_get_channel(priv->wdev.wiphy,
1964 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1968 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1969 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1970 capability = le16_to_cpu(fixed->capabilities);
1971 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1973 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1974 timestamp, capability, beacon_interval, ie, ie_len, signal,
1978 static int rndis_check_bssid_list(struct usbnet *usbdev)
1981 struct ndis_80211_bssid_list_ex *bssid_list;
1982 struct ndis_80211_bssid_ex *bssid;
1983 int ret = -EINVAL, len, count, bssid_len;
1984 bool resized = false;
1986 netdev_dbg(usbdev->net, "check_bssid_list\n");
1988 len = CONTROL_BUFFER_SIZE;
1990 buf = kmalloc(len, GFP_KERNEL);
1996 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2000 if (!resized && len > CONTROL_BUFFER_SIZE) {
2007 bssid = bssid_list->bssid;
2008 bssid_len = le32_to_cpu(bssid->length);
2009 count = le32_to_cpu(bssid_list->num_items);
2010 netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
2013 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
2014 rndis_bss_info_update(usbdev, bssid);
2016 bssid = (void *)bssid + bssid_len;
2017 bssid_len = le32_to_cpu(bssid->length);
2026 static void rndis_get_scan_results(struct work_struct *work)
2028 struct rndis_wlan_private *priv =
2029 container_of(work, struct rndis_wlan_private, scan_work.work);
2030 struct usbnet *usbdev = priv->usbdev;
2033 netdev_dbg(usbdev->net, "get_scan_results\n");
2035 if (!priv->scan_request)
2038 ret = rndis_check_bssid_list(usbdev);
2040 cfg80211_scan_done(priv->scan_request, ret < 0);
2042 priv->scan_request = NULL;
2045 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2046 struct cfg80211_connect_params *sme)
2048 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2049 struct usbnet *usbdev = priv->usbdev;
2050 struct ieee80211_channel *channel = sme->channel;
2051 struct ndis_80211_ssid ssid;
2052 int pairwise = RNDIS_WLAN_ALG_NONE;
2053 int groupwise = RNDIS_WLAN_ALG_NONE;
2054 int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2055 int length, i, ret, chan = -1;
2058 chan = ieee80211_frequency_to_channel(channel->center_freq);
2060 groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2061 for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2063 rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2065 if (sme->crypto.n_ciphers_pairwise > 0 &&
2066 pairwise == RNDIS_WLAN_ALG_NONE) {
2067 netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2071 for (i = 0; i < sme->crypto.n_akm_suites; i++)
2073 rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2075 if (sme->crypto.n_akm_suites > 0 &&
2076 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2077 netdev_err(usbdev->net, "Invalid keymgmt\n");
2081 netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2082 sme->ssid, sme->bssid, chan,
2083 sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2084 groupwise, pairwise, keymgmt);
2086 if (is_associated(usbdev))
2087 disassociate(usbdev, false);
2089 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2091 netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2093 goto err_turn_radio_on;
2096 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2099 netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2101 goto err_turn_radio_on;
2104 set_priv_filter(usbdev);
2106 ret = set_encr_mode(usbdev, pairwise, groupwise);
2108 netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2110 goto err_turn_radio_on;
2114 ret = set_channel(usbdev, chan);
2116 netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2118 goto err_turn_radio_on;
2122 if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2123 priv->encr_tx_key_index = sme->key_idx;
2124 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2126 netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2127 ret, sme->key_len, sme->key_idx);
2128 goto err_turn_radio_on;
2132 if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2133 !is_broadcast_ether_addr(sme->bssid)) {
2134 ret = set_bssid(usbdev, sme->bssid);
2136 netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2138 goto err_turn_radio_on;
2141 clear_bssid(usbdev);
2143 length = sme->ssid_len;
2144 if (length > NDIS_802_11_LENGTH_SSID)
2145 length = NDIS_802_11_LENGTH_SSID;
2147 memset(&ssid, 0, sizeof(ssid));
2148 ssid.length = cpu_to_le32(length);
2149 memcpy(ssid.essid, sme->ssid, length);
2151 /* Pause and purge rx queue, so we don't pass packets before
2152 * 'media connect'-indication.
2154 usbnet_pause_rx(usbdev);
2155 usbnet_purge_paused_rxq(usbdev);
2157 ret = set_essid(usbdev, &ssid);
2159 netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2163 disassociate(usbdev, true);
2168 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2171 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2172 struct usbnet *usbdev = priv->usbdev;
2174 netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2176 priv->connected = false;
2177 memset(priv->bssid, 0, ETH_ALEN);
2179 return deauthenticate(usbdev);
2182 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2183 struct cfg80211_ibss_params *params)
2185 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2186 struct usbnet *usbdev = priv->usbdev;
2187 struct ieee80211_channel *channel = params->channel;
2188 struct ndis_80211_ssid ssid;
2189 enum nl80211_auth_type auth_type;
2190 int ret, alg, length, chan = -1;
2193 chan = ieee80211_frequency_to_channel(channel->center_freq);
2195 /* TODO: How to handle ad-hoc encryption?
2196 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2197 * pre-shared for encryption (open/shared/wpa), is key set before
2198 * join_ibss? Which auth_type to use (not in params)? What about WPA?
2200 if (params->privacy) {
2201 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2202 alg = RNDIS_WLAN_ALG_WEP;
2204 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2205 alg = RNDIS_WLAN_ALG_NONE;
2208 netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2209 params->ssid, params->bssid, chan, params->privacy);
2211 if (is_associated(usbdev))
2212 disassociate(usbdev, false);
2214 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2216 netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2218 goto err_turn_radio_on;
2221 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2223 netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2225 goto err_turn_radio_on;
2228 set_priv_filter(usbdev);
2230 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2232 netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2234 goto err_turn_radio_on;
2238 ret = set_channel(usbdev, chan);
2240 netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2242 goto err_turn_radio_on;
2246 if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2247 !is_broadcast_ether_addr(params->bssid)) {
2248 ret = set_bssid(usbdev, params->bssid);
2250 netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2252 goto err_turn_radio_on;
2255 clear_bssid(usbdev);
2257 length = params->ssid_len;
2258 if (length > NDIS_802_11_LENGTH_SSID)
2259 length = NDIS_802_11_LENGTH_SSID;
2261 memset(&ssid, 0, sizeof(ssid));
2262 ssid.length = cpu_to_le32(length);
2263 memcpy(ssid.essid, params->ssid, length);
2265 /* Don't need to pause rx queue for ad-hoc. */
2266 usbnet_purge_paused_rxq(usbdev);
2267 usbnet_resume_rx(usbdev);
2269 ret = set_essid(usbdev, &ssid);
2271 netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2276 disassociate(usbdev, true);
2281 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2283 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2284 struct usbnet *usbdev = priv->usbdev;
2286 netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2288 priv->connected = false;
2289 memset(priv->bssid, 0, ETH_ALEN);
2291 return deauthenticate(usbdev);
2294 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
2295 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2297 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2298 struct usbnet *usbdev = priv->usbdev;
2300 return set_channel(usbdev,
2301 ieee80211_frequency_to_channel(chan->center_freq));
2304 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2305 u8 key_index, const u8 *mac_addr,
2306 struct key_params *params)
2308 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2309 struct usbnet *usbdev = priv->usbdev;
2312 netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2313 __func__, key_index, mac_addr, params->cipher);
2315 switch (params->cipher) {
2316 case WLAN_CIPHER_SUITE_WEP40:
2317 case WLAN_CIPHER_SUITE_WEP104:
2318 return add_wep_key(usbdev, params->key, params->key_len,
2320 case WLAN_CIPHER_SUITE_TKIP:
2321 case WLAN_CIPHER_SUITE_CCMP:
2324 if (params->seq && params->seq_len > 0)
2325 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2327 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2328 NDIS_80211_ADDKEY_TRANSMIT_KEY;
2330 return add_wpa_key(usbdev, params->key, params->key_len,
2331 key_index, mac_addr, params->seq,
2332 params->seq_len, params->cipher, flags);
2334 netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2335 __func__, params->cipher);
2340 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2341 u8 key_index, const u8 *mac_addr)
2343 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2344 struct usbnet *usbdev = priv->usbdev;
2346 netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2348 return remove_key(usbdev, key_index, mac_addr);
2351 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2354 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2355 struct usbnet *usbdev = priv->usbdev;
2356 struct rndis_wlan_encr_key key;
2358 netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2360 priv->encr_tx_key_index = key_index;
2362 key = priv->encr_keys[key_index];
2364 return add_wep_key(usbdev, key.material, key.len, key_index);
2367 static void rndis_fill_station_info(struct usbnet *usbdev,
2368 struct station_info *sinfo)
2370 __le32 linkspeed, rssi;
2373 memset(sinfo, 0, sizeof(*sinfo));
2375 len = sizeof(linkspeed);
2376 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2378 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2379 sinfo->filled |= STATION_INFO_TX_BITRATE;
2383 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2385 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2386 sinfo->filled |= STATION_INFO_SIGNAL;
2390 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2391 u8 *mac, struct station_info *sinfo)
2393 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2394 struct usbnet *usbdev = priv->usbdev;
2396 if (compare_ether_addr(priv->bssid, mac))
2399 rndis_fill_station_info(usbdev, sinfo);
2404 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2405 int idx, u8 *mac, struct station_info *sinfo)
2407 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2408 struct usbnet *usbdev = priv->usbdev;
2413 memcpy(mac, priv->bssid, ETH_ALEN);
2415 rndis_fill_station_info(usbdev, sinfo);
2420 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2421 struct cfg80211_pmksa *pmksa)
2423 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2424 struct usbnet *usbdev = priv->usbdev;
2425 struct ndis_80211_pmkid *pmkids;
2426 u32 *tmp = (u32 *)pmksa->pmkid;
2428 netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2430 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2431 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2433 pmkids = get_device_pmkids(usbdev);
2434 if (IS_ERR(pmkids)) {
2435 /* couldn't read PMKID cache from device */
2436 return PTR_ERR(pmkids);
2439 pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2440 if (IS_ERR(pmkids)) {
2441 /* not found, list full, etc */
2442 return PTR_ERR(pmkids);
2445 return set_device_pmkids(usbdev, pmkids);
2448 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2449 struct cfg80211_pmksa *pmksa)
2451 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2452 struct usbnet *usbdev = priv->usbdev;
2453 struct ndis_80211_pmkid *pmkids;
2454 u32 *tmp = (u32 *)pmksa->pmkid;
2456 netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2458 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2459 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2461 pmkids = get_device_pmkids(usbdev);
2462 if (IS_ERR(pmkids)) {
2463 /* Couldn't read PMKID cache from device */
2464 return PTR_ERR(pmkids);
2467 pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2468 if (IS_ERR(pmkids)) {
2469 /* not found, etc */
2470 return PTR_ERR(pmkids);
2473 return set_device_pmkids(usbdev, pmkids);
2476 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2478 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2479 struct usbnet *usbdev = priv->usbdev;
2480 struct ndis_80211_pmkid pmkid;
2482 netdev_dbg(usbdev->net, "%s()\n", __func__);
2484 memset(&pmkid, 0, sizeof(pmkid));
2486 pmkid.length = cpu_to_le32(sizeof(pmkid));
2487 pmkid.bssid_info_count = cpu_to_le32(0);
2489 return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
2493 * workers, indication handlers, device poller
2495 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2497 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2498 struct ndis_80211_assoc_info *info;
2499 u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
2501 int resp_ie_len, req_ie_len;
2502 u8 *req_ie, *resp_ie;
2504 bool roamed = false;
2506 if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2507 /* received media connect indication while connected, either
2508 * device reassociated with same AP or roamed to new. */
2517 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2518 memset(assoc_buf, 0, sizeof(assoc_buf));
2519 info = (void *)assoc_buf;
2521 /* Get association info IEs from device and send them back to
2523 ret = get_association_info(usbdev, info, sizeof(assoc_buf));
2525 req_ie_len = le32_to_cpu(info->req_ie_length);
2526 if (req_ie_len > 0) {
2527 offset = le32_to_cpu(info->offset_req_ies);
2528 req_ie = (u8 *)info + offset;
2531 resp_ie_len = le32_to_cpu(info->resp_ie_length);
2532 if (resp_ie_len > 0) {
2533 offset = le32_to_cpu(info->offset_resp_ies);
2534 resp_ie = (u8 *)info + offset;
2537 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2540 ret = get_bssid(usbdev, bssid);
2542 memset(bssid, 0, sizeof(bssid));
2544 netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2545 bssid, roamed ? " roamed" : "");
2547 /* Internal bss list in device always contains at least the currently
2548 * connected bss and we can get it to cfg80211 with
2549 * rndis_check_bssid_list().
2550 * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2553 rndis_check_bssid_list(usbdev);
2555 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2557 cfg80211_connect_result(usbdev->net, bssid, req_ie,
2558 req_ie_len, resp_ie,
2559 resp_ie_len, 0, GFP_KERNEL);
2561 cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2562 resp_ie, resp_ie_len, GFP_KERNEL);
2563 } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2564 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2566 priv->connected = true;
2567 memcpy(priv->bssid, bssid, ETH_ALEN);
2569 usbnet_resume_rx(usbdev);
2570 netif_carrier_on(usbdev->net);
2573 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2575 union iwreq_data evt;
2577 netif_carrier_off(usbdev->net);
2580 evt.data.length = 0;
2581 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2582 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2585 static void rndis_wlan_worker(struct work_struct *work)
2587 struct rndis_wlan_private *priv =
2588 container_of(work, struct rndis_wlan_private, work);
2589 struct usbnet *usbdev = priv->usbdev;
2591 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2592 rndis_wlan_do_link_up_work(usbdev);
2594 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2595 rndis_wlan_do_link_down_work(usbdev);
2597 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2598 set_multicast_list(usbdev);
2601 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2603 struct usbnet *usbdev = netdev_priv(dev);
2604 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2606 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2609 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2610 queue_work(priv->workqueue, &priv->work);
2613 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2614 struct ndis_80211_status_indication *indication,
2619 int flags, buflen, key_id;
2620 bool pairwise_error, group_error;
2621 struct ndis_80211_auth_request *auth_req;
2622 enum nl80211_key_type key_type;
2624 /* must have at least one array entry */
2625 if (len < offsetof(struct ndis_80211_status_indication, u) +
2626 sizeof(struct ndis_80211_auth_request)) {
2627 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2632 buf = (void *)&indication->u.auth_request[0];
2633 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2635 while (buflen >= sizeof(*auth_req)) {
2636 auth_req = (void *)buf;
2638 flags = le32_to_cpu(auth_req->flags);
2639 pairwise_error = false;
2640 group_error = false;
2643 type = "reauth request";
2645 type = "key update request";
2647 pairwise_error = true;
2648 type = "pairwise_error";
2652 type = "group_error";
2655 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2656 type, le32_to_cpu(auth_req->flags));
2658 if (pairwise_error) {
2659 key_type = NL80211_KEYTYPE_PAIRWISE;
2662 cfg80211_michael_mic_failure(usbdev->net,
2664 key_type, key_id, NULL,
2669 key_type = NL80211_KEYTYPE_GROUP;
2672 cfg80211_michael_mic_failure(usbdev->net,
2674 key_type, key_id, NULL,
2678 buflen -= le32_to_cpu(auth_req->length);
2679 buf += le32_to_cpu(auth_req->length);
2683 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2684 struct ndis_80211_status_indication *indication,
2687 struct ndis_80211_pmkid_cand_list *cand_list;
2688 int list_len, expected_len, i;
2690 if (len < offsetof(struct ndis_80211_status_indication, u) +
2691 sizeof(struct ndis_80211_pmkid_cand_list)) {
2692 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2697 list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2698 sizeof(struct ndis_80211_pmkid_candidate);
2699 expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2700 offsetof(struct ndis_80211_status_indication, u);
2702 if (len < expected_len) {
2703 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2708 cand_list = &indication->u.cand_list;
2710 netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2711 le32_to_cpu(cand_list->version),
2712 le32_to_cpu(cand_list->num_candidates));
2714 if (le32_to_cpu(cand_list->version) != 1)
2717 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2718 struct ndis_80211_pmkid_candidate *cand =
2719 &cand_list->candidate_list[i];
2721 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2722 i, le32_to_cpu(cand->flags), cand->bssid);
2725 struct iw_pmkid_cand pcand;
2726 union iwreq_data wrqu;
2728 memset(&pcand, 0, sizeof(pcand));
2729 if (le32_to_cpu(cand->flags) & 0x01)
2730 pcand.flags |= IW_PMKID_CAND_PREAUTH;
2732 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2734 memset(&wrqu, 0, sizeof(wrqu));
2735 wrqu.data.length = sizeof(pcand);
2736 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2742 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2743 struct rndis_indicate *msg, int buflen)
2745 struct ndis_80211_status_indication *indication;
2748 offset = offsetof(struct rndis_indicate, status) +
2749 le32_to_cpu(msg->offset);
2750 len = le32_to_cpu(msg->length);
2753 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2758 if (offset + len > buflen) {
2759 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2760 offset + len, buflen);
2764 indication = (void *)((u8 *)msg + offset);
2766 switch (le32_to_cpu(indication->status_type)) {
2767 case NDIS_80211_STATUSTYPE_RADIOSTATE:
2768 netdev_info(usbdev->net, "radio state indication: %i\n",
2769 le32_to_cpu(indication->u.radio_status));
2772 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2773 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2774 le32_to_cpu(indication->u.media_stream_mode));
2777 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2778 rndis_wlan_auth_indication(usbdev, indication, len);
2781 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2782 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2786 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2787 le32_to_cpu(indication->status_type));
2791 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2793 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2794 struct rndis_indicate *msg = ind;
2796 switch (msg->status) {
2797 case RNDIS_STATUS_MEDIA_CONNECT:
2798 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2799 /* OID_802_11_ADD_KEY causes sometimes extra
2800 * "media connect" indications which confuses driver
2801 * and userspace to think that device is
2802 * roaming/reassociating when it isn't.
2804 netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
2808 usbnet_pause_rx(usbdev);
2810 netdev_info(usbdev->net, "media connect\n");
2812 /* queue work to avoid recursive calls into rndis_command */
2813 set_bit(WORK_LINK_UP, &priv->work_pending);
2814 queue_work(priv->workqueue, &priv->work);
2817 case RNDIS_STATUS_MEDIA_DISCONNECT:
2818 netdev_info(usbdev->net, "media disconnect\n");
2820 /* queue work to avoid recursive calls into rndis_command */
2821 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2822 queue_work(priv->workqueue, &priv->work);
2825 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2826 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2830 netdev_info(usbdev->net, "indication: 0x%08x\n",
2831 le32_to_cpu(msg->status));
2836 static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
2841 } networks_supported;
2842 struct ndis_80211_capability *caps;
2843 u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
2844 int len, retval, i, n;
2845 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2847 /* determine supported modes */
2848 len = sizeof(networks_supported);
2849 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2850 &networks_supported, &len);
2852 n = le32_to_cpu(networks_supported.num_items);
2855 for (i = 0; i < n; i++) {
2856 switch (le32_to_cpu(networks_supported.items[i])) {
2857 case NDIS_80211_TYPE_FREQ_HOP:
2858 case NDIS_80211_TYPE_DIRECT_SEQ:
2859 priv->caps |= CAP_MODE_80211B;
2861 case NDIS_80211_TYPE_OFDM_A:
2862 priv->caps |= CAP_MODE_80211A;
2864 case NDIS_80211_TYPE_OFDM_G:
2865 priv->caps |= CAP_MODE_80211G;
2871 /* get device 802.11 capabilities, number of PMKIDs */
2872 caps = (struct ndis_80211_capability *)caps_buf;
2873 len = sizeof(caps_buf);
2874 retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
2876 netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
2877 "ver %d, pmkids %d, auth-encr-pairs %d\n",
2878 le32_to_cpu(caps->length),
2879 le32_to_cpu(caps->version),
2880 le32_to_cpu(caps->num_pmkids),
2881 le32_to_cpu(caps->num_auth_encr_pair));
2882 wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
2884 wiphy->max_num_pmkids = 0;
2889 #define DEVICE_POLLER_JIFFIES (HZ)
2890 static void rndis_device_poller(struct work_struct *work)
2892 struct rndis_wlan_private *priv =
2893 container_of(work, struct rndis_wlan_private,
2894 dev_poller_work.work);
2895 struct usbnet *usbdev = priv->usbdev;
2898 int update_jiffies = DEVICE_POLLER_JIFFIES;
2901 /* Only check/do workaround when connected. Calling is_associated()
2902 * also polls device with rndis_command() and catches for media link
2905 if (!is_associated(usbdev))
2909 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2911 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
2913 netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
2914 ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
2916 /* Workaround transfer stalls on poor quality links.
2917 * TODO: find right way to fix these stalls (as stalls do not happen
2918 * with ndiswrapper/windows driver). */
2919 if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
2920 /* Decrease stats worker interval to catch stalls.
2921 * faster. Faster than 400-500ms causes packet loss,
2922 * Slower doesn't catch stalls fast enough.
2924 j = msecs_to_jiffies(priv->param_workaround_interval);
2925 if (j > DEVICE_POLLER_JIFFIES)
2926 j = DEVICE_POLLER_JIFFIES;
2931 /* Send scan OID. Use of both OIDs is required to get device
2934 tmp = cpu_to_le32(1);
2935 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2938 len = CONTROL_BUFFER_SIZE;
2939 buf = kmalloc(len, GFP_KERNEL);
2943 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2948 if (update_jiffies >= HZ)
2949 update_jiffies = round_jiffies_relative(update_jiffies);
2951 j = round_jiffies_relative(update_jiffies);
2952 if (abs(j - update_jiffies) <= 10)
2956 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2961 * driver/device initialization
2963 static void rndis_copy_module_params(struct usbnet *usbdev)
2965 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2967 priv->param_country[0] = modparam_country[0];
2968 priv->param_country[1] = modparam_country[1];
2969 priv->param_country[2] = 0;
2970 priv->param_frameburst = modparam_frameburst;
2971 priv->param_afterburner = modparam_afterburner;
2972 priv->param_power_save = modparam_power_save;
2973 priv->param_power_output = modparam_power_output;
2974 priv->param_roamtrigger = modparam_roamtrigger;
2975 priv->param_roamdelta = modparam_roamdelta;
2977 priv->param_country[0] = toupper(priv->param_country[0]);
2978 priv->param_country[1] = toupper(priv->param_country[1]);
2979 /* doesn't support EU as country code, use FI instead */
2980 if (!strcmp(priv->param_country, "EU"))
2981 strcpy(priv->param_country, "FI");
2983 if (priv->param_power_save < 0)
2984 priv->param_power_save = 0;
2985 else if (priv->param_power_save > 2)
2986 priv->param_power_save = 2;
2988 if (priv->param_power_output < 0)
2989 priv->param_power_output = 0;
2990 else if (priv->param_power_output > 3)
2991 priv->param_power_output = 3;
2993 if (priv->param_roamtrigger < -80)
2994 priv->param_roamtrigger = -80;
2995 else if (priv->param_roamtrigger > -60)
2996 priv->param_roamtrigger = -60;
2998 if (priv->param_roamdelta < 0)
2999 priv->param_roamdelta = 0;
3000 else if (priv->param_roamdelta > 2)
3001 priv->param_roamdelta = 2;
3003 if (modparam_workaround_interval < 0)
3004 priv->param_workaround_interval = 500;
3006 priv->param_workaround_interval = modparam_workaround_interval;
3009 static int bcm4320a_early_init(struct usbnet *usbdev)
3011 /* copy module parameters for bcm4320a so that iwconfig reports txpower
3012 * and workaround parameter is copied to private structure correctly.
3014 rndis_copy_module_params(usbdev);
3016 /* bcm4320a doesn't handle configuration parameters well. Try
3017 * set any and you get partially zeroed mac and broken device.
3023 static int bcm4320b_early_init(struct usbnet *usbdev)
3025 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3028 rndis_copy_module_params(usbdev);
3030 /* Early initialization settings, setting these won't have effect
3031 * if called after generic_rndis_bind().
3034 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3035 rndis_set_config_parameter_str(usbdev, "FrameBursting",
3036 priv->param_frameburst ? "1" : "0");
3037 rndis_set_config_parameter_str(usbdev, "Afterburner",
3038 priv->param_afterburner ? "1" : "0");
3039 sprintf(buf, "%d", priv->param_power_save);
3040 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3041 sprintf(buf, "%d", priv->param_power_output);
3042 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3043 sprintf(buf, "%d", priv->param_roamtrigger);
3044 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3045 sprintf(buf, "%d", priv->param_roamdelta);
3046 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3051 /* same as rndis_netdev_ops but with local multicast handler */
3052 static const struct net_device_ops rndis_wlan_netdev_ops = {
3053 .ndo_open = usbnet_open,
3054 .ndo_stop = usbnet_stop,
3055 .ndo_start_xmit = usbnet_start_xmit,
3056 .ndo_tx_timeout = usbnet_tx_timeout,
3057 .ndo_set_mac_address = eth_mac_addr,
3058 .ndo_validate_addr = eth_validate_addr,
3059 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3062 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3064 struct wiphy *wiphy;
3065 struct rndis_wlan_private *priv;
3069 /* allocate wiphy and rndis private data
3070 * NOTE: We only support a single virtual interface, so wiphy
3071 * and wireless_dev are somewhat synonymous for this device.
3073 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3077 priv = wiphy_priv(wiphy);
3078 usbdev->net->ieee80211_ptr = &priv->wdev;
3079 priv->wdev.wiphy = wiphy;
3080 priv->wdev.iftype = NL80211_IFTYPE_STATION;
3082 /* These have to be initialized before calling generic_rndis_bind().
3083 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3085 usbdev->driver_priv = priv;
3086 priv->usbdev = usbdev;
3088 mutex_init(&priv->command_lock);
3090 /* because rndis_command() sleeps we need to use workqueue */
3091 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3092 INIT_WORK(&priv->work, rndis_wlan_worker);
3093 INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3094 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3096 /* try bind rndis_host */
3097 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3101 /* generic_rndis_bind set packet filter to multicast_all+
3102 * promisc mode which doesn't work well for our devices (device
3103 * picks up rssi to closest station instead of to access point).
3105 * rndis_host wants to avoid all OID as much as possible
3106 * so do promisc/multicast handling in rndis_wlan.
3108 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3110 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3111 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3115 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3117 priv->multicast_size = le32_to_cpu(tmp);
3118 if (retval < 0 || priv->multicast_size < 0)
3119 priv->multicast_size = 0;
3120 if (priv->multicast_size > 0)
3121 usbdev->net->flags |= IFF_MULTICAST;
3123 usbdev->net->flags &= ~IFF_MULTICAST;
3125 /* fill-out wiphy structure and register w/ cfg80211 */
3126 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3127 wiphy->privid = rndis_wiphy_privid;
3128 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3129 | BIT(NL80211_IFTYPE_ADHOC);
3130 wiphy->max_scan_ssids = 1;
3132 /* TODO: fill-out band/encr information based on priv->caps */
3133 rndis_wlan_get_caps(usbdev, wiphy);
3135 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3136 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3137 priv->band.channels = priv->channels;
3138 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3139 priv->band.bitrates = priv->rates;
3140 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3141 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3142 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3144 memcpy(priv->cipher_suites, rndis_cipher_suites,
3145 sizeof(rndis_cipher_suites));
3146 wiphy->cipher_suites = priv->cipher_suites;
3147 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3149 set_wiphy_dev(wiphy, &usbdev->udev->dev);
3151 if (wiphy_register(wiphy)) {
3156 set_default_iw_params(usbdev);
3158 /* set default rts/frag */
3159 rndis_set_wiphy_params(wiphy,
3160 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3163 priv->radio_on = true;
3164 disassociate(usbdev, true);
3165 netif_carrier_off(usbdev->net);
3170 cancel_delayed_work_sync(&priv->dev_poller_work);
3171 cancel_delayed_work_sync(&priv->scan_work);
3172 cancel_work_sync(&priv->work);
3173 flush_workqueue(priv->workqueue);
3174 destroy_workqueue(priv->workqueue);
3180 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3182 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3184 /* turn radio off */
3185 disassociate(usbdev, false);
3187 cancel_delayed_work_sync(&priv->dev_poller_work);
3188 cancel_delayed_work_sync(&priv->scan_work);
3189 cancel_work_sync(&priv->work);
3190 flush_workqueue(priv->workqueue);
3191 destroy_workqueue(priv->workqueue);
3193 rndis_unbind(usbdev, intf);
3195 wiphy_unregister(priv->wdev.wiphy);
3196 wiphy_free(priv->wdev.wiphy);
3199 static int rndis_wlan_reset(struct usbnet *usbdev)
3201 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3204 netdev_dbg(usbdev->net, "%s()\n", __func__);
3206 retval = rndis_reset(usbdev);
3208 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3210 /* rndis_reset cleared multicast list, so restore here.
3211 (set_multicast_list() also turns on current packet filter) */
3212 set_multicast_list(usbdev);
3214 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3215 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3217 return deauthenticate(usbdev);
3220 static int rndis_wlan_stop(struct usbnet *usbdev)
3222 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3226 netdev_dbg(usbdev->net, "%s()\n", __func__);
3228 retval = disassociate(usbdev, false);
3230 priv->work_pending = 0;
3231 cancel_delayed_work_sync(&priv->dev_poller_work);
3232 cancel_delayed_work_sync(&priv->scan_work);
3233 cancel_work_sync(&priv->work);
3234 flush_workqueue(priv->workqueue);
3236 if (priv->scan_request) {
3237 cfg80211_scan_done(priv->scan_request, true);
3238 priv->scan_request = NULL;
3241 /* Set current packet filter zero to block receiving data packets from
3244 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3250 static const struct driver_info bcm4320b_info = {
3251 .description = "Wireless RNDIS device, BCM4320b based",
3252 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3253 FLAG_AVOID_UNLINK_URBS,
3254 .bind = rndis_wlan_bind,
3255 .unbind = rndis_wlan_unbind,
3256 .status = rndis_status,
3257 .rx_fixup = rndis_rx_fixup,
3258 .tx_fixup = rndis_tx_fixup,
3259 .reset = rndis_wlan_reset,
3260 .stop = rndis_wlan_stop,
3261 .early_init = bcm4320b_early_init,
3262 .indication = rndis_wlan_indication,
3265 static const struct driver_info bcm4320a_info = {
3266 .description = "Wireless RNDIS device, BCM4320a based",
3267 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3268 FLAG_AVOID_UNLINK_URBS,
3269 .bind = rndis_wlan_bind,
3270 .unbind = rndis_wlan_unbind,
3271 .status = rndis_status,
3272 .rx_fixup = rndis_rx_fixup,
3273 .tx_fixup = rndis_tx_fixup,
3274 .reset = rndis_wlan_reset,
3275 .stop = rndis_wlan_stop,
3276 .early_init = bcm4320a_early_init,
3277 .indication = rndis_wlan_indication,
3280 static const struct driver_info rndis_wlan_info = {
3281 .description = "Wireless RNDIS device",
3282 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3283 FLAG_AVOID_UNLINK_URBS,
3284 .bind = rndis_wlan_bind,
3285 .unbind = rndis_wlan_unbind,
3286 .status = rndis_status,
3287 .rx_fixup = rndis_rx_fixup,
3288 .tx_fixup = rndis_tx_fixup,
3289 .reset = rndis_wlan_reset,
3290 .stop = rndis_wlan_stop,
3291 .early_init = bcm4320a_early_init,
3292 .indication = rndis_wlan_indication,
3295 /*-------------------------------------------------------------------------*/
3297 static const struct usb_device_id products [] = {
3298 #define RNDIS_MASTER_INTERFACE \
3299 .bInterfaceClass = USB_CLASS_COMM, \
3300 .bInterfaceSubClass = 2 /* ACM */, \
3301 .bInterfaceProtocol = 0x0ff
3303 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3304 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3307 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3308 | USB_DEVICE_ID_MATCH_DEVICE,
3310 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
3311 RNDIS_MASTER_INTERFACE,
3312 .driver_info = (unsigned long) &bcm4320b_info,
3314 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3315 | USB_DEVICE_ID_MATCH_DEVICE,
3317 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
3318 RNDIS_MASTER_INTERFACE,
3319 .driver_info = (unsigned long) &bcm4320b_info,
3321 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3322 | USB_DEVICE_ID_MATCH_DEVICE,
3324 .idProduct = 0x011b, /* Belkin F5D7051 */
3325 RNDIS_MASTER_INTERFACE,
3326 .driver_info = (unsigned long) &bcm4320b_info,
3328 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3329 | USB_DEVICE_ID_MATCH_DEVICE,
3330 .idVendor = 0x1799, /* Belkin has two vendor ids */
3331 .idProduct = 0x011b, /* Belkin F5D7051 */
3332 RNDIS_MASTER_INTERFACE,
3333 .driver_info = (unsigned long) &bcm4320b_info,
3335 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3336 | USB_DEVICE_ID_MATCH_DEVICE,
3338 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
3339 RNDIS_MASTER_INTERFACE,
3340 .driver_info = (unsigned long) &bcm4320b_info,
3342 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3343 | USB_DEVICE_ID_MATCH_DEVICE,
3345 .idProduct = 0x0026, /* Linksys WUSB54GSC */
3346 RNDIS_MASTER_INTERFACE,
3347 .driver_info = (unsigned long) &bcm4320b_info,
3349 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3350 | USB_DEVICE_ID_MATCH_DEVICE,
3352 .idProduct = 0x1717, /* Asus WL169gE */
3353 RNDIS_MASTER_INTERFACE,
3354 .driver_info = (unsigned long) &bcm4320b_info,
3356 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3357 | USB_DEVICE_ID_MATCH_DEVICE,
3359 .idProduct = 0xd11b, /* Eminent EM4045 */
3360 RNDIS_MASTER_INTERFACE,
3361 .driver_info = (unsigned long) &bcm4320b_info,
3363 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3364 | USB_DEVICE_ID_MATCH_DEVICE,
3366 .idProduct = 0x0715, /* BT Voyager 1055 */
3367 RNDIS_MASTER_INTERFACE,
3368 .driver_info = (unsigned long) &bcm4320b_info,
3370 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3371 * parameters available, hardware probably contain older firmware version with
3372 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3375 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3376 | USB_DEVICE_ID_MATCH_DEVICE,
3378 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3379 RNDIS_MASTER_INTERFACE,
3380 .driver_info = (unsigned long) &bcm4320a_info,
3382 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3383 | USB_DEVICE_ID_MATCH_DEVICE,
3385 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3386 RNDIS_MASTER_INTERFACE,
3387 .driver_info = (unsigned long) &bcm4320a_info,
3389 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3390 | USB_DEVICE_ID_MATCH_DEVICE,
3392 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3393 RNDIS_MASTER_INTERFACE,
3394 .driver_info = (unsigned long) &bcm4320a_info,
3396 /* Generic Wireless RNDIS devices that we don't have exact
3397 * idVendor/idProduct/chip yet.
3400 /* RNDIS is MSFT's un-official variant of CDC ACM */
3401 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3402 .driver_info = (unsigned long) &rndis_wlan_info,
3404 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3405 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3406 .driver_info = (unsigned long) &rndis_wlan_info,
3410 MODULE_DEVICE_TABLE(usb, products);
3412 static struct usb_driver rndis_wlan_driver = {
3413 .name = "rndis_wlan",
3414 .id_table = products,
3415 .probe = usbnet_probe,
3416 .disconnect = usbnet_disconnect,
3417 .suspend = usbnet_suspend,
3418 .resume = usbnet_resume,
3421 static int __init rndis_wlan_init(void)
3423 return usb_register(&rndis_wlan_driver);
3425 module_init(rndis_wlan_init);
3427 static void __exit rndis_wlan_exit(void)
3429 usb_deregister(&rndis_wlan_driver);
3431 module_exit(rndis_wlan_exit);
3433 MODULE_AUTHOR("Bjorge Dijkstra");
3434 MODULE_AUTHOR("Jussi Kivilinna");
3435 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3436 MODULE_LICENSE("GPL");