GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / rndis_wlan.c
blob1e143b0ca84f95866e63aa21f1207c42807b248d
1 /*
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,
89 int, 0444);
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
146 * 25% : 5 mW ~ 7dBm
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,
183 NDIS_80211_AUTH_WPA,
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 {
237 __le32 length;
238 u8 bssid[6];
239 u8 padding[2];
240 __le32 flags;
241 } __packed;
243 struct ndis_80211_pmkid_candidate {
244 u8 bssid[6];
245 u8 padding[2];
246 __le32 flags;
247 } __packed;
249 struct ndis_80211_pmkid_cand_list {
250 __le32 version;
251 __le32 num_candidates;
252 struct ndis_80211_pmkid_candidate candidate_list[0];
253 } __packed;
255 struct ndis_80211_status_indication {
256 __le32 status_type;
257 union {
258 __le32 media_stream_mode;
259 __le32 radio_status;
260 struct ndis_80211_auth_request auth_request[0];
261 struct ndis_80211_pmkid_cand_list cand_list;
262 } u;
263 } __packed;
265 struct ndis_80211_ssid {
266 __le32 length;
267 u8 essid[NDIS_802_11_LENGTH_SSID];
268 } __packed;
270 struct ndis_80211_conf_freq_hop {
271 __le32 length;
272 __le32 hop_pattern;
273 __le32 hop_set;
274 __le32 dwell_time;
275 } __packed;
277 struct ndis_80211_conf {
278 __le32 length;
279 __le32 beacon_period;
280 __le32 atim_window;
281 __le32 ds_config;
282 struct ndis_80211_conf_freq_hop fh_config;
283 } __packed;
285 struct ndis_80211_bssid_ex {
286 __le32 length;
287 u8 mac[6];
288 u8 padding[2];
289 struct ndis_80211_ssid ssid;
290 __le32 privacy;
291 __le32 rssi;
292 __le32 net_type;
293 struct ndis_80211_conf config;
294 __le32 net_infra;
295 u8 rates[NDIS_802_11_LENGTH_RATES_EX];
296 __le32 ie_length;
297 u8 ies[0];
298 } __packed;
300 struct ndis_80211_bssid_list_ex {
301 __le32 num_items;
302 struct ndis_80211_bssid_ex bssid[0];
303 } __packed;
305 struct ndis_80211_fixed_ies {
306 u8 timestamp[8];
307 __le16 beacon_interval;
308 __le16 capabilities;
309 } __packed;
311 struct ndis_80211_wep_key {
312 __le32 size;
313 __le32 index;
314 __le32 length;
315 u8 material[32];
316 } __packed;
318 struct ndis_80211_key {
319 __le32 size;
320 __le32 index;
321 __le32 length;
322 u8 bssid[6];
323 u8 padding[6];
324 u8 rsc[8];
325 u8 material[32];
326 } __packed;
328 struct ndis_80211_remove_key {
329 __le32 size;
330 __le32 index;
331 u8 bssid[6];
332 u8 padding[2];
333 } __packed;
335 struct ndis_config_param {
336 __le32 name_offs;
337 __le32 name_length;
338 __le32 type;
339 __le32 value_offs;
340 __le32 value_length;
341 } __packed;
343 struct ndis_80211_assoc_info {
344 __le32 length;
345 __le16 req_ies;
346 struct req_ie {
347 __le16 capa;
348 __le16 listen_interval;
349 u8 cur_ap_address[6];
350 } req_ie;
351 __le32 req_ie_length;
352 __le32 offset_req_ies;
353 __le16 resp_ies;
354 struct resp_ie {
355 __le16 capa;
356 __le16 status_code;
357 __le16 assoc_id;
358 } resp_ie;
359 __le32 resp_ie_length;
360 __le32 offset_resp_ies;
361 } __packed;
363 struct ndis_80211_auth_encr_pair {
364 __le32 auth_mode;
365 __le32 encr_mode;
366 } __packed;
368 struct ndis_80211_capability {
369 __le32 length;
370 __le32 version;
371 __le32 num_pmkids;
372 __le32 num_auth_encr_pair;
373 struct ndis_80211_auth_encr_pair auth_encr_pair[0];
374 } __packed;
376 struct ndis_80211_bssid_info {
377 u8 bssid[6];
378 u8 pmkid[16];
381 struct ndis_80211_pmkid {
382 __le32 length;
383 __le32 bssid_info_count;
384 struct ndis_80211_bssid_info bssid_info[0];
388 * private data
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[] = {
430 { .bitrate = 10 },
431 { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
432 { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
433 { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
434 { .bitrate = 60 },
435 { .bitrate = 90 },
436 { .bitrate = 120 },
437 { .bitrate = 180 },
438 { .bitrate = 240 },
439 { .bitrate = 360 },
440 { .bitrate = 480 },
441 { .bitrate = 540 }
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 {
452 int len;
453 u32 cipher;
454 u8 material[32];
455 u8 bssid[ETH_ALEN];
456 bool pairwise;
457 bool tx_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;
474 int last_qual;
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)];
481 int caps;
482 int multicast_size;
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;
491 int param_roamdelta;
492 u32 param_workaround_interval;
494 /* hardware state */
495 bool radio_on;
496 int infra_mode;
497 bool connected;
498 u8 bssid[ETH_ALEN];
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];
505 int wpa_version;
507 u8 command_buffer[COMMAND_BUFFER_SIZE];
511 * cfg80211 ops
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,
524 enum nl80211_tx_power_setting type,
525 int mbm);
526 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
528 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
529 struct cfg80211_connect_params *sme);
531 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
532 u16 reason_code);
534 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
535 struct cfg80211_ibss_params *params);
537 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
539 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
540 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
542 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
543 u8 key_index, const u8 *mac_addr,
544 struct key_params *params);
546 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
547 u8 key_index, const u8 *mac_addr);
549 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
550 u8 key_index);
552 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
553 u8 *mac, struct station_info *sinfo);
555 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
556 int idx, u8 *mac, struct station_info *sinfo);
558 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
559 struct cfg80211_pmksa *pmksa);
561 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
562 struct cfg80211_pmksa *pmksa);
564 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
566 static struct cfg80211_ops rndis_config_ops = {
567 .change_virtual_intf = rndis_change_virtual_intf,
568 .scan = rndis_scan,
569 .set_wiphy_params = rndis_set_wiphy_params,
570 .set_tx_power = rndis_set_tx_power,
571 .get_tx_power = rndis_get_tx_power,
572 .connect = rndis_connect,
573 .disconnect = rndis_disconnect,
574 .join_ibss = rndis_join_ibss,
575 .leave_ibss = rndis_leave_ibss,
576 .set_channel = rndis_set_channel,
577 .add_key = rndis_add_key,
578 .del_key = rndis_del_key,
579 .set_default_key = rndis_set_default_key,
580 .get_station = rndis_get_station,
581 .dump_station = rndis_dump_station,
582 .set_pmksa = rndis_set_pmksa,
583 .del_pmksa = rndis_del_pmksa,
584 .flush_pmksa = rndis_flush_pmksa,
587 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
590 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
592 return (struct rndis_wlan_private *)dev->driver_priv;
595 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
597 switch (priv->param_power_output) {
598 default:
599 case 3:
600 return BCM4320_DEFAULT_TXPOWER_DBM_100;
601 case 2:
602 return BCM4320_DEFAULT_TXPOWER_DBM_75;
603 case 1:
604 return BCM4320_DEFAULT_TXPOWER_DBM_50;
605 case 0:
606 return BCM4320_DEFAULT_TXPOWER_DBM_25;
610 static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
612 int cipher = priv->encr_keys[idx].cipher;
614 return (cipher == WLAN_CIPHER_SUITE_CCMP ||
615 cipher == WLAN_CIPHER_SUITE_TKIP);
618 static int rndis_cipher_to_alg(u32 cipher)
620 switch (cipher) {
621 default:
622 return RNDIS_WLAN_ALG_NONE;
623 case WLAN_CIPHER_SUITE_WEP40:
624 case WLAN_CIPHER_SUITE_WEP104:
625 return RNDIS_WLAN_ALG_WEP;
626 case WLAN_CIPHER_SUITE_TKIP:
627 return RNDIS_WLAN_ALG_TKIP;
628 case WLAN_CIPHER_SUITE_CCMP:
629 return RNDIS_WLAN_ALG_CCMP;
633 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
635 switch (akm_suite) {
636 default:
637 return RNDIS_WLAN_KEY_MGMT_NONE;
638 case WLAN_AKM_SUITE_8021X:
639 return RNDIS_WLAN_KEY_MGMT_802_1X;
640 case WLAN_AKM_SUITE_PSK:
641 return RNDIS_WLAN_KEY_MGMT_PSK;
645 #ifdef DEBUG
646 static const char *oid_to_string(__le32 oid)
648 switch (oid) {
649 #define OID_STR(oid) case oid: return(#oid)
650 /* from rndis_host.h */
651 OID_STR(OID_802_3_PERMANENT_ADDRESS);
652 OID_STR(OID_GEN_MAXIMUM_FRAME_SIZE);
653 OID_STR(OID_GEN_CURRENT_PACKET_FILTER);
654 OID_STR(OID_GEN_PHYSICAL_MEDIUM);
656 /* from rndis_wlan.c */
657 OID_STR(OID_GEN_LINK_SPEED);
658 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
660 OID_STR(OID_GEN_XMIT_OK);
661 OID_STR(OID_GEN_RCV_OK);
662 OID_STR(OID_GEN_XMIT_ERROR);
663 OID_STR(OID_GEN_RCV_ERROR);
664 OID_STR(OID_GEN_RCV_NO_BUFFER);
666 OID_STR(OID_802_3_CURRENT_ADDRESS);
667 OID_STR(OID_802_3_MULTICAST_LIST);
668 OID_STR(OID_802_3_MAXIMUM_LIST_SIZE);
670 OID_STR(OID_802_11_BSSID);
671 OID_STR(OID_802_11_SSID);
672 OID_STR(OID_802_11_INFRASTRUCTURE_MODE);
673 OID_STR(OID_802_11_ADD_WEP);
674 OID_STR(OID_802_11_REMOVE_WEP);
675 OID_STR(OID_802_11_DISASSOCIATE);
676 OID_STR(OID_802_11_AUTHENTICATION_MODE);
677 OID_STR(OID_802_11_PRIVACY_FILTER);
678 OID_STR(OID_802_11_BSSID_LIST_SCAN);
679 OID_STR(OID_802_11_ENCRYPTION_STATUS);
680 OID_STR(OID_802_11_ADD_KEY);
681 OID_STR(OID_802_11_REMOVE_KEY);
682 OID_STR(OID_802_11_ASSOCIATION_INFORMATION);
683 OID_STR(OID_802_11_PMKID);
684 OID_STR(OID_802_11_NETWORK_TYPES_SUPPORTED);
685 OID_STR(OID_802_11_NETWORK_TYPE_IN_USE);
686 OID_STR(OID_802_11_TX_POWER_LEVEL);
687 OID_STR(OID_802_11_RSSI);
688 OID_STR(OID_802_11_RSSI_TRIGGER);
689 OID_STR(OID_802_11_FRAGMENTATION_THRESHOLD);
690 OID_STR(OID_802_11_RTS_THRESHOLD);
691 OID_STR(OID_802_11_SUPPORTED_RATES);
692 OID_STR(OID_802_11_CONFIGURATION);
693 OID_STR(OID_802_11_BSSID_LIST);
694 #undef OID_STR
697 return "?";
699 #else
700 static const char *oid_to_string(__le32 oid)
702 return "?";
704 #endif
706 /* translate error code */
707 static int rndis_error_status(__le32 rndis_status)
709 int ret = -EINVAL;
710 switch (rndis_status) {
711 case RNDIS_STATUS_SUCCESS:
712 ret = 0;
713 break;
714 case RNDIS_STATUS_FAILURE:
715 case RNDIS_STATUS_INVALID_DATA:
716 ret = -EINVAL;
717 break;
718 case RNDIS_STATUS_NOT_SUPPORTED:
719 ret = -EOPNOTSUPP;
720 break;
721 case RNDIS_STATUS_ADAPTER_NOT_READY:
722 case RNDIS_STATUS_ADAPTER_NOT_OPEN:
723 ret = -EBUSY;
724 break;
726 return ret;
729 static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
731 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
732 union {
733 void *buf;
734 struct rndis_msg_hdr *header;
735 struct rndis_query *get;
736 struct rndis_query_c *get_c;
737 } u;
738 int ret, buflen;
739 int resplen, respoffs, copylen;
741 buflen = *len + sizeof(*u.get);
742 if (buflen < CONTROL_BUFFER_SIZE)
743 buflen = CONTROL_BUFFER_SIZE;
745 if (buflen > COMMAND_BUFFER_SIZE) {
746 u.buf = kmalloc(buflen, GFP_KERNEL);
747 if (!u.buf)
748 return -ENOMEM;
749 } else {
750 u.buf = priv->command_buffer;
753 mutex_lock(&priv->command_lock);
755 memset(u.get, 0, sizeof *u.get);
756 u.get->msg_type = RNDIS_MSG_QUERY;
757 u.get->msg_len = cpu_to_le32(sizeof *u.get);
758 u.get->oid = oid;
760 priv->current_command_oid = oid;
761 ret = rndis_command(dev, u.header, buflen);
762 priv->current_command_oid = 0;
763 if (ret < 0)
764 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
765 __func__, oid_to_string(oid), ret,
766 le32_to_cpu(u.get_c->status));
768 if (ret == 0) {
769 resplen = le32_to_cpu(u.get_c->len);
770 respoffs = le32_to_cpu(u.get_c->offset) + 8;
772 if (respoffs > buflen) {
773 /* Device returned data offset outside buffer, error. */
774 netdev_dbg(dev->net, "%s(%s): received invalid "
775 "data offset: %d > %d\n", __func__,
776 oid_to_string(oid), respoffs, buflen);
778 ret = -EINVAL;
779 goto exit_unlock;
782 if ((resplen + respoffs) > buflen) {
783 /* Device would have returned more data if buffer would
784 * have been big enough. Copy just the bits that we got.
786 copylen = buflen - respoffs;
787 } else {
788 copylen = resplen;
791 if (copylen > *len)
792 copylen = *len;
794 memcpy(data, u.buf + respoffs, copylen);
796 *len = resplen;
798 ret = rndis_error_status(u.get_c->status);
799 if (ret < 0)
800 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
801 __func__, oid_to_string(oid),
802 le32_to_cpu(u.get_c->status), ret);
805 exit_unlock:
806 mutex_unlock(&priv->command_lock);
808 if (u.buf != priv->command_buffer)
809 kfree(u.buf);
810 return ret;
813 static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
815 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
816 union {
817 void *buf;
818 struct rndis_msg_hdr *header;
819 struct rndis_set *set;
820 struct rndis_set_c *set_c;
821 } u;
822 int ret, buflen;
824 buflen = len + sizeof(*u.set);
825 if (buflen < CONTROL_BUFFER_SIZE)
826 buflen = CONTROL_BUFFER_SIZE;
828 if (buflen > COMMAND_BUFFER_SIZE) {
829 u.buf = kmalloc(buflen, GFP_KERNEL);
830 if (!u.buf)
831 return -ENOMEM;
832 } else {
833 u.buf = priv->command_buffer;
836 mutex_lock(&priv->command_lock);
838 memset(u.set, 0, sizeof *u.set);
839 u.set->msg_type = RNDIS_MSG_SET;
840 u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
841 u.set->oid = oid;
842 u.set->len = cpu_to_le32(len);
843 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
844 u.set->handle = cpu_to_le32(0);
845 memcpy(u.buf + sizeof(*u.set), data, len);
847 priv->current_command_oid = oid;
848 ret = rndis_command(dev, u.header, buflen);
849 priv->current_command_oid = 0;
850 if (ret < 0)
851 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
852 __func__, oid_to_string(oid), ret,
853 le32_to_cpu(u.set_c->status));
855 if (ret == 0) {
856 ret = rndis_error_status(u.set_c->status);
858 if (ret < 0)
859 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
860 __func__, oid_to_string(oid),
861 le32_to_cpu(u.set_c->status), ret);
864 mutex_unlock(&priv->command_lock);
866 if (u.buf != priv->command_buffer)
867 kfree(u.buf);
868 return ret;
871 static int rndis_reset(struct usbnet *usbdev)
873 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
874 struct rndis_reset *reset;
875 int ret;
877 mutex_lock(&priv->command_lock);
879 reset = (void *)priv->command_buffer;
880 memset(reset, 0, sizeof(*reset));
881 reset->msg_type = RNDIS_MSG_RESET;
882 reset->msg_len = cpu_to_le32(sizeof(*reset));
883 priv->current_command_oid = 0;
884 ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
886 mutex_unlock(&priv->command_lock);
888 if (ret < 0)
889 return ret;
890 return 0;
894 * Specs say that we can only set config parameters only soon after device
895 * initialization.
896 * value_type: 0 = u32, 2 = unicode string
898 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
899 int value_type, void *value)
901 struct ndis_config_param *infobuf;
902 int value_len, info_len, param_len, ret, i;
903 __le16 *unibuf;
904 __le32 *dst_value;
906 if (value_type == 0)
907 value_len = sizeof(__le32);
908 else if (value_type == 2)
909 value_len = strlen(value) * sizeof(__le16);
910 else
911 return -EINVAL;
913 param_len = strlen(param) * sizeof(__le16);
914 info_len = sizeof(*infobuf) + param_len + value_len;
916 #ifdef DEBUG
917 info_len += 12;
918 #endif
919 infobuf = kmalloc(info_len, GFP_KERNEL);
920 if (!infobuf)
921 return -ENOMEM;
923 #ifdef DEBUG
924 info_len -= 12;
925 /* extra 12 bytes are for padding (debug output) */
926 memset(infobuf, 0xCC, info_len + 12);
927 #endif
929 if (value_type == 2)
930 netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
931 param, (u8 *)value);
932 else
933 netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
934 param, *(u32 *)value);
936 infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
937 infobuf->name_length = cpu_to_le32(param_len);
938 infobuf->type = cpu_to_le32(value_type);
939 infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
940 infobuf->value_length = cpu_to_le32(value_len);
942 /* simple string to unicode string conversion */
943 unibuf = (void *)infobuf + sizeof(*infobuf);
944 for (i = 0; i < param_len / sizeof(__le16); i++)
945 unibuf[i] = cpu_to_le16(param[i]);
947 if (value_type == 2) {
948 unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
949 for (i = 0; i < value_len / sizeof(__le16); i++)
950 unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
951 } else {
952 dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
953 *dst_value = cpu_to_le32(*(u32 *)value);
956 #ifdef DEBUG
957 netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
958 for (i = 0; i < info_len; i += 12) {
959 u32 *tmp = (u32 *)((u8 *)infobuf + i);
960 netdev_dbg(dev->net, "%08X:%08X:%08X\n",
961 cpu_to_be32(tmp[0]),
962 cpu_to_be32(tmp[1]),
963 cpu_to_be32(tmp[2]));
965 #endif
967 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
968 infobuf, info_len);
969 if (ret != 0)
970 netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
971 ret);
973 kfree(infobuf);
974 return ret;
977 static int rndis_set_config_parameter_str(struct usbnet *dev,
978 char *param, char *value)
980 return rndis_set_config_parameter(dev, param, 2, value);
984 * data conversion functions
986 static int level_to_qual(int level)
988 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
989 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
993 * common functions
995 static int set_infra_mode(struct usbnet *usbdev, int mode);
996 static void restore_keys(struct usbnet *usbdev);
997 static int rndis_check_bssid_list(struct usbnet *usbdev);
999 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
1001 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1002 int ret;
1004 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
1005 if (ret < 0) {
1006 netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
1007 return ret;
1009 if (ret == 0) {
1010 memcpy(&priv->essid, ssid, sizeof(priv->essid));
1011 priv->radio_on = true;
1012 netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
1015 return ret;
1018 static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1020 int ret;
1022 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1023 if (ret < 0) {
1024 netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1025 bssid, ret);
1026 return ret;
1029 return ret;
1032 static int clear_bssid(struct usbnet *usbdev)
1034 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1036 return set_bssid(usbdev, broadcast_mac);
1039 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1041 int ret, len;
1043 len = ETH_ALEN;
1044 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1046 if (ret != 0)
1047 memset(bssid, 0, ETH_ALEN);
1049 return ret;
1052 static int get_association_info(struct usbnet *usbdev,
1053 struct ndis_80211_assoc_info *info, int len)
1055 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1056 info, &len);
1059 static bool is_associated(struct usbnet *usbdev)
1061 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1062 u8 bssid[ETH_ALEN];
1063 int ret;
1065 if (!priv->radio_on)
1066 return false;
1068 ret = get_bssid(usbdev, bssid);
1070 return (ret == 0 && !is_zero_ether_addr(bssid));
1073 static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1075 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1076 struct ndis_80211_ssid ssid;
1077 int i, ret = 0;
1079 if (priv->radio_on) {
1080 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1081 if (ret == 0) {
1082 priv->radio_on = false;
1083 netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1084 __func__);
1086 if (reset_ssid)
1087 msleep(100);
1091 /* disassociate causes radio to be turned off; if reset_ssid
1092 * is given, set random ssid to enable radio */
1093 if (reset_ssid) {
1094 /* Set device to infrastructure mode so we don't get ad-hoc
1095 * 'media connect' indications with the random ssid.
1097 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1099 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1100 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1101 ssid.essid[0] = 0x1;
1102 ssid.essid[1] = 0xff;
1103 for (i = 2; i < sizeof(ssid.essid); i++)
1104 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1105 ret = set_essid(usbdev, &ssid);
1107 return ret;
1110 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1111 enum nl80211_auth_type auth_type, int keymgmt)
1113 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1114 __le32 tmp;
1115 int auth_mode, ret;
1117 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1118 __func__, wpa_version, auth_type, keymgmt);
1120 if (wpa_version & NL80211_WPA_VERSION_2) {
1121 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1122 auth_mode = NDIS_80211_AUTH_WPA2;
1123 else
1124 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1125 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1126 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1127 auth_mode = NDIS_80211_AUTH_WPA;
1128 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1129 auth_mode = NDIS_80211_AUTH_WPA_PSK;
1130 else
1131 auth_mode = NDIS_80211_AUTH_WPA_NONE;
1132 } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1133 auth_mode = NDIS_80211_AUTH_SHARED;
1134 else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1135 auth_mode = NDIS_80211_AUTH_OPEN;
1136 else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1137 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1138 else
1139 return -ENOTSUPP;
1141 tmp = cpu_to_le32(auth_mode);
1142 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1143 sizeof(tmp));
1144 if (ret != 0) {
1145 netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1146 ret);
1147 return ret;
1150 priv->wpa_version = wpa_version;
1152 return 0;
1155 static int set_priv_filter(struct usbnet *usbdev)
1157 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1158 __le32 tmp;
1160 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1161 __func__, priv->wpa_version);
1163 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1164 priv->wpa_version & NL80211_WPA_VERSION_1)
1165 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1166 else
1167 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1169 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1170 sizeof(tmp));
1173 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1175 __le32 tmp;
1176 int encr_mode, ret;
1178 netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1179 __func__, pairwise, groupwise);
1181 if (pairwise & RNDIS_WLAN_ALG_CCMP)
1182 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1183 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1184 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1185 else if (pairwise & RNDIS_WLAN_ALG_WEP)
1186 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1187 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1188 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1189 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1190 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1191 else
1192 encr_mode = NDIS_80211_ENCR_DISABLED;
1194 tmp = cpu_to_le32(encr_mode);
1195 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1196 sizeof(tmp));
1197 if (ret != 0) {
1198 netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1199 ret);
1200 return ret;
1203 return 0;
1206 static int set_infra_mode(struct usbnet *usbdev, int mode)
1208 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1209 __le32 tmp;
1210 int ret;
1212 netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1213 __func__, priv->infra_mode);
1215 tmp = cpu_to_le32(mode);
1216 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1217 sizeof(tmp));
1218 if (ret != 0) {
1219 netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1220 ret);
1221 return ret;
1224 /* NDIS drivers clear keys when infrastructure mode is
1225 * changed. But Linux tools assume otherwise. So set the
1226 * keys */
1227 restore_keys(usbdev);
1229 priv->infra_mode = mode;
1230 return 0;
1233 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1235 __le32 tmp;
1237 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1239 if (rts_threshold < 0 || rts_threshold > 2347)
1240 rts_threshold = 2347;
1242 tmp = cpu_to_le32(rts_threshold);
1243 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1244 sizeof(tmp));
1247 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1249 __le32 tmp;
1251 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1253 if (frag_threshold < 256 || frag_threshold > 2346)
1254 frag_threshold = 2346;
1256 tmp = cpu_to_le32(frag_threshold);
1257 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1258 sizeof(tmp));
1261 static void set_default_iw_params(struct usbnet *usbdev)
1263 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1264 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1265 RNDIS_WLAN_KEY_MGMT_NONE);
1266 set_priv_filter(usbdev);
1267 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1270 static int deauthenticate(struct usbnet *usbdev)
1272 int ret;
1274 ret = disassociate(usbdev, true);
1275 set_default_iw_params(usbdev);
1276 return ret;
1279 static int set_channel(struct usbnet *usbdev, int channel)
1281 struct ndis_80211_conf config;
1282 unsigned int dsconfig;
1283 int len, ret;
1285 netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1287 /* this OID is valid only when not associated */
1288 if (is_associated(usbdev))
1289 return 0;
1291 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1293 len = sizeof(config);
1294 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1295 if (ret < 0) {
1296 netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1297 __func__);
1298 return ret;
1301 config.ds_config = cpu_to_le32(dsconfig);
1302 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1303 sizeof(config));
1305 netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1307 return ret;
1310 /* index must be 0 - N, as per NDIS */
1311 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1312 int index)
1314 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1315 struct ndis_80211_wep_key ndis_key;
1316 u32 cipher;
1317 int ret;
1319 netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1320 __func__, index, key_len);
1322 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
1323 return -EINVAL;
1325 if (key_len == 5)
1326 cipher = WLAN_CIPHER_SUITE_WEP40;
1327 else
1328 cipher = WLAN_CIPHER_SUITE_WEP104;
1330 memset(&ndis_key, 0, sizeof(ndis_key));
1332 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1333 ndis_key.length = cpu_to_le32(key_len);
1334 ndis_key.index = cpu_to_le32(index);
1335 memcpy(&ndis_key.material, key, key_len);
1337 if (index == priv->encr_tx_key_index) {
1338 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1339 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1340 RNDIS_WLAN_ALG_NONE);
1341 if (ret)
1342 netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1343 ret);
1346 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1347 sizeof(ndis_key));
1348 if (ret != 0) {
1349 netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1350 index + 1, ret);
1351 return ret;
1354 priv->encr_keys[index].len = key_len;
1355 priv->encr_keys[index].cipher = cipher;
1356 memcpy(&priv->encr_keys[index].material, key, key_len);
1357 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1359 return 0;
1362 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1363 int index, const u8 *addr, const u8 *rx_seq,
1364 int seq_len, u32 cipher, __le32 flags)
1366 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1367 struct ndis_80211_key ndis_key;
1368 bool is_addr_ok;
1369 int ret;
1371 if (index < 0 || index >= 4) {
1372 netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1373 __func__, index);
1374 return -EINVAL;
1376 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1377 netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1378 __func__, key_len);
1379 return -EINVAL;
1381 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1382 if (!rx_seq || seq_len <= 0) {
1383 netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1384 __func__);
1385 return -EINVAL;
1387 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1388 netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1389 return -EINVAL;
1393 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1394 !is_broadcast_ether_addr(addr);
1395 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1396 netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1397 __func__, addr);
1398 return -EINVAL;
1401 netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1402 __func__, index,
1403 !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1404 !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1405 !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1407 memset(&ndis_key, 0, sizeof(ndis_key));
1409 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1410 sizeof(ndis_key.material) + key_len);
1411 ndis_key.length = cpu_to_le32(key_len);
1412 ndis_key.index = cpu_to_le32(index) | flags;
1414 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1415 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1416 * different order than NDIS spec, so swap the order here. */
1417 memcpy(ndis_key.material, key, 16);
1418 memcpy(ndis_key.material + 16, key + 24, 8);
1419 memcpy(ndis_key.material + 24, key + 16, 8);
1420 } else
1421 memcpy(ndis_key.material, key, key_len);
1423 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1424 memcpy(ndis_key.rsc, rx_seq, seq_len);
1426 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1427 /* pairwise key */
1428 memcpy(ndis_key.bssid, addr, ETH_ALEN);
1429 } else {
1430 /* group key */
1431 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1432 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1433 else
1434 get_bssid(usbdev, ndis_key.bssid);
1437 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1438 le32_to_cpu(ndis_key.size));
1439 netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1440 __func__, ret);
1441 if (ret != 0)
1442 return ret;
1444 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1445 priv->encr_keys[index].len = key_len;
1446 priv->encr_keys[index].cipher = cipher;
1447 memcpy(&priv->encr_keys[index].material, key, key_len);
1448 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1449 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1450 else
1451 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
1453 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1454 priv->encr_tx_key_index = index;
1456 return 0;
1459 static int restore_key(struct usbnet *usbdev, int key_idx)
1461 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1462 struct rndis_wlan_encr_key key;
1464 if (is_wpa_key(priv, key_idx))
1465 return 0;
1467 key = priv->encr_keys[key_idx];
1469 netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1471 if (key.len == 0)
1472 return 0;
1474 return add_wep_key(usbdev, key.material, key.len, key_idx);
1477 static void restore_keys(struct usbnet *usbdev)
1479 int i;
1481 for (i = 0; i < 4; i++)
1482 restore_key(usbdev, i);
1485 static void clear_key(struct rndis_wlan_private *priv, int idx)
1487 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1490 /* remove_key is for both wep and wpa */
1491 static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
1493 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1494 struct ndis_80211_remove_key remove_key;
1495 __le32 keyindex;
1496 bool is_wpa;
1497 int ret;
1499 if (priv->encr_keys[index].len == 0)
1500 return 0;
1502 is_wpa = is_wpa_key(priv, index);
1504 netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1505 __func__, index, is_wpa ? "wpa" : "wep",
1506 priv->encr_keys[index].len);
1508 clear_key(priv, index);
1510 if (is_wpa) {
1511 remove_key.size = cpu_to_le32(sizeof(remove_key));
1512 remove_key.index = cpu_to_le32(index);
1513 if (bssid) {
1514 /* pairwise key */
1515 if (!is_broadcast_ether_addr(bssid))
1516 remove_key.index |=
1517 NDIS_80211_ADDKEY_PAIRWISE_KEY;
1518 memcpy(remove_key.bssid, bssid,
1519 sizeof(remove_key.bssid));
1520 } else
1521 memset(remove_key.bssid, 0xff,
1522 sizeof(remove_key.bssid));
1524 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1525 sizeof(remove_key));
1526 if (ret != 0)
1527 return ret;
1528 } else {
1529 keyindex = cpu_to_le32(index);
1530 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1531 sizeof(keyindex));
1532 if (ret != 0) {
1533 netdev_warn(usbdev->net,
1534 "removing encryption key %d failed (%08X)\n",
1535 index, ret);
1536 return ret;
1540 /* if it is transmit key, disable encryption */
1541 if (index == priv->encr_tx_key_index)
1542 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1544 return 0;
1547 static void set_multicast_list(struct usbnet *usbdev)
1549 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1550 struct netdev_hw_addr *ha;
1551 __le32 filter, basefilter;
1552 int ret;
1553 char *mc_addrs = NULL;
1554 int mc_count;
1556 basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1557 RNDIS_PACKET_TYPE_BROADCAST;
1559 if (usbdev->net->flags & IFF_PROMISC) {
1560 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1561 RNDIS_PACKET_TYPE_ALL_LOCAL;
1562 } else if (usbdev->net->flags & IFF_ALLMULTI) {
1563 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1566 if (filter != basefilter)
1567 goto set_filter;
1570 * mc_list should be accessed holding the lock, so copy addresses to
1571 * local buffer first.
1573 netif_addr_lock_bh(usbdev->net);
1574 mc_count = netdev_mc_count(usbdev->net);
1575 if (mc_count > priv->multicast_size) {
1576 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1577 } else if (mc_count) {
1578 int i = 0;
1580 mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1581 if (!mc_addrs) {
1582 netdev_warn(usbdev->net,
1583 "couldn't alloc %d bytes of memory\n",
1584 mc_count * ETH_ALEN);
1585 netif_addr_unlock_bh(usbdev->net);
1586 return;
1589 netdev_for_each_mc_addr(ha, usbdev->net)
1590 memcpy(mc_addrs + i++ * ETH_ALEN,
1591 ha->addr, ETH_ALEN);
1593 netif_addr_unlock_bh(usbdev->net);
1595 if (filter != basefilter)
1596 goto set_filter;
1598 if (mc_count) {
1599 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
1600 mc_count * ETH_ALEN);
1601 kfree(mc_addrs);
1602 if (ret == 0)
1603 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1604 else
1605 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1607 netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1608 mc_count, priv->multicast_size, ret);
1611 set_filter:
1612 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1613 sizeof(filter));
1614 if (ret < 0) {
1615 netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1616 le32_to_cpu(filter));
1619 netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1620 le32_to_cpu(filter), ret);
1623 #ifdef DEBUG
1624 static void debug_print_pmkids(struct usbnet *usbdev,
1625 struct ndis_80211_pmkid *pmkids,
1626 const char *func_str)
1628 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1629 int i, len, count, max_pmkids, entry_len;
1631 max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1632 len = le32_to_cpu(pmkids->length);
1633 count = le32_to_cpu(pmkids->bssid_info_count);
1635 entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1637 netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1638 "%d)\n", func_str, count, len, entry_len);
1640 if (count > max_pmkids)
1641 count = max_pmkids;
1643 for (i = 0; i < count; i++) {
1644 u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1646 netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
1647 "pmkid: %08X:%08X:%08X:%08X\n",
1648 func_str, pmkids->bssid_info[i].bssid,
1649 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1650 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1653 #else
1654 static void debug_print_pmkids(struct usbnet *usbdev,
1655 struct ndis_80211_pmkid *pmkids,
1656 const char *func_str)
1658 return;
1660 #endif
1662 static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1664 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1665 struct ndis_80211_pmkid *pmkids;
1666 int len, ret, max_pmkids;
1668 max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1669 len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
1671 pmkids = kzalloc(len, GFP_KERNEL);
1672 if (!pmkids)
1673 return ERR_PTR(-ENOMEM);
1675 pmkids->length = cpu_to_le32(len);
1676 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1678 ret = rndis_query_oid(usbdev, OID_802_11_PMKID, pmkids, &len);
1679 if (ret < 0) {
1680 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d)"
1681 " -> %d\n", __func__, len, max_pmkids, ret);
1683 kfree(pmkids);
1684 return ERR_PTR(ret);
1687 if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1688 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1690 debug_print_pmkids(usbdev, pmkids, __func__);
1692 return pmkids;
1695 static int set_device_pmkids(struct usbnet *usbdev,
1696 struct ndis_80211_pmkid *pmkids)
1698 int ret, len, num_pmkids;
1700 num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1701 len = sizeof(*pmkids) + num_pmkids * sizeof(pmkids->bssid_info[0]);
1702 pmkids->length = cpu_to_le32(len);
1704 debug_print_pmkids(usbdev, pmkids, __func__);
1706 ret = rndis_set_oid(usbdev, OID_802_11_PMKID, pmkids,
1707 le32_to_cpu(pmkids->length));
1708 if (ret < 0) {
1709 netdev_dbg(usbdev->net, "%s(): OID_802_11_PMKID(%d, %d) -> %d"
1710 "\n", __func__, len, num_pmkids, ret);
1713 kfree(pmkids);
1714 return ret;
1717 static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1718 struct ndis_80211_pmkid *pmkids,
1719 struct cfg80211_pmksa *pmksa,
1720 int max_pmkids)
1722 int i, len, count, newlen, err;
1724 len = le32_to_cpu(pmkids->length);
1725 count = le32_to_cpu(pmkids->bssid_info_count);
1727 if (count > max_pmkids)
1728 count = max_pmkids;
1730 for (i = 0; i < count; i++)
1731 if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
1732 pmksa->bssid))
1733 break;
1735 /* pmkid not found */
1736 if (i == count) {
1737 netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1738 __func__, pmksa->bssid);
1739 err = -ENOENT;
1740 goto error;
1743 for (; i + 1 < count; i++)
1744 pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1746 count--;
1747 newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
1749 pmkids->length = cpu_to_le32(newlen);
1750 pmkids->bssid_info_count = cpu_to_le32(count);
1752 return pmkids;
1753 error:
1754 kfree(pmkids);
1755 return ERR_PTR(err);
1758 static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1759 struct ndis_80211_pmkid *pmkids,
1760 struct cfg80211_pmksa *pmksa,
1761 int max_pmkids)
1763 int i, err, len, count, newlen;
1765 len = le32_to_cpu(pmkids->length);
1766 count = le32_to_cpu(pmkids->bssid_info_count);
1768 if (count > max_pmkids)
1769 count = max_pmkids;
1771 /* update with new pmkid */
1772 for (i = 0; i < count; i++) {
1773 if (compare_ether_addr(pmkids->bssid_info[i].bssid,
1774 pmksa->bssid))
1775 continue;
1777 memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1778 WLAN_PMKID_LEN);
1780 return pmkids;
1783 /* out of space, return error */
1784 if (i == max_pmkids) {
1785 netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1786 err = -ENOSPC;
1787 goto error;
1790 /* add new pmkid */
1791 newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
1793 pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1794 if (!pmkids) {
1795 err = -ENOMEM;
1796 goto error;
1799 pmkids->length = cpu_to_le32(newlen);
1800 pmkids->bssid_info_count = cpu_to_le32(count + 1);
1802 memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1803 memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1805 return pmkids;
1806 error:
1807 kfree(pmkids);
1808 return ERR_PTR(err);
1812 * cfg80211 ops
1814 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1815 struct net_device *dev,
1816 enum nl80211_iftype type, u32 *flags,
1817 struct vif_params *params)
1819 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1820 struct usbnet *usbdev = priv->usbdev;
1821 int mode;
1823 switch (type) {
1824 case NL80211_IFTYPE_ADHOC:
1825 mode = NDIS_80211_INFRA_ADHOC;
1826 break;
1827 case NL80211_IFTYPE_STATION:
1828 mode = NDIS_80211_INFRA_INFRA;
1829 break;
1830 default:
1831 return -EINVAL;
1834 priv->wdev.iftype = type;
1836 return set_infra_mode(usbdev, mode);
1839 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1841 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1842 struct usbnet *usbdev = priv->usbdev;
1843 int err;
1845 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1846 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1847 if (err < 0)
1848 return err;
1851 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1852 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1853 if (err < 0)
1854 return err;
1857 return 0;
1860 static int rndis_set_tx_power(struct wiphy *wiphy,
1861 enum nl80211_tx_power_setting type,
1862 int mbm)
1864 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1865 struct usbnet *usbdev = priv->usbdev;
1867 netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1868 __func__, type, mbm);
1870 if (mbm < 0 || (mbm % 100))
1871 return -ENOTSUPP;
1873 /* Device doesn't support changing txpower after initialization, only
1874 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1875 * currently used.
1877 if (type == NL80211_TX_POWER_AUTOMATIC ||
1878 MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1879 if (!priv->radio_on)
1880 disassociate(usbdev, true); /* turn on radio */
1882 return 0;
1885 return -ENOTSUPP;
1888 static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1890 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1891 struct usbnet *usbdev = priv->usbdev;
1893 *dbm = get_bcm4320_power_dbm(priv);
1895 netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1897 return 0;
1900 #define SCAN_DELAY_JIFFIES (6 * HZ)
1901 static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1902 struct cfg80211_scan_request *request)
1904 struct usbnet *usbdev = netdev_priv(dev);
1905 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1906 int ret;
1907 __le32 tmp;
1909 netdev_dbg(usbdev->net, "cfg80211.scan\n");
1911 /* Get current bssid list from device before new scan, as new scan
1912 * clears internal bssid list.
1914 rndis_check_bssid_list(usbdev);
1916 if (!request)
1917 return -EINVAL;
1919 if (priv->scan_request && priv->scan_request != request)
1920 return -EBUSY;
1922 priv->scan_request = request;
1924 tmp = cpu_to_le32(1);
1925 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1926 sizeof(tmp));
1927 if (ret == 0) {
1928 /* Wait before retrieving scan results from device */
1929 queue_delayed_work(priv->workqueue, &priv->scan_work,
1930 SCAN_DELAY_JIFFIES);
1933 return ret;
1936 static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1937 struct ndis_80211_bssid_ex *bssid)
1939 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1940 struct ieee80211_channel *channel;
1941 s32 signal;
1942 u64 timestamp;
1943 u16 capability;
1944 u16 beacon_interval;
1945 struct ndis_80211_fixed_ies *fixed;
1946 int ie_len, bssid_len;
1947 u8 *ie;
1949 netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
1950 bssid->ssid.essid, bssid->mac);
1952 /* parse bssid structure */
1953 bssid_len = le32_to_cpu(bssid->length);
1955 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1956 sizeof(struct ndis_80211_fixed_ies))
1957 return NULL;
1959 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1961 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1962 ie_len = min(bssid_len - (int)sizeof(*bssid),
1963 (int)le32_to_cpu(bssid->ie_length));
1964 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1965 if (ie_len < 0)
1966 return NULL;
1968 /* extract data for cfg80211_inform_bss */
1969 channel = ieee80211_get_channel(priv->wdev.wiphy,
1970 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1971 if (!channel)
1972 return NULL;
1974 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1975 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1976 capability = le16_to_cpu(fixed->capabilities);
1977 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1979 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1980 timestamp, capability, beacon_interval, ie, ie_len, signal,
1981 GFP_KERNEL);
1984 static int rndis_check_bssid_list(struct usbnet *usbdev)
1986 void *buf = NULL;
1987 struct ndis_80211_bssid_list_ex *bssid_list;
1988 struct ndis_80211_bssid_ex *bssid;
1989 int ret = -EINVAL, len, count, bssid_len;
1990 bool resized = false;
1992 netdev_dbg(usbdev->net, "check_bssid_list\n");
1994 len = CONTROL_BUFFER_SIZE;
1995 resize_buf:
1996 buf = kmalloc(len, GFP_KERNEL);
1997 if (!buf) {
1998 ret = -ENOMEM;
1999 goto out;
2002 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2003 if (ret != 0)
2004 goto out;
2006 if (!resized && len > CONTROL_BUFFER_SIZE) {
2007 resized = true;
2008 kfree(buf);
2009 goto resize_buf;
2012 bssid_list = buf;
2013 bssid = bssid_list->bssid;
2014 bssid_len = le32_to_cpu(bssid->length);
2015 count = le32_to_cpu(bssid_list->num_items);
2016 netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
2017 count, len);
2019 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
2020 rndis_bss_info_update(usbdev, bssid);
2022 bssid = (void *)bssid + bssid_len;
2023 bssid_len = le32_to_cpu(bssid->length);
2024 count--;
2027 out:
2028 kfree(buf);
2029 return ret;
2032 static void rndis_get_scan_results(struct work_struct *work)
2034 struct rndis_wlan_private *priv =
2035 container_of(work, struct rndis_wlan_private, scan_work.work);
2036 struct usbnet *usbdev = priv->usbdev;
2037 int ret;
2039 netdev_dbg(usbdev->net, "get_scan_results\n");
2041 if (!priv->scan_request)
2042 return;
2044 ret = rndis_check_bssid_list(usbdev);
2046 cfg80211_scan_done(priv->scan_request, ret < 0);
2048 priv->scan_request = NULL;
2051 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2052 struct cfg80211_connect_params *sme)
2054 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2055 struct usbnet *usbdev = priv->usbdev;
2056 struct ieee80211_channel *channel = sme->channel;
2057 struct ndis_80211_ssid ssid;
2058 int pairwise = RNDIS_WLAN_ALG_NONE;
2059 int groupwise = RNDIS_WLAN_ALG_NONE;
2060 int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2061 int length, i, ret, chan = -1;
2063 if (channel)
2064 chan = ieee80211_frequency_to_channel(channel->center_freq);
2066 groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2067 for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2068 pairwise |=
2069 rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2071 if (sme->crypto.n_ciphers_pairwise > 0 &&
2072 pairwise == RNDIS_WLAN_ALG_NONE) {
2073 netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2074 return -ENOTSUPP;
2077 for (i = 0; i < sme->crypto.n_akm_suites; i++)
2078 keymgmt |=
2079 rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2081 if (sme->crypto.n_akm_suites > 0 &&
2082 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2083 netdev_err(usbdev->net, "Invalid keymgmt\n");
2084 return -ENOTSUPP;
2087 netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2088 sme->ssid, sme->bssid, chan,
2089 sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2090 groupwise, pairwise, keymgmt);
2092 if (is_associated(usbdev))
2093 disassociate(usbdev, false);
2095 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2096 if (ret < 0) {
2097 netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2098 ret);
2099 goto err_turn_radio_on;
2102 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2103 keymgmt);
2104 if (ret < 0) {
2105 netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2106 ret);
2107 goto err_turn_radio_on;
2110 set_priv_filter(usbdev);
2112 ret = set_encr_mode(usbdev, pairwise, groupwise);
2113 if (ret < 0) {
2114 netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2115 ret);
2116 goto err_turn_radio_on;
2119 if (channel) {
2120 ret = set_channel(usbdev, chan);
2121 if (ret < 0) {
2122 netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2123 ret);
2124 goto err_turn_radio_on;
2128 if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2129 priv->encr_tx_key_index = sme->key_idx;
2130 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2131 if (ret < 0) {
2132 netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2133 ret, sme->key_len, sme->key_idx);
2134 goto err_turn_radio_on;
2138 if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2139 !is_broadcast_ether_addr(sme->bssid)) {
2140 ret = set_bssid(usbdev, sme->bssid);
2141 if (ret < 0) {
2142 netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2143 ret);
2144 goto err_turn_radio_on;
2146 } else
2147 clear_bssid(usbdev);
2149 length = sme->ssid_len;
2150 if (length > NDIS_802_11_LENGTH_SSID)
2151 length = NDIS_802_11_LENGTH_SSID;
2153 memset(&ssid, 0, sizeof(ssid));
2154 ssid.length = cpu_to_le32(length);
2155 memcpy(ssid.essid, sme->ssid, length);
2157 /* Pause and purge rx queue, so we don't pass packets before
2158 * 'media connect'-indication.
2160 usbnet_pause_rx(usbdev);
2161 usbnet_purge_paused_rxq(usbdev);
2163 ret = set_essid(usbdev, &ssid);
2164 if (ret < 0)
2165 netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2166 return ret;
2168 err_turn_radio_on:
2169 disassociate(usbdev, true);
2171 return ret;
2174 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2175 u16 reason_code)
2177 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2178 struct usbnet *usbdev = priv->usbdev;
2180 netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2182 priv->connected = false;
2183 memset(priv->bssid, 0, ETH_ALEN);
2185 return deauthenticate(usbdev);
2188 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2189 struct cfg80211_ibss_params *params)
2191 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2192 struct usbnet *usbdev = priv->usbdev;
2193 struct ieee80211_channel *channel = params->channel;
2194 struct ndis_80211_ssid ssid;
2195 enum nl80211_auth_type auth_type;
2196 int ret, alg, length, chan = -1;
2198 if (channel)
2199 chan = ieee80211_frequency_to_channel(channel->center_freq);
2201 /* TODO: How to handle ad-hoc encryption?
2202 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2203 * pre-shared for encryption (open/shared/wpa), is key set before
2204 * join_ibss? Which auth_type to use (not in params)? What about WPA?
2206 if (params->privacy) {
2207 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2208 alg = RNDIS_WLAN_ALG_WEP;
2209 } else {
2210 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2211 alg = RNDIS_WLAN_ALG_NONE;
2214 netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2215 params->ssid, params->bssid, chan, params->privacy);
2217 if (is_associated(usbdev))
2218 disassociate(usbdev, false);
2220 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2221 if (ret < 0) {
2222 netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2223 ret);
2224 goto err_turn_radio_on;
2227 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2228 if (ret < 0) {
2229 netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2230 ret);
2231 goto err_turn_radio_on;
2234 set_priv_filter(usbdev);
2236 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2237 if (ret < 0) {
2238 netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2239 ret);
2240 goto err_turn_radio_on;
2243 if (channel) {
2244 ret = set_channel(usbdev, chan);
2245 if (ret < 0) {
2246 netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2247 ret);
2248 goto err_turn_radio_on;
2252 if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2253 !is_broadcast_ether_addr(params->bssid)) {
2254 ret = set_bssid(usbdev, params->bssid);
2255 if (ret < 0) {
2256 netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2257 ret);
2258 goto err_turn_radio_on;
2260 } else
2261 clear_bssid(usbdev);
2263 length = params->ssid_len;
2264 if (length > NDIS_802_11_LENGTH_SSID)
2265 length = NDIS_802_11_LENGTH_SSID;
2267 memset(&ssid, 0, sizeof(ssid));
2268 ssid.length = cpu_to_le32(length);
2269 memcpy(ssid.essid, params->ssid, length);
2271 /* Don't need to pause rx queue for ad-hoc. */
2272 usbnet_purge_paused_rxq(usbdev);
2273 usbnet_resume_rx(usbdev);
2275 ret = set_essid(usbdev, &ssid);
2276 if (ret < 0)
2277 netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2278 ret);
2279 return ret;
2281 err_turn_radio_on:
2282 disassociate(usbdev, true);
2284 return ret;
2287 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2289 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2290 struct usbnet *usbdev = priv->usbdev;
2292 netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2294 priv->connected = false;
2295 memset(priv->bssid, 0, ETH_ALEN);
2297 return deauthenticate(usbdev);
2300 static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
2301 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2303 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2304 struct usbnet *usbdev = priv->usbdev;
2306 return set_channel(usbdev,
2307 ieee80211_frequency_to_channel(chan->center_freq));
2310 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2311 u8 key_index, const u8 *mac_addr,
2312 struct key_params *params)
2314 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2315 struct usbnet *usbdev = priv->usbdev;
2316 __le32 flags;
2318 netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2319 __func__, key_index, mac_addr, params->cipher);
2321 switch (params->cipher) {
2322 case WLAN_CIPHER_SUITE_WEP40:
2323 case WLAN_CIPHER_SUITE_WEP104:
2324 return add_wep_key(usbdev, params->key, params->key_len,
2325 key_index);
2326 case WLAN_CIPHER_SUITE_TKIP:
2327 case WLAN_CIPHER_SUITE_CCMP:
2328 flags = 0;
2330 if (params->seq && params->seq_len > 0)
2331 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2332 if (mac_addr)
2333 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2334 NDIS_80211_ADDKEY_TRANSMIT_KEY;
2336 return add_wpa_key(usbdev, params->key, params->key_len,
2337 key_index, mac_addr, params->seq,
2338 params->seq_len, params->cipher, flags);
2339 default:
2340 netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2341 __func__, params->cipher);
2342 return -ENOTSUPP;
2346 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2347 u8 key_index, const u8 *mac_addr)
2349 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2350 struct usbnet *usbdev = priv->usbdev;
2352 netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2354 return remove_key(usbdev, key_index, mac_addr);
2357 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2358 u8 key_index)
2360 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2361 struct usbnet *usbdev = priv->usbdev;
2362 struct rndis_wlan_encr_key key;
2364 netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2366 priv->encr_tx_key_index = key_index;
2368 key = priv->encr_keys[key_index];
2370 return add_wep_key(usbdev, key.material, key.len, key_index);
2373 static void rndis_fill_station_info(struct usbnet *usbdev,
2374 struct station_info *sinfo)
2376 __le32 linkspeed, rssi;
2377 int ret, len;
2379 memset(sinfo, 0, sizeof(*sinfo));
2381 len = sizeof(linkspeed);
2382 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2383 if (ret == 0) {
2384 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2385 sinfo->filled |= STATION_INFO_TX_BITRATE;
2388 len = sizeof(rssi);
2389 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2390 if (ret == 0) {
2391 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2392 sinfo->filled |= STATION_INFO_SIGNAL;
2396 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2397 u8 *mac, struct station_info *sinfo)
2399 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2400 struct usbnet *usbdev = priv->usbdev;
2402 if (compare_ether_addr(priv->bssid, mac))
2403 return -ENOENT;
2405 rndis_fill_station_info(usbdev, sinfo);
2407 return 0;
2410 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2411 int idx, u8 *mac, struct station_info *sinfo)
2413 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2414 struct usbnet *usbdev = priv->usbdev;
2416 if (idx != 0)
2417 return -ENOENT;
2419 memcpy(mac, priv->bssid, ETH_ALEN);
2421 rndis_fill_station_info(usbdev, sinfo);
2423 return 0;
2426 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2427 struct cfg80211_pmksa *pmksa)
2429 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2430 struct usbnet *usbdev = priv->usbdev;
2431 struct ndis_80211_pmkid *pmkids;
2432 u32 *tmp = (u32 *)pmksa->pmkid;
2434 netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2435 pmksa->bssid,
2436 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2437 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2439 pmkids = get_device_pmkids(usbdev);
2440 if (IS_ERR(pmkids)) {
2441 /* couldn't read PMKID cache from device */
2442 return PTR_ERR(pmkids);
2445 pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2446 if (IS_ERR(pmkids)) {
2447 /* not found, list full, etc */
2448 return PTR_ERR(pmkids);
2451 return set_device_pmkids(usbdev, pmkids);
2454 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2455 struct cfg80211_pmksa *pmksa)
2457 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2458 struct usbnet *usbdev = priv->usbdev;
2459 struct ndis_80211_pmkid *pmkids;
2460 u32 *tmp = (u32 *)pmksa->pmkid;
2462 netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2463 pmksa->bssid,
2464 cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2465 cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2467 pmkids = get_device_pmkids(usbdev);
2468 if (IS_ERR(pmkids)) {
2469 /* Couldn't read PMKID cache from device */
2470 return PTR_ERR(pmkids);
2473 pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2474 if (IS_ERR(pmkids)) {
2475 /* not found, etc */
2476 return PTR_ERR(pmkids);
2479 return set_device_pmkids(usbdev, pmkids);
2482 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2484 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2485 struct usbnet *usbdev = priv->usbdev;
2486 struct ndis_80211_pmkid pmkid;
2488 netdev_dbg(usbdev->net, "%s()\n", __func__);
2490 memset(&pmkid, 0, sizeof(pmkid));
2492 pmkid.length = cpu_to_le32(sizeof(pmkid));
2493 pmkid.bssid_info_count = cpu_to_le32(0);
2495 return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
2499 * workers, indication handlers, device poller
2501 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2503 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2504 struct ndis_80211_assoc_info *info = NULL;
2505 u8 bssid[ETH_ALEN];
2506 int resp_ie_len, req_ie_len;
2507 u8 *req_ie, *resp_ie;
2508 int ret, offset;
2509 bool roamed = false;
2511 if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2512 /* received media connect indication while connected, either
2513 * device reassociated with same AP or roamed to new. */
2514 roamed = true;
2517 req_ie_len = 0;
2518 resp_ie_len = 0;
2519 req_ie = NULL;
2520 resp_ie = NULL;
2522 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2523 info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2524 if (!info) {
2525 /* No memory? Try resume work later */
2526 set_bit(WORK_LINK_UP, &priv->work_pending);
2527 queue_work(priv->workqueue, &priv->work);
2528 return;
2531 /* Get association info IEs from device. */
2532 ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2533 if (!ret) {
2534 req_ie_len = le32_to_cpu(info->req_ie_length);
2535 if (req_ie_len > 0) {
2536 offset = le32_to_cpu(info->offset_req_ies);
2538 if (offset > CONTROL_BUFFER_SIZE)
2539 offset = CONTROL_BUFFER_SIZE;
2541 req_ie = (u8 *)info + offset;
2543 if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2544 req_ie_len =
2545 CONTROL_BUFFER_SIZE - offset;
2548 resp_ie_len = le32_to_cpu(info->resp_ie_length);
2549 if (resp_ie_len > 0) {
2550 offset = le32_to_cpu(info->offset_resp_ies);
2552 if (offset > CONTROL_BUFFER_SIZE)
2553 offset = CONTROL_BUFFER_SIZE;
2555 resp_ie = (u8 *)info + offset;
2557 if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2558 resp_ie_len =
2559 CONTROL_BUFFER_SIZE - offset;
2562 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2563 return;
2565 ret = get_bssid(usbdev, bssid);
2566 if (ret < 0)
2567 memset(bssid, 0, sizeof(bssid));
2569 netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2570 bssid, roamed ? " roamed" : "");
2572 /* Internal bss list in device always contains at least the currently
2573 * connected bss and we can get it to cfg80211 with
2574 * rndis_check_bssid_list().
2575 * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2576 * spec.
2578 rndis_check_bssid_list(usbdev);
2580 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2581 if (!roamed)
2582 cfg80211_connect_result(usbdev->net, bssid, req_ie,
2583 req_ie_len, resp_ie,
2584 resp_ie_len, 0, GFP_KERNEL);
2585 else
2586 cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2587 resp_ie, resp_ie_len, GFP_KERNEL);
2588 } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2589 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2591 if (info != NULL)
2592 kfree(info);
2594 priv->connected = true;
2595 memcpy(priv->bssid, bssid, ETH_ALEN);
2597 usbnet_resume_rx(usbdev);
2598 netif_carrier_on(usbdev->net);
2601 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2603 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2605 if (priv->connected) {
2606 priv->connected = false;
2607 memset(priv->bssid, 0, ETH_ALEN);
2609 deauthenticate(usbdev);
2611 cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
2614 netif_carrier_off(usbdev->net);
2617 static void rndis_wlan_worker(struct work_struct *work)
2619 struct rndis_wlan_private *priv =
2620 container_of(work, struct rndis_wlan_private, work);
2621 struct usbnet *usbdev = priv->usbdev;
2623 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2624 rndis_wlan_do_link_up_work(usbdev);
2626 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2627 rndis_wlan_do_link_down_work(usbdev);
2629 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2630 set_multicast_list(usbdev);
2633 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2635 struct usbnet *usbdev = netdev_priv(dev);
2636 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2638 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2639 return;
2641 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2642 queue_work(priv->workqueue, &priv->work);
2645 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2646 struct ndis_80211_status_indication *indication,
2647 int len)
2649 u8 *buf;
2650 const char *type;
2651 int flags, buflen, key_id;
2652 bool pairwise_error, group_error;
2653 struct ndis_80211_auth_request *auth_req;
2654 enum nl80211_key_type key_type;
2656 /* must have at least one array entry */
2657 if (len < offsetof(struct ndis_80211_status_indication, u) +
2658 sizeof(struct ndis_80211_auth_request)) {
2659 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2660 len);
2661 return;
2664 buf = (void *)&indication->u.auth_request[0];
2665 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2667 while (buflen >= sizeof(*auth_req)) {
2668 auth_req = (void *)buf;
2669 type = "unknown";
2670 flags = le32_to_cpu(auth_req->flags);
2671 pairwise_error = false;
2672 group_error = false;
2674 if (flags & 0x1)
2675 type = "reauth request";
2676 if (flags & 0x2)
2677 type = "key update request";
2678 if (flags & 0x6) {
2679 pairwise_error = true;
2680 type = "pairwise_error";
2682 if (flags & 0xe) {
2683 group_error = true;
2684 type = "group_error";
2687 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2688 type, le32_to_cpu(auth_req->flags));
2690 if (pairwise_error) {
2691 key_type = NL80211_KEYTYPE_PAIRWISE;
2692 key_id = -1;
2694 cfg80211_michael_mic_failure(usbdev->net,
2695 auth_req->bssid,
2696 key_type, key_id, NULL,
2697 GFP_KERNEL);
2700 if (group_error) {
2701 key_type = NL80211_KEYTYPE_GROUP;
2702 key_id = -1;
2704 cfg80211_michael_mic_failure(usbdev->net,
2705 auth_req->bssid,
2706 key_type, key_id, NULL,
2707 GFP_KERNEL);
2710 buflen -= le32_to_cpu(auth_req->length);
2711 buf += le32_to_cpu(auth_req->length);
2715 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2716 struct ndis_80211_status_indication *indication,
2717 int len)
2719 struct ndis_80211_pmkid_cand_list *cand_list;
2720 int list_len, expected_len, i;
2722 if (len < offsetof(struct ndis_80211_status_indication, u) +
2723 sizeof(struct ndis_80211_pmkid_cand_list)) {
2724 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2725 len);
2726 return;
2729 list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2730 sizeof(struct ndis_80211_pmkid_candidate);
2731 expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2732 offsetof(struct ndis_80211_status_indication, u);
2734 if (len < expected_len) {
2735 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2736 len, expected_len);
2737 return;
2740 cand_list = &indication->u.cand_list;
2742 netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2743 le32_to_cpu(cand_list->version),
2744 le32_to_cpu(cand_list->num_candidates));
2746 if (le32_to_cpu(cand_list->version) != 1)
2747 return;
2749 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2750 struct ndis_80211_pmkid_candidate *cand =
2751 &cand_list->candidate_list[i];
2753 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2754 i, le32_to_cpu(cand->flags), cand->bssid);
2759 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2760 struct rndis_indicate *msg, int buflen)
2762 struct ndis_80211_status_indication *indication;
2763 int len, offset;
2765 offset = offsetof(struct rndis_indicate, status) +
2766 le32_to_cpu(msg->offset);
2767 len = le32_to_cpu(msg->length);
2769 if (len < 8) {
2770 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2771 len);
2772 return;
2775 if (offset + len > buflen) {
2776 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2777 offset + len, buflen);
2778 return;
2781 indication = (void *)((u8 *)msg + offset);
2783 switch (le32_to_cpu(indication->status_type)) {
2784 case NDIS_80211_STATUSTYPE_RADIOSTATE:
2785 netdev_info(usbdev->net, "radio state indication: %i\n",
2786 le32_to_cpu(indication->u.radio_status));
2787 return;
2789 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
2790 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2791 le32_to_cpu(indication->u.media_stream_mode));
2792 return;
2794 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2795 rndis_wlan_auth_indication(usbdev, indication, len);
2796 return;
2798 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2799 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2800 return;
2802 default:
2803 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2804 le32_to_cpu(indication->status_type));
2808 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
2810 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2811 struct rndis_indicate *msg = ind;
2813 switch (msg->status) {
2814 case RNDIS_STATUS_MEDIA_CONNECT:
2815 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2816 /* OID_802_11_ADD_KEY causes sometimes extra
2817 * "media connect" indications which confuses driver
2818 * and userspace to think that device is
2819 * roaming/reassociating when it isn't.
2821 netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
2822 return;
2825 usbnet_pause_rx(usbdev);
2827 netdev_info(usbdev->net, "media connect\n");
2829 /* queue work to avoid recursive calls into rndis_command */
2830 set_bit(WORK_LINK_UP, &priv->work_pending);
2831 queue_work(priv->workqueue, &priv->work);
2832 break;
2834 case RNDIS_STATUS_MEDIA_DISCONNECT:
2835 netdev_info(usbdev->net, "media disconnect\n");
2837 /* queue work to avoid recursive calls into rndis_command */
2838 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2839 queue_work(priv->workqueue, &priv->work);
2840 break;
2842 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2843 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2844 break;
2846 default:
2847 netdev_info(usbdev->net, "indication: 0x%08x\n",
2848 le32_to_cpu(msg->status));
2849 break;
2853 static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
2855 struct {
2856 __le32 num_items;
2857 __le32 items[8];
2858 } networks_supported;
2859 struct ndis_80211_capability *caps;
2860 u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
2861 int len, retval, i, n;
2862 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2864 /* determine supported modes */
2865 len = sizeof(networks_supported);
2866 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
2867 &networks_supported, &len);
2868 if (retval >= 0) {
2869 n = le32_to_cpu(networks_supported.num_items);
2870 if (n > 8)
2871 n = 8;
2872 for (i = 0; i < n; i++) {
2873 switch (le32_to_cpu(networks_supported.items[i])) {
2874 case NDIS_80211_TYPE_FREQ_HOP:
2875 case NDIS_80211_TYPE_DIRECT_SEQ:
2876 priv->caps |= CAP_MODE_80211B;
2877 break;
2878 case NDIS_80211_TYPE_OFDM_A:
2879 priv->caps |= CAP_MODE_80211A;
2880 break;
2881 case NDIS_80211_TYPE_OFDM_G:
2882 priv->caps |= CAP_MODE_80211G;
2883 break;
2888 /* get device 802.11 capabilities, number of PMKIDs */
2889 caps = (struct ndis_80211_capability *)caps_buf;
2890 len = sizeof(caps_buf);
2891 retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
2892 if (retval >= 0) {
2893 netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
2894 "ver %d, pmkids %d, auth-encr-pairs %d\n",
2895 le32_to_cpu(caps->length),
2896 le32_to_cpu(caps->version),
2897 le32_to_cpu(caps->num_pmkids),
2898 le32_to_cpu(caps->num_auth_encr_pair));
2899 wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
2900 } else
2901 wiphy->max_num_pmkids = 0;
2903 return retval;
2906 #define DEVICE_POLLER_JIFFIES (HZ)
2907 static void rndis_device_poller(struct work_struct *work)
2909 struct rndis_wlan_private *priv =
2910 container_of(work, struct rndis_wlan_private,
2911 dev_poller_work.work);
2912 struct usbnet *usbdev = priv->usbdev;
2913 __le32 rssi, tmp;
2914 int len, ret, j;
2915 int update_jiffies = DEVICE_POLLER_JIFFIES;
2916 void *buf;
2918 if (!is_associated(usbdev))
2919 goto end;
2921 len = sizeof(rssi);
2922 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2923 if (ret == 0)
2924 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
2926 netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
2927 ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
2929 if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
2930 /* Decrease stats worker interval to catch stalls.
2931 * faster. Faster than 400-500ms causes packet loss,
2932 * Slower doesn't catch stalls fast enough.
2934 j = msecs_to_jiffies(priv->param_workaround_interval);
2935 if (j > DEVICE_POLLER_JIFFIES)
2936 j = DEVICE_POLLER_JIFFIES;
2937 else if (j <= 0)
2938 j = 1;
2939 update_jiffies = j;
2941 /* Send scan OID. Use of both OIDs is required to get device
2942 * working.
2944 tmp = cpu_to_le32(1);
2945 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2946 sizeof(tmp));
2948 len = CONTROL_BUFFER_SIZE;
2949 buf = kmalloc(len, GFP_KERNEL);
2950 if (!buf)
2951 goto end;
2953 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2954 kfree(buf);
2957 end:
2958 if (update_jiffies >= HZ)
2959 update_jiffies = round_jiffies_relative(update_jiffies);
2960 else {
2961 j = round_jiffies_relative(update_jiffies);
2962 if (abs(j - update_jiffies) <= 10)
2963 update_jiffies = j;
2966 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2967 update_jiffies);
2971 * driver/device initialization
2973 static void rndis_copy_module_params(struct usbnet *usbdev)
2975 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2977 priv->param_country[0] = modparam_country[0];
2978 priv->param_country[1] = modparam_country[1];
2979 priv->param_country[2] = 0;
2980 priv->param_frameburst = modparam_frameburst;
2981 priv->param_afterburner = modparam_afterburner;
2982 priv->param_power_save = modparam_power_save;
2983 priv->param_power_output = modparam_power_output;
2984 priv->param_roamtrigger = modparam_roamtrigger;
2985 priv->param_roamdelta = modparam_roamdelta;
2987 priv->param_country[0] = toupper(priv->param_country[0]);
2988 priv->param_country[1] = toupper(priv->param_country[1]);
2989 /* doesn't support EU as country code, use FI instead */
2990 if (!strcmp(priv->param_country, "EU"))
2991 strcpy(priv->param_country, "FI");
2993 if (priv->param_power_save < 0)
2994 priv->param_power_save = 0;
2995 else if (priv->param_power_save > 2)
2996 priv->param_power_save = 2;
2998 if (priv->param_power_output < 0)
2999 priv->param_power_output = 0;
3000 else if (priv->param_power_output > 3)
3001 priv->param_power_output = 3;
3003 if (priv->param_roamtrigger < -80)
3004 priv->param_roamtrigger = -80;
3005 else if (priv->param_roamtrigger > -60)
3006 priv->param_roamtrigger = -60;
3008 if (priv->param_roamdelta < 0)
3009 priv->param_roamdelta = 0;
3010 else if (priv->param_roamdelta > 2)
3011 priv->param_roamdelta = 2;
3013 if (modparam_workaround_interval < 0)
3014 priv->param_workaround_interval = 500;
3015 else
3016 priv->param_workaround_interval = modparam_workaround_interval;
3019 static int bcm4320a_early_init(struct usbnet *usbdev)
3021 rndis_copy_module_params(usbdev);
3023 /* bcm4320a doesn't handle configuration parameters well. Try
3024 * set any and you get partially zeroed mac and broken device.
3027 return 0;
3030 static int bcm4320b_early_init(struct usbnet *usbdev)
3032 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3033 char buf[8];
3035 rndis_copy_module_params(usbdev);
3037 /* Early initialization settings, setting these won't have effect
3038 * if called after generic_rndis_bind().
3041 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3042 rndis_set_config_parameter_str(usbdev, "FrameBursting",
3043 priv->param_frameburst ? "1" : "0");
3044 rndis_set_config_parameter_str(usbdev, "Afterburner",
3045 priv->param_afterburner ? "1" : "0");
3046 sprintf(buf, "%d", priv->param_power_save);
3047 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3048 sprintf(buf, "%d", priv->param_power_output);
3049 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3050 sprintf(buf, "%d", priv->param_roamtrigger);
3051 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3052 sprintf(buf, "%d", priv->param_roamdelta);
3053 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3055 return 0;
3058 /* same as rndis_netdev_ops but with local multicast handler */
3059 static const struct net_device_ops rndis_wlan_netdev_ops = {
3060 .ndo_open = usbnet_open,
3061 .ndo_stop = usbnet_stop,
3062 .ndo_start_xmit = usbnet_start_xmit,
3063 .ndo_tx_timeout = usbnet_tx_timeout,
3064 .ndo_set_mac_address = eth_mac_addr,
3065 .ndo_validate_addr = eth_validate_addr,
3066 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
3069 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3071 struct wiphy *wiphy;
3072 struct rndis_wlan_private *priv;
3073 int retval, len;
3074 __le32 tmp;
3076 /* allocate wiphy and rndis private data
3077 * NOTE: We only support a single virtual interface, so wiphy
3078 * and wireless_dev are somewhat synonymous for this device.
3080 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3081 if (!wiphy)
3082 return -ENOMEM;
3084 priv = wiphy_priv(wiphy);
3085 usbdev->net->ieee80211_ptr = &priv->wdev;
3086 priv->wdev.wiphy = wiphy;
3087 priv->wdev.iftype = NL80211_IFTYPE_STATION;
3089 /* These have to be initialized before calling generic_rndis_bind().
3090 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3092 usbdev->driver_priv = priv;
3093 priv->usbdev = usbdev;
3095 mutex_init(&priv->command_lock);
3097 /* because rndis_command() sleeps we need to use workqueue */
3098 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3099 INIT_WORK(&priv->work, rndis_wlan_worker);
3100 INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3101 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3103 /* try bind rndis_host */
3104 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3105 if (retval < 0)
3106 goto fail;
3108 /* generic_rndis_bind set packet filter to multicast_all+
3109 * promisc mode which doesn't work well for our devices (device
3110 * picks up rssi to closest station instead of to access point).
3112 * rndis_host wants to avoid all OID as much as possible
3113 * so do promisc/multicast handling in rndis_wlan.
3115 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3117 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
3118 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
3119 sizeof(tmp));
3121 len = sizeof(tmp);
3122 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3123 &len);
3124 priv->multicast_size = le32_to_cpu(tmp);
3125 if (retval < 0 || priv->multicast_size < 0)
3126 priv->multicast_size = 0;
3127 if (priv->multicast_size > 0)
3128 usbdev->net->flags |= IFF_MULTICAST;
3129 else
3130 usbdev->net->flags &= ~IFF_MULTICAST;
3132 /* fill-out wiphy structure and register w/ cfg80211 */
3133 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3134 wiphy->privid = rndis_wiphy_privid;
3135 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3136 | BIT(NL80211_IFTYPE_ADHOC);
3137 wiphy->max_scan_ssids = 1;
3139 /* TODO: fill-out band/encr information based on priv->caps */
3140 rndis_wlan_get_caps(usbdev, wiphy);
3142 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3143 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3144 priv->band.channels = priv->channels;
3145 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3146 priv->band.bitrates = priv->rates;
3147 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3148 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3149 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3151 memcpy(priv->cipher_suites, rndis_cipher_suites,
3152 sizeof(rndis_cipher_suites));
3153 wiphy->cipher_suites = priv->cipher_suites;
3154 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3156 set_wiphy_dev(wiphy, &usbdev->udev->dev);
3158 if (wiphy_register(wiphy)) {
3159 retval = -ENODEV;
3160 goto fail;
3163 set_default_iw_params(usbdev);
3165 /* set default rts/frag */
3166 rndis_set_wiphy_params(wiphy,
3167 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3169 /* turn radio on */
3170 priv->radio_on = true;
3171 disassociate(usbdev, true);
3172 netif_carrier_off(usbdev->net);
3174 return 0;
3176 fail:
3177 cancel_delayed_work_sync(&priv->dev_poller_work);
3178 cancel_delayed_work_sync(&priv->scan_work);
3179 cancel_work_sync(&priv->work);
3180 flush_workqueue(priv->workqueue);
3181 destroy_workqueue(priv->workqueue);
3183 wiphy_free(wiphy);
3184 return retval;
3187 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3189 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3191 /* turn radio off */
3192 disassociate(usbdev, false);
3194 cancel_delayed_work_sync(&priv->dev_poller_work);
3195 cancel_delayed_work_sync(&priv->scan_work);
3196 cancel_work_sync(&priv->work);
3197 flush_workqueue(priv->workqueue);
3198 destroy_workqueue(priv->workqueue);
3200 rndis_unbind(usbdev, intf);
3202 wiphy_unregister(priv->wdev.wiphy);
3203 wiphy_free(priv->wdev.wiphy);
3206 static int rndis_wlan_reset(struct usbnet *usbdev)
3208 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3209 int retval;
3211 netdev_dbg(usbdev->net, "%s()\n", __func__);
3213 retval = rndis_reset(usbdev);
3214 if (retval)
3215 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3217 /* rndis_reset cleared multicast list, so restore here.
3218 (set_multicast_list() also turns on current packet filter) */
3219 set_multicast_list(usbdev);
3221 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3222 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3224 return deauthenticate(usbdev);
3227 static int rndis_wlan_stop(struct usbnet *usbdev)
3229 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3230 int retval;
3231 __le32 filter;
3233 netdev_dbg(usbdev->net, "%s()\n", __func__);
3235 retval = disassociate(usbdev, false);
3237 priv->work_pending = 0;
3238 cancel_delayed_work_sync(&priv->dev_poller_work);
3239 cancel_delayed_work_sync(&priv->scan_work);
3240 cancel_work_sync(&priv->work);
3241 flush_workqueue(priv->workqueue);
3243 if (priv->scan_request) {
3244 cfg80211_scan_done(priv->scan_request, true);
3245 priv->scan_request = NULL;
3248 /* Set current packet filter zero to block receiving data packets from
3249 device. */
3250 filter = 0;
3251 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3252 sizeof(filter));
3254 return retval;
3257 static const struct driver_info bcm4320b_info = {
3258 .description = "Wireless RNDIS device, BCM4320b based",
3259 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3260 FLAG_AVOID_UNLINK_URBS,
3261 .bind = rndis_wlan_bind,
3262 .unbind = rndis_wlan_unbind,
3263 .status = rndis_status,
3264 .rx_fixup = rndis_rx_fixup,
3265 .tx_fixup = rndis_tx_fixup,
3266 .reset = rndis_wlan_reset,
3267 .stop = rndis_wlan_stop,
3268 .early_init = bcm4320b_early_init,
3269 .indication = rndis_wlan_indication,
3272 static const struct driver_info bcm4320a_info = {
3273 .description = "Wireless RNDIS device, BCM4320a based",
3274 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3275 FLAG_AVOID_UNLINK_URBS,
3276 .bind = rndis_wlan_bind,
3277 .unbind = rndis_wlan_unbind,
3278 .status = rndis_status,
3279 .rx_fixup = rndis_rx_fixup,
3280 .tx_fixup = rndis_tx_fixup,
3281 .reset = rndis_wlan_reset,
3282 .stop = rndis_wlan_stop,
3283 .early_init = bcm4320a_early_init,
3284 .indication = rndis_wlan_indication,
3287 static const struct driver_info rndis_wlan_info = {
3288 .description = "Wireless RNDIS device",
3289 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3290 FLAG_AVOID_UNLINK_URBS,
3291 .bind = rndis_wlan_bind,
3292 .unbind = rndis_wlan_unbind,
3293 .status = rndis_status,
3294 .rx_fixup = rndis_rx_fixup,
3295 .tx_fixup = rndis_tx_fixup,
3296 .reset = rndis_wlan_reset,
3297 .stop = rndis_wlan_stop,
3298 .early_init = bcm4320a_early_init,
3299 .indication = rndis_wlan_indication,
3302 /*-------------------------------------------------------------------------*/
3304 static const struct usb_device_id products [] = {
3305 #define RNDIS_MASTER_INTERFACE \
3306 .bInterfaceClass = USB_CLASS_COMM, \
3307 .bInterfaceSubClass = 2 /* ACM */, \
3308 .bInterfaceProtocol = 0x0ff
3310 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3311 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3314 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3315 | USB_DEVICE_ID_MATCH_DEVICE,
3316 .idVendor = 0x0411,
3317 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
3318 RNDIS_MASTER_INTERFACE,
3319 .driver_info = (unsigned long) &bcm4320b_info,
3320 }, {
3321 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3322 | USB_DEVICE_ID_MATCH_DEVICE,
3323 .idVendor = 0x0baf,
3324 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
3325 RNDIS_MASTER_INTERFACE,
3326 .driver_info = (unsigned long) &bcm4320b_info,
3327 }, {
3328 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3329 | USB_DEVICE_ID_MATCH_DEVICE,
3330 .idVendor = 0x050d,
3331 .idProduct = 0x011b, /* Belkin F5D7051 */
3332 RNDIS_MASTER_INTERFACE,
3333 .driver_info = (unsigned long) &bcm4320b_info,
3334 }, {
3335 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3336 | USB_DEVICE_ID_MATCH_DEVICE,
3337 .idVendor = 0x1799, /* Belkin has two vendor ids */
3338 .idProduct = 0x011b, /* Belkin F5D7051 */
3339 RNDIS_MASTER_INTERFACE,
3340 .driver_info = (unsigned long) &bcm4320b_info,
3341 }, {
3342 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3343 | USB_DEVICE_ID_MATCH_DEVICE,
3344 .idVendor = 0x13b1,
3345 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
3346 RNDIS_MASTER_INTERFACE,
3347 .driver_info = (unsigned long) &bcm4320b_info,
3348 }, {
3349 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3350 | USB_DEVICE_ID_MATCH_DEVICE,
3351 .idVendor = 0x13b1,
3352 .idProduct = 0x0026, /* Linksys WUSB54GSC */
3353 RNDIS_MASTER_INTERFACE,
3354 .driver_info = (unsigned long) &bcm4320b_info,
3355 }, {
3356 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3357 | USB_DEVICE_ID_MATCH_DEVICE,
3358 .idVendor = 0x0b05,
3359 .idProduct = 0x1717, /* Asus WL169gE */
3360 RNDIS_MASTER_INTERFACE,
3361 .driver_info = (unsigned long) &bcm4320b_info,
3362 }, {
3363 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3364 | USB_DEVICE_ID_MATCH_DEVICE,
3365 .idVendor = 0x0a5c,
3366 .idProduct = 0xd11b, /* Eminent EM4045 */
3367 RNDIS_MASTER_INTERFACE,
3368 .driver_info = (unsigned long) &bcm4320b_info,
3369 }, {
3370 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3371 | USB_DEVICE_ID_MATCH_DEVICE,
3372 .idVendor = 0x1690,
3373 .idProduct = 0x0715, /* BT Voyager 1055 */
3374 RNDIS_MASTER_INTERFACE,
3375 .driver_info = (unsigned long) &bcm4320b_info,
3377 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3378 * parameters available, hardware probably contain older firmware version with
3379 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3382 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3383 | USB_DEVICE_ID_MATCH_DEVICE,
3384 .idVendor = 0x13b1,
3385 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3386 RNDIS_MASTER_INTERFACE,
3387 .driver_info = (unsigned long) &bcm4320a_info,
3388 }, {
3389 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3390 | USB_DEVICE_ID_MATCH_DEVICE,
3391 .idVendor = 0x0baf,
3392 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3393 RNDIS_MASTER_INTERFACE,
3394 .driver_info = (unsigned long) &bcm4320a_info,
3395 }, {
3396 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3397 | USB_DEVICE_ID_MATCH_DEVICE,
3398 .idVendor = 0x0411,
3399 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3400 RNDIS_MASTER_INTERFACE,
3401 .driver_info = (unsigned long) &bcm4320a_info,
3403 /* Generic Wireless RNDIS devices that we don't have exact
3404 * idVendor/idProduct/chip yet.
3407 /* RNDIS is MSFT's un-official variant of CDC ACM */
3408 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3409 .driver_info = (unsigned long) &rndis_wlan_info,
3410 }, {
3411 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3412 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3413 .driver_info = (unsigned long) &rndis_wlan_info,
3415 { }, // END
3417 MODULE_DEVICE_TABLE(usb, products);
3419 static struct usb_driver rndis_wlan_driver = {
3420 .name = "rndis_wlan",
3421 .id_table = products,
3422 .probe = usbnet_probe,
3423 .disconnect = usbnet_disconnect,
3424 .suspend = usbnet_suspend,
3425 .resume = usbnet_resume,
3428 static int __init rndis_wlan_init(void)
3430 return usb_register(&rndis_wlan_driver);
3432 module_init(rndis_wlan_init);
3434 static void __exit rndis_wlan_exit(void)
3436 usb_deregister(&rndis_wlan_driver);
3438 module_exit(rndis_wlan_exit);
3440 MODULE_AUTHOR("Bjorge Dijkstra");
3441 MODULE_AUTHOR("Jussi Kivilinna");
3442 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3443 MODULE_LICENSE("GPL");