mac80211: support hardware TX fragmentation offload
[deliverable/linux.git] / drivers / net / wireless / rndis_wlan.c
CommitLineData
bf164cc0
JK
1/*
2 * Driver for RNDIS based wireless USB devices.
3 *
4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
9656e85b 5 * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
bf164cc0
JK
6 *
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.
11 *
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.
16 *
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
20 *
21 * Portions of this file are based on NDISwrapper project,
22 * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
23 * http://ndiswrapper.sourceforge.net/
24 */
25
26// #define DEBUG // error path messages, extra info
27// #define VERBOSE // more; success messages
28
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>
2c706002 40#include <linux/ieee80211.h>
bf164cc0
JK
41#include <linux/if_arp.h>
42#include <linux/ctype.h>
43#include <linux/spinlock.h>
5a0e3ad6 44#include <linux/slab.h>
bf164cc0 45#include <net/iw_handler.h>
5c8fa4f7 46#include <net/cfg80211.h>
bf164cc0
JK
47#include <linux/usb/usbnet.h>
48#include <linux/usb/rndis_host.h>
49
50
51/* NOTE: All these are settings for Broadcom chipset */
52static char modparam_country[4] = "EU";
53module_param_string(country, modparam_country, 4, 0444);
54MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
55
56static int modparam_frameburst = 1;
57module_param_named(frameburst, modparam_frameburst, int, 0444);
58MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
59
60static int modparam_afterburner = 0;
61module_param_named(afterburner, modparam_afterburner, int, 0444);
62MODULE_PARM_DESC(afterburner,
63 "enable afterburner aka '125 High Speed Mode' (default: off)");
64
65static int modparam_power_save = 0;
66module_param_named(power_save, modparam_power_save, int, 0444);
67MODULE_PARM_DESC(power_save,
68 "set power save mode: 0=off, 1=on, 2=fast (default: off)");
69
70static int modparam_power_output = 3;
71module_param_named(power_output, modparam_power_output, int, 0444);
72MODULE_PARM_DESC(power_output,
73 "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
74
75static int modparam_roamtrigger = -70;
76module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
77MODULE_PARM_DESC(roamtrigger,
78 "set roaming dBm trigger: -80=optimize for distance, "
79 "-60=bandwidth (default: -70)");
80
81static int modparam_roamdelta = 1;
82module_param_named(roamdelta, modparam_roamdelta, int, 0444);
83MODULE_PARM_DESC(roamdelta,
84 "set roaming tendency: 0=aggressive, 1=moderate, "
85 "2=conservative (default: moderate)");
86
77593ae2 87static int modparam_workaround_interval;
bf164cc0
JK
88module_param_named(workaround_interval, modparam_workaround_interval,
89 int, 0444);
90MODULE_PARM_DESC(workaround_interval,
77593ae2 91 "set stall workaround interval in msecs (0=disabled) (default: 0)");
bf164cc0
JK
92
93
94/* various RNDIS OID defs */
35c26c2c
HH
95#define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
96#define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
97
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)
103
35c26c2c
HH
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)
107
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)
0308383f 121#define OID_802_11_CAPABILITY cpu_to_le32(0x0d010122)
35c26c2c
HH
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)
bf164cc0
JK
133
134
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 */
138
139
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.
222ec50a
JK
143 * 100% : 20 mW ~ 13dBm
144 * 75% : 15 mW ~ 12dBm
145 * 50% : 10 mW ~ 10dBm
146 * 25% : 5 mW ~ 7dBm
bf164cc0 147 */
222ec50a
JK
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
bf164cc0
JK
152
153
154/* codes for "status" field of completion messages */
35c26c2c
HH
155#define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
156#define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
bf164cc0
JK
157
158
159/* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
160 * slightly modified for datatype endianess, etc
161 */
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
165
1e41e1d2 166enum ndis_80211_net_type {
aa18294a
JK
167 NDIS_80211_TYPE_FREQ_HOP,
168 NDIS_80211_TYPE_DIRECT_SEQ,
169 NDIS_80211_TYPE_OFDM_A,
170 NDIS_80211_TYPE_OFDM_G
1e41e1d2
JK
171};
172
173enum ndis_80211_net_infra {
aa18294a
JK
174 NDIS_80211_INFRA_ADHOC,
175 NDIS_80211_INFRA_INFRA,
176 NDIS_80211_INFRA_AUTO_UNKNOWN
1e41e1d2
JK
177};
178
179enum ndis_80211_auth_mode {
aa18294a
JK
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
1e41e1d2
JK
188};
189
190enum ndis_80211_encr_status {
aa18294a
JK
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
1e41e1d2
JK
199};
200
201enum ndis_80211_priv_filter {
aa18294a
JK
202 NDIS_80211_PRIV_ACCEPT_ALL,
203 NDIS_80211_PRIV_8021X_WEP
1e41e1d2
JK
204};
205
030645ac
JK
206enum 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,
211};
212
213enum ndis_80211_media_stream_mode {
214 NDIS_80211_MEDIA_STREAM_OFF,
215 NDIS_80211_MEDIA_STREAM_ON
216};
217
218enum ndis_80211_radio_status {
219 NDIS_80211_RADIO_STATUS_ON,
220 NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
221 NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
222};
223
b4703a2e 224enum ndis_80211_addkey_bits {
aa18294a
JK
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)
b4703a2e
JK
229};
230
231enum ndis_80211_addwep_bits {
aa18294a
JK
232 NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
233 NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
b4703a2e
JK
234};
235
030645ac
JK
236struct ndis_80211_auth_request {
237 __le32 length;
238 u8 bssid[6];
239 u8 padding[2];
240 __le32 flags;
ba2d3587 241} __packed;
030645ac
JK
242
243struct ndis_80211_pmkid_candidate {
244 u8 bssid[6];
245 u8 padding[2];
246 __le32 flags;
ba2d3587 247} __packed;
030645ac
JK
248
249struct ndis_80211_pmkid_cand_list {
250 __le32 version;
251 __le32 num_candidates;
252 struct ndis_80211_pmkid_candidate candidate_list[0];
ba2d3587 253} __packed;
030645ac
JK
254
255struct ndis_80211_status_indication {
256 __le32 status_type;
257 union {
53d27eaf
JK
258 __le32 media_stream_mode;
259 __le32 radio_status;
030645ac
JK
260 struct ndis_80211_auth_request auth_request[0];
261 struct ndis_80211_pmkid_cand_list cand_list;
262 } u;
ba2d3587 263} __packed;
030645ac 264
1e41e1d2 265struct ndis_80211_ssid {
461b3f2a
JK
266 __le32 length;
267 u8 essid[NDIS_802_11_LENGTH_SSID];
ba2d3587 268} __packed;
bf164cc0 269
1e41e1d2 270struct ndis_80211_conf_freq_hop {
461b3f2a
JK
271 __le32 length;
272 __le32 hop_pattern;
273 __le32 hop_set;
274 __le32 dwell_time;
ba2d3587 275} __packed;
bf164cc0 276
1e41e1d2 277struct ndis_80211_conf {
461b3f2a
JK
278 __le32 length;
279 __le32 beacon_period;
280 __le32 atim_window;
281 __le32 ds_config;
282 struct ndis_80211_conf_freq_hop fh_config;
ba2d3587 283} __packed;
bf164cc0 284
1e41e1d2 285struct ndis_80211_bssid_ex {
461b3f2a
JK
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];
ba2d3587 298} __packed;
bf164cc0 299
1e41e1d2 300struct ndis_80211_bssid_list_ex {
461b3f2a
JK
301 __le32 num_items;
302 struct ndis_80211_bssid_ex bssid[0];
ba2d3587 303} __packed;
bf164cc0 304
1e41e1d2 305struct ndis_80211_fixed_ies {
461b3f2a
JK
306 u8 timestamp[8];
307 __le16 beacon_interval;
308 __le16 capabilities;
ba2d3587 309} __packed;
bf164cc0 310
1e41e1d2 311struct ndis_80211_wep_key {
461b3f2a
JK
312 __le32 size;
313 __le32 index;
314 __le32 length;
315 u8 material[32];
ba2d3587 316} __packed;
bf164cc0 317
1e41e1d2 318struct ndis_80211_key {
461b3f2a
JK
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];
ba2d3587 326} __packed;
bf164cc0 327
1e41e1d2 328struct ndis_80211_remove_key {
461b3f2a
JK
329 __le32 size;
330 __le32 index;
331 u8 bssid[6];
9d40934e 332 u8 padding[2];
ba2d3587 333} __packed;
bf164cc0 334
1e41e1d2 335struct ndis_config_param {
461b3f2a
JK
336 __le32 name_offs;
337 __le32 name_length;
338 __le32 type;
339 __le32 value_offs;
340 __le32 value_length;
ba2d3587 341} __packed;
bf164cc0 342
4364623c
SA
343struct 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;
ba2d3587 361} __packed;
4364623c 362
0308383f
JK
363struct ndis_80211_auth_encr_pair {
364 __le32 auth_mode;
365 __le32 encr_mode;
ba2d3587 366} __packed;
0308383f
JK
367
368struct 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];
ba2d3587 374} __packed;
0308383f 375
5a7d0583
JK
376struct ndis_80211_bssid_info {
377 u8 bssid[6];
378 u8 pmkid[16];
379};
380
381struct ndis_80211_pmkid {
382 __le32 length;
383 __le32 bssid_info_count;
384 struct ndis_80211_bssid_info bssid_info[0];
385};
386
bf164cc0
JK
387/*
388 * private data
389 */
390#define NET_TYPE_11FB 0
391
392#define CAP_MODE_80211A 1
393#define CAP_MODE_80211B 2
394#define CAP_MODE_80211G 4
395#define CAP_MODE_MASK 7
bf164cc0 396
6010ce07
JK
397#define WORK_LINK_UP (1<<0)
398#define WORK_LINK_DOWN (1<<1)
399#define WORK_SET_MULTICAST_LIST (1<<2)
bf164cc0 400
5c52323e
JK
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)
405
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)
409
90d07349
JK
410#define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
411
5c8fa4f7
JL
412static 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 },
427};
428
429static 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 }
442};
443
4a7f13ee
JK
444static 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,
449};
450
b7cfc5b3
JK
451struct rndis_wlan_encr_key {
452 int len;
84bf8400 453 u32 cipher;
b7cfc5b3
JK
454 u8 material[32];
455 u8 bssid[ETH_ALEN];
456 bool pairwise;
457 bool tx_key;
458};
459
bf164cc0 460/* RNDIS device private data */
582241a0 461struct rndis_wlan_private {
bf164cc0
JK
462 struct usbnet *usbdev;
463
5c8fa4f7
JL
464 struct wireless_dev wdev;
465
71a7b26d
JK
466 struct cfg80211_scan_request *scan_request;
467
bf164cc0 468 struct workqueue_struct *workqueue;
305e243e 469 struct delayed_work dev_poller_work;
71a7b26d 470 struct delayed_work scan_work;
bf164cc0
JK
471 struct work_struct work;
472 struct mutex command_lock;
bf164cc0 473 unsigned long work_pending;
8b89a288 474 int last_qual;
bf164cc0 475
5c8fa4f7
JL
476 struct ieee80211_supported_band band;
477 struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
478 struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
4a7f13ee 479 u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
5c8fa4f7 480
bf164cc0
JK
481 int caps;
482 int multicast_size;
483
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;
493
494 /* hardware state */
051ae0bf 495 bool radio_on;
bf164cc0 496 int infra_mode;
5c52323e 497 bool connected;
8b89a288 498 u8 bssid[ETH_ALEN];
1e41e1d2 499 struct ndis_80211_ssid essid;
5f81ff5a 500 __le32 current_command_oid;
bf164cc0
JK
501
502 /* encryption stuff */
503 int encr_tx_key_index;
b7cfc5b3 504 struct rndis_wlan_encr_key encr_keys[4];
bf164cc0 505 int wpa_version;
90d07349
JK
506
507 u8 command_buffer[COMMAND_BUFFER_SIZE];
bf164cc0
JK
508};
509
964c1d41
JL
510/*
511 * cfg80211 ops
512 */
e36d56b6
JB
513static int rndis_change_virtual_intf(struct wiphy *wiphy,
514 struct net_device *dev,
964c1d41
JL
515 enum nl80211_iftype type, u32 *flags,
516 struct vif_params *params);
517
71a7b26d
JK
518static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
519 struct cfg80211_scan_request *request);
520
d75ec2b7
JK
521static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
522
fa61cf70
JO
523static int rndis_set_tx_power(struct wiphy *wiphy,
524 enum nl80211_tx_power_setting type,
525 int mbm);
222ec50a
JK
526static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm);
527
5c52323e
JK
528static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
529 struct cfg80211_connect_params *sme);
530
531static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
532 u16 reason_code);
533
534static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
535 struct cfg80211_ibss_params *params);
536
537static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
538
f444de05 539static int rndis_set_channel(struct wiphy *wiphy, struct net_device *dev,
5554adbe
JK
540 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
541
84bf8400 542static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
e31b8213
JB
543 u8 key_index, bool pairwise, const u8 *mac_addr,
544 struct key_params *params);
84bf8400
JK
545
546static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
e31b8213 547 u8 key_index, bool pairwise, const u8 *mac_addr);
84bf8400
JK
548
549static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
550 u8 key_index);
551
8b89a288
JK
552static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
553 u8 *mac, struct station_info *sinfo);
554
d695df90
JK
555static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
556 int idx, u8 *mac, struct station_info *sinfo);
557
5a7d0583
JK
558static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
559 struct cfg80211_pmksa *pmksa);
560
561static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
562 struct cfg80211_pmksa *pmksa);
563
564static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
565
caa6dfad 566static struct cfg80211_ops rndis_config_ops = {
964c1d41 567 .change_virtual_intf = rndis_change_virtual_intf,
71a7b26d 568 .scan = rndis_scan,
d75ec2b7 569 .set_wiphy_params = rndis_set_wiphy_params,
222ec50a
JK
570 .set_tx_power = rndis_set_tx_power,
571 .get_tx_power = rndis_get_tx_power,
5c52323e
JK
572 .connect = rndis_connect,
573 .disconnect = rndis_disconnect,
574 .join_ibss = rndis_join_ibss,
575 .leave_ibss = rndis_leave_ibss,
5554adbe 576 .set_channel = rndis_set_channel,
84bf8400
JK
577 .add_key = rndis_add_key,
578 .del_key = rndis_del_key,
579 .set_default_key = rndis_set_default_key,
8b89a288 580 .get_station = rndis_get_station,
d695df90 581 .dump_station = rndis_dump_station,
5a7d0583
JK
582 .set_pmksa = rndis_set_pmksa,
583 .del_pmksa = rndis_del_pmksa,
584 .flush_pmksa = rndis_flush_pmksa,
964c1d41 585};
bf164cc0 586
caa6dfad 587static void *rndis_wiphy_privid = &rndis_wiphy_privid;
bf164cc0 588
bf164cc0 589
582241a0 590static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
bf164cc0 591{
582241a0 592 return (struct rndis_wlan_private *)dev->driver_priv;
bf164cc0
JK
593}
594
222ec50a 595static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
bf164cc0 596{
222ec50a
JK
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;
607 }
bf164cc0
JK
608}
609
b7cfc5b3
JK
610static bool is_wpa_key(struct rndis_wlan_private *priv, int idx)
611{
612 int cipher = priv->encr_keys[idx].cipher;
613
614 return (cipher == WLAN_CIPHER_SUITE_CCMP ||
615 cipher == WLAN_CIPHER_SUITE_TKIP);
616}
617
5c52323e
JK
618static int rndis_cipher_to_alg(u32 cipher)
619{
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;
630 }
631}
632
633static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
634{
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;
642 }
643}
644
27b7b5c1
JK
645#ifdef DEBUG
646static const char *oid_to_string(__le32 oid)
647{
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);
655
656 /* from rndis_wlan.c */
657 OID_STR(OID_GEN_LINK_SPEED);
658 OID_STR(OID_GEN_RNDIS_CONFIG_PARAMETER);
659
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);
665
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);
669
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
695 }
696
697 return "?";
698}
699#else
700static const char *oid_to_string(__le32 oid)
701{
702 return "?";
703}
704#endif
705
bf164cc0
JK
706/* translate error code */
707static int rndis_error_status(__le32 rndis_status)
708{
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;
725 }
726 return ret;
727}
728
bf164cc0
JK
729static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
730{
582241a0 731 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
bf164cc0
JK
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;
80f8c5b4 739 int resplen, respoffs, copylen;
bf164cc0
JK
740
741 buflen = *len + sizeof(*u.get);
742 if (buflen < CONTROL_BUFFER_SIZE)
743 buflen = CONTROL_BUFFER_SIZE;
90d07349
JK
744
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;
751 }
752
753 mutex_lock(&priv->command_lock);
754
bf164cc0
JK
755 memset(u.get, 0, sizeof *u.get);
756 u.get->msg_type = RNDIS_MSG_QUERY;
35c26c2c 757 u.get->msg_len = cpu_to_le32(sizeof *u.get);
bf164cc0
JK
758 u.get->oid = oid;
759
5f81ff5a 760 priv->current_command_oid = oid;
818727ba 761 ret = rndis_command(dev, u.header, buflen);
5f81ff5a 762 priv->current_command_oid = 0;
27b7b5c1 763 if (ret < 0)
60b86755
JP
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));
27b7b5c1 767
bf164cc0 768 if (ret == 0) {
80f8c5b4
JK
769 resplen = le32_to_cpu(u.get_c->len);
770 respoffs = le32_to_cpu(u.get_c->offset) + 8;
c1f8ca1d 771
80f8c5b4
JK
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);
c1f8ca1d 777
80f8c5b4
JK
778 ret = -EINVAL;
779 goto exit_unlock;
780 }
781
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.
785 */
786 copylen = buflen - respoffs;
787 } else {
788 copylen = resplen;
789 }
790
791 if (copylen > *len)
792 copylen = *len;
793
794 memcpy(data, u.buf + respoffs, copylen);
795
796 *len = resplen;
27b7b5c1 797
c1f8ca1d 798 ret = rndis_error_status(u.get_c->status);
27b7b5c1 799 if (ret < 0)
60b86755
JP
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);
bf164cc0
JK
803 }
804
80f8c5b4 805exit_unlock:
90d07349
JK
806 mutex_unlock(&priv->command_lock);
807
808 if (u.buf != priv->command_buffer)
809 kfree(u.buf);
bf164cc0
JK
810 return ret;
811}
812
bf164cc0
JK
813static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
814{
582241a0 815 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
bf164cc0
JK
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;
823
824 buflen = len + sizeof(*u.set);
825 if (buflen < CONTROL_BUFFER_SIZE)
826 buflen = CONTROL_BUFFER_SIZE;
90d07349
JK
827
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;
834 }
835
836 mutex_lock(&priv->command_lock);
bf164cc0
JK
837
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);
35c26c2c
HH
843 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
844 u.set->handle = cpu_to_le32(0);
bf164cc0
JK
845 memcpy(u.buf + sizeof(*u.set), data, len);
846
5f81ff5a 847 priv->current_command_oid = oid;
818727ba 848 ret = rndis_command(dev, u.header, buflen);
5f81ff5a 849 priv->current_command_oid = 0;
27b7b5c1 850 if (ret < 0)
60b86755
JP
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));
27b7b5c1
JK
854
855 if (ret == 0) {
bf164cc0
JK
856 ret = rndis_error_status(u.set_c->status);
857
27b7b5c1 858 if (ret < 0)
60b86755
JP
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);
27b7b5c1
JK
862 }
863
90d07349
JK
864 mutex_unlock(&priv->command_lock);
865
866 if (u.buf != priv->command_buffer)
867 kfree(u.buf);
bf164cc0
JK
868 return ret;
869}
870
7eaab708
JK
871static int rndis_reset(struct usbnet *usbdev)
872{
873 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
874 struct rndis_reset *reset;
875 int ret;
876
877 mutex_lock(&priv->command_lock);
878
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));
5f81ff5a 883 priv->current_command_oid = 0;
7eaab708
JK
884 ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
885
886 mutex_unlock(&priv->command_lock);
887
888 if (ret < 0)
889 return ret;
890 return 0;
891}
892
bf164cc0
JK
893/*
894 * Specs say that we can only set config parameters only soon after device
895 * initialization.
896 * value_type: 0 = u32, 2 = unicode string
897 */
898static int rndis_set_config_parameter(struct usbnet *dev, char *param,
899 int value_type, void *value)
900{
1e41e1d2 901 struct ndis_config_param *infobuf;
bf164cc0
JK
902 int value_len, info_len, param_len, ret, i;
903 __le16 *unibuf;
904 __le32 *dst_value;
905
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;
912
913 param_len = strlen(param) * sizeof(__le16);
914 info_len = sizeof(*infobuf) + param_len + value_len;
915
916#ifdef DEBUG
917 info_len += 12;
918#endif
919 infobuf = kmalloc(info_len, GFP_KERNEL);
920 if (!infobuf)
921 return -ENOMEM;
922
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
928
929 if (value_type == 2)
60b86755
JP
930 netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
931 param, (u8 *)value);
bf164cc0 932 else
60b86755
JP
933 netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
934 param, *(u32 *)value);
bf164cc0 935
461b3f2a
JK
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);
bf164cc0
JK
941
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]);
946
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);
954 }
955
956#ifdef DEBUG
60b86755 957 netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
bf164cc0
JK
958 for (i = 0; i < info_len; i += 12) {
959 u32 *tmp = (u32 *)((u8 *)infobuf + i);
60b86755
JP
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]));
bf164cc0
JK
964 }
965#endif
966
967 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
968 infobuf, info_len);
969 if (ret != 0)
60b86755
JP
970 netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
971 ret);
bf164cc0
JK
972
973 kfree(infobuf);
974 return ret;
975}
976
977static int rndis_set_config_parameter_str(struct usbnet *dev,
978 char *param, char *value)
979{
c5c4fe90 980 return rndis_set_config_parameter(dev, param, 2, value);
bf164cc0
JK
981}
982
bf164cc0
JK
983/*
984 * data conversion functions
985 */
986static int level_to_qual(int level)
987{
988 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
989 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
990}
991
bf164cc0
JK
992/*
993 * common functions
994 */
9f77ccab 995static int set_infra_mode(struct usbnet *usbdev, int mode);
b7cfc5b3 996static void restore_keys(struct usbnet *usbdev);
db0dd396 997static int rndis_check_bssid_list(struct usbnet *usbdev);
bf164cc0 998
1e41e1d2 999static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
bf164cc0 1000{
582241a0 1001 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
1002 int ret;
1003
1004 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
5c52323e 1005 if (ret < 0) {
60b86755 1006 netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
5c52323e
JK
1007 return ret;
1008 }
bf164cc0
JK
1009 if (ret == 0) {
1010 memcpy(&priv->essid, ssid, sizeof(priv->essid));
051ae0bf 1011 priv->radio_on = true;
60b86755 1012 netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
bf164cc0
JK
1013 }
1014
1015 return ret;
1016}
1017
5c52323e
JK
1018static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1019{
1020 int ret;
1021
1022 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
1023 if (ret < 0) {
60b86755
JP
1024 netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1025 bssid, ret);
5c52323e
JK
1026 return ret;
1027 }
1028
1029 return ret;
1030}
1031
1032static int clear_bssid(struct usbnet *usbdev)
1033{
1034 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1035
1036 return set_bssid(usbdev, broadcast_mac);
1037}
bf164cc0
JK
1038
1039static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1040{
1041 int ret, len;
1042
1043 len = ETH_ALEN;
1044 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
1045
1046 if (ret != 0)
1047 memset(bssid, 0, ETH_ALEN);
1048
1049 return ret;
1050}
1051
4364623c
SA
1052static int get_association_info(struct usbnet *usbdev,
1053 struct ndis_80211_assoc_info *info, int len)
1054{
1055 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
1056 info, &len);
1057}
bf164cc0 1058
5c52323e 1059static bool is_associated(struct usbnet *usbdev)
bf164cc0 1060{
5c52323e 1061 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
1062 u8 bssid[ETH_ALEN];
1063 int ret;
1064
5c52323e
JK
1065 if (!priv->radio_on)
1066 return false;
1067
bf164cc0
JK
1068 ret = get_bssid(usbdev, bssid);
1069
7b1fff99 1070 return (ret == 0 && !is_zero_ether_addr(bssid));
bf164cc0
JK
1071}
1072
051ae0bf 1073static int disassociate(struct usbnet *usbdev, bool reset_ssid)
bf164cc0 1074{
582241a0 1075 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1e41e1d2 1076 struct ndis_80211_ssid ssid;
bf164cc0
JK
1077 int i, ret = 0;
1078
1079 if (priv->radio_on) {
1080 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1081 if (ret == 0) {
051ae0bf 1082 priv->radio_on = false;
60b86755
JP
1083 netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1084 __func__);
bf164cc0
JK
1085
1086 if (reset_ssid)
1087 msleep(100);
1088 }
1089 }
1090
1091 /* disassociate causes radio to be turned off; if reset_ssid
1092 * is given, set random ssid to enable radio */
1093 if (reset_ssid) {
9f77ccab
JK
1094 /* Set device to infrastructure mode so we don't get ad-hoc
1095 * 'media connect' indications with the random ssid.
1096 */
1097 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1098
461b3f2a
JK
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);
bf164cc0
JK
1105 ret = set_essid(usbdev, &ssid);
1106 }
1107 return ret;
1108}
1109
5c52323e
JK
1110static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1111 enum nl80211_auth_type auth_type, int keymgmt)
bf164cc0 1112{
582241a0 1113 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
1114 __le32 tmp;
1115 int auth_mode, ret;
1116
60b86755
JP
1117 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1118 __func__, wpa_version, auth_type, keymgmt);
bf164cc0 1119
5c52323e
JK
1120 if (wpa_version & NL80211_WPA_VERSION_2) {
1121 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
aa18294a 1122 auth_mode = NDIS_80211_AUTH_WPA2;
bf164cc0 1123 else
aa18294a 1124 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
5c52323e
JK
1125 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1126 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
aa18294a 1127 auth_mode = NDIS_80211_AUTH_WPA;
5c52323e 1128 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
aa18294a 1129 auth_mode = NDIS_80211_AUTH_WPA_PSK;
bf164cc0 1130 else
aa18294a 1131 auth_mode = NDIS_80211_AUTH_WPA_NONE;
5c52323e
JK
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)
aa18294a 1135 auth_mode = NDIS_80211_AUTH_OPEN;
634a555c
JK
1136 else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1137 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
5c52323e
JK
1138 else
1139 return -ENOTSUPP;
bf164cc0
JK
1140
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) {
60b86755
JP
1145 netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1146 ret);
bf164cc0
JK
1147 return ret;
1148 }
1149
1150 priv->wpa_version = wpa_version;
5c52323e 1151
bf164cc0
JK
1152 return 0;
1153}
1154
bf164cc0
JK
1155static int set_priv_filter(struct usbnet *usbdev)
1156{
582241a0 1157 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
1158 __le32 tmp;
1159
60b86755
JP
1160 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1161 __func__, priv->wpa_version);
bf164cc0 1162
5c52323e
JK
1163 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1164 priv->wpa_version & NL80211_WPA_VERSION_1)
aa18294a 1165 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
bf164cc0 1166 else
aa18294a 1167 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
bf164cc0
JK
1168
1169 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1170 sizeof(tmp));
1171}
1172
bf164cc0
JK
1173static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1174{
bf164cc0
JK
1175 __le32 tmp;
1176 int encr_mode, ret;
1177
60b86755
JP
1178 netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1179 __func__, pairwise, groupwise);
bf164cc0 1180
5c52323e 1181 if (pairwise & RNDIS_WLAN_ALG_CCMP)
aa18294a 1182 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
5c52323e 1183 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
aa18294a 1184 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
5c52323e 1185 else if (pairwise & RNDIS_WLAN_ALG_WEP)
aa18294a 1186 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
5c52323e 1187 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
aa18294a 1188 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
5c52323e 1189 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
aa18294a 1190 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
bf164cc0 1191 else
aa18294a 1192 encr_mode = NDIS_80211_ENCR_DISABLED;
bf164cc0
JK
1193
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) {
60b86755
JP
1198 netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1199 ret);
bf164cc0
JK
1200 return ret;
1201 }
1202
bf164cc0
JK
1203 return 0;
1204}
1205
bf164cc0
JK
1206static int set_infra_mode(struct usbnet *usbdev, int mode)
1207{
582241a0 1208 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0 1209 __le32 tmp;
b7cfc5b3 1210 int ret;
bf164cc0 1211
60b86755
JP
1212 netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1213 __func__, priv->infra_mode);
bf164cc0
JK
1214
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) {
60b86755
JP
1219 netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1220 ret);
bf164cc0
JK
1221 return ret;
1222 }
1223
1224 /* NDIS drivers clear keys when infrastructure mode is
1225 * changed. But Linux tools assume otherwise. So set the
1226 * keys */
b7cfc5b3 1227 restore_keys(usbdev);
bf164cc0
JK
1228
1229 priv->infra_mode = mode;
1230 return 0;
1231}
1232
d75ec2b7
JK
1233static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1234{
1235 __le32 tmp;
1236
60b86755 1237 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
d75ec2b7
JK
1238
1239 if (rts_threshold < 0 || rts_threshold > 2347)
1240 rts_threshold = 2347;
1241
1242 tmp = cpu_to_le32(rts_threshold);
1243 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1244 sizeof(tmp));
1245}
1246
d75ec2b7
JK
1247static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1248{
1249 __le32 tmp;
1250
60b86755 1251 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
d75ec2b7
JK
1252
1253 if (frag_threshold < 256 || frag_threshold > 2346)
1254 frag_threshold = 2346;
1255
1256 tmp = cpu_to_le32(frag_threshold);
1257 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1258 sizeof(tmp));
1259}
1260
bf164cc0
JK
1261static void set_default_iw_params(struct usbnet *usbdev)
1262{
aa18294a 1263 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
5c52323e
JK
1264 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1265 RNDIS_WLAN_KEY_MGMT_NONE);
bf164cc0 1266 set_priv_filter(usbdev);
5c52323e 1267 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
bf164cc0
JK
1268}
1269
bf164cc0
JK
1270static int deauthenticate(struct usbnet *usbdev)
1271{
1272 int ret;
1273
051ae0bf 1274 ret = disassociate(usbdev, true);
bf164cc0
JK
1275 set_default_iw_params(usbdev);
1276 return ret;
1277}
1278
5c52323e
JK
1279static int set_channel(struct usbnet *usbdev, int channel)
1280{
1281 struct ndis_80211_conf config;
1282 unsigned int dsconfig;
1283 int len, ret;
1284
60b86755 1285 netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
5c52323e
JK
1286
1287 /* this OID is valid only when not associated */
1288 if (is_associated(usbdev))
1289 return 0;
1290
1291 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1292
1293 len = sizeof(config);
1294 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1295 if (ret < 0) {
60b86755
JP
1296 netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1297 __func__);
5c52323e
JK
1298 return ret;
1299 }
1300
1301 config.ds_config = cpu_to_le32(dsconfig);
1302 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1303 sizeof(config));
1304
60b86755 1305 netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
5c52323e
JK
1306
1307 return ret;
1308}
1309
bf164cc0 1310/* index must be 0 - N, as per NDIS */
5c52323e
JK
1311static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1312 int index)
bf164cc0 1313{
582241a0 1314 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1e41e1d2 1315 struct ndis_80211_wep_key ndis_key;
84bf8400
JK
1316 u32 cipher;
1317 int ret;
1318
60b86755
JP
1319 netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1320 __func__, index, key_len);
bf164cc0 1321
40ba60dd 1322 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
bf164cc0
JK
1323 return -EINVAL;
1324
b7cfc5b3
JK
1325 if (key_len == 5)
1326 cipher = WLAN_CIPHER_SUITE_WEP40;
1327 else
1328 cipher = WLAN_CIPHER_SUITE_WEP104;
1329
bf164cc0
JK
1330 memset(&ndis_key, 0, sizeof(ndis_key));
1331
461b3f2a
JK
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);
bf164cc0
JK
1336
1337 if (index == priv->encr_tx_key_index) {
aa18294a 1338 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
5c52323e
JK
1339 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1340 RNDIS_WLAN_ALG_NONE);
bf164cc0 1341 if (ret)
60b86755
JP
1342 netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1343 ret);
bf164cc0
JK
1344 }
1345
1346 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1347 sizeof(ndis_key));
1348 if (ret != 0) {
60b86755
JP
1349 netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1350 index + 1, ret);
bf164cc0
JK
1351 return ret;
1352 }
1353
b7cfc5b3
JK
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);
bf164cc0
JK
1358
1359 return 0;
1360}
1361
b145ee0c 1362static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
84bf8400 1363 int index, const u8 *addr, const u8 *rx_seq,
53d27eaf 1364 int seq_len, u32 cipher, __le32 flags)
b145ee0c 1365{
582241a0 1366 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
b145ee0c 1367 struct ndis_80211_key ndis_key;
b7cfc5b3 1368 bool is_addr_ok;
b145ee0c
JK
1369 int ret;
1370
b7cfc5b3 1371 if (index < 0 || index >= 4) {
60b86755
JP
1372 netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1373 __func__, index);
b145ee0c 1374 return -EINVAL;
b7cfc5b3
JK
1375 }
1376 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
60b86755
JP
1377 netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1378 __func__, key_len);
b145ee0c 1379 return -EINVAL;
b7cfc5b3 1380 }
84bf8400
JK
1381 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1382 if (!rx_seq || seq_len <= 0) {
60b86755
JP
1383 netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1384 __func__);
84bf8400
JK
1385 return -EINVAL;
1386 }
1387 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
60b86755 1388 netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
84bf8400
JK
1389 return -EINVAL;
1390 }
b7cfc5b3 1391 }
84bf8400 1392
7b1fff99
JK
1393 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1394 !is_broadcast_ether_addr(addr);
b7cfc5b3 1395 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
60b86755
JP
1396 netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1397 __func__, addr);
b145ee0c 1398 return -EINVAL;
b7cfc5b3 1399 }
b145ee0c 1400
60b86755
JP
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));
b145ee0c
JK
1406
1407 memset(&ndis_key, 0, sizeof(ndis_key));
1408
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;
1413
b7cfc5b3 1414 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
b145ee0c
JK
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);
1422
aa18294a 1423 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
84bf8400 1424 memcpy(ndis_key.rsc, rx_seq, seq_len);
b145ee0c 1425
aa18294a 1426 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
b145ee0c 1427 /* pairwise key */
b7cfc5b3 1428 memcpy(ndis_key.bssid, addr, ETH_ALEN);
b145ee0c
JK
1429 } else {
1430 /* group key */
aa18294a 1431 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
b145ee0c
JK
1432 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1433 else
1434 get_bssid(usbdev, ndis_key.bssid);
1435 }
1436
1437 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1438 le32_to_cpu(ndis_key.size));
60b86755
JP
1439 netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1440 __func__, ret);
b145ee0c
JK
1441 if (ret != 0)
1442 return ret;
1443
b7cfc5b3
JK
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);
9839178e 1452
aa18294a 1453 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
b145ee0c
JK
1454 priv->encr_tx_key_index = index;
1455
1456 return 0;
1457}
1458
b7cfc5b3
JK
1459static int restore_key(struct usbnet *usbdev, int key_idx)
1460{
1461 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1462 struct rndis_wlan_encr_key key;
84bf8400
JK
1463
1464 if (is_wpa_key(priv, key_idx))
1465 return 0;
b7cfc5b3
JK
1466
1467 key = priv->encr_keys[key_idx];
1468
60b86755 1469 netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
b7cfc5b3
JK
1470
1471 if (key.len == 0)
1472 return 0;
1473
b7cfc5b3
JK
1474 return add_wep_key(usbdev, key.material, key.len, key_idx);
1475}
1476
b7cfc5b3
JK
1477static void restore_keys(struct usbnet *usbdev)
1478{
1479 int i;
1480
1481 for (i = 0; i < 4; i++)
1482 restore_key(usbdev, i);
1483}
1484
b7cfc5b3
JK
1485static void clear_key(struct rndis_wlan_private *priv, int idx)
1486{
1487 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1488}
1489
bf164cc0 1490/* remove_key is for both wep and wpa */
84bf8400 1491static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
bf164cc0 1492{
582241a0 1493 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1e41e1d2 1494 struct ndis_80211_remove_key remove_key;
bf164cc0 1495 __le32 keyindex;
b7cfc5b3 1496 bool is_wpa;
bf164cc0
JK
1497 int ret;
1498
b7cfc5b3 1499 if (priv->encr_keys[index].len == 0)
bf164cc0
JK
1500 return 0;
1501
b7cfc5b3 1502 is_wpa = is_wpa_key(priv, index);
bf164cc0 1503
60b86755
JP
1504 netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1505 __func__, index, is_wpa ? "wpa" : "wep",
1506 priv->encr_keys[index].len);
b7cfc5b3
JK
1507
1508 clear_key(priv, index);
1509
1510 if (is_wpa) {
461b3f2a
JK
1511 remove_key.size = cpu_to_le32(sizeof(remove_key));
1512 remove_key.index = cpu_to_le32(index);
bf164cc0
JK
1513 if (bssid) {
1514 /* pairwise key */
7b1fff99 1515 if (!is_broadcast_ether_addr(bssid))
b4703a2e 1516 remove_key.index |=
aa18294a 1517 NDIS_80211_ADDKEY_PAIRWISE_KEY;
461b3f2a
JK
1518 memcpy(remove_key.bssid, bssid,
1519 sizeof(remove_key.bssid));
bf164cc0 1520 } else
461b3f2a
JK
1521 memset(remove_key.bssid, 0xff,
1522 sizeof(remove_key.bssid));
bf164cc0
JK
1523
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) {
60b86755
JP
1533 netdev_warn(usbdev->net,
1534 "removing encryption key %d failed (%08X)\n",
1535 index, ret);
bf164cc0
JK
1536 return ret;
1537 }
1538 }
1539
1540 /* if it is transmit key, disable encryption */
1541 if (index == priv->encr_tx_key_index)
5c52323e 1542 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
bf164cc0
JK
1543
1544 return 0;
1545}
1546
bf164cc0
JK
1547static void set_multicast_list(struct usbnet *usbdev)
1548{
582241a0 1549 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
22bedad3 1550 struct netdev_hw_addr *ha;
12c3400a
JP
1551 __le32 filter, basefilter;
1552 int ret;
1553 char *mc_addrs = NULL;
1554 int mc_count;
bf164cc0 1555
12c3400a
JP
1556 basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1557 RNDIS_PACKET_TYPE_BROADCAST;
bf164cc0
JK
1558
1559 if (usbdev->net->flags & IFF_PROMISC) {
1560 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1561 RNDIS_PACKET_TYPE_ALL_LOCAL;
12c3400a
JP
1562 } else if (usbdev->net->flags & IFF_ALLMULTI) {
1563 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1564 }
1565
1566 if (filter != basefilter)
1567 goto set_filter;
1568
1569 /*
1570 * mc_list should be accessed holding the lock, so copy addresses to
1571 * local buffer first.
1572 */
1573 netif_addr_lock_bh(usbdev->net);
1574 mc_count = netdev_mc_count(usbdev->net);
1575 if (mc_count > priv->multicast_size) {
bf164cc0 1576 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
12c3400a
JP
1577 } else if (mc_count) {
1578 int i = 0;
1579
1580 mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1581 if (!mc_addrs) {
60b86755
JP
1582 netdev_warn(usbdev->net,
1583 "couldn't alloc %d bytes of memory\n",
12c3400a 1584 mc_count * ETH_ALEN);
655ffee2 1585 netif_addr_unlock_bh(usbdev->net);
bf164cc0
JK
1586 return;
1587 }
1588
22bedad3 1589 netdev_for_each_mc_addr(ha, usbdev->net)
12c3400a 1590 memcpy(mc_addrs + i++ * ETH_ALEN,
22bedad3 1591 ha->addr, ETH_ALEN);
12c3400a
JP
1592 }
1593 netif_addr_unlock_bh(usbdev->net);
bf164cc0 1594
12c3400a
JP
1595 if (filter != basefilter)
1596 goto set_filter;
1597
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)
bf164cc0
JK
1603 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1604 else
1605 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1606
60b86755 1607 netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
12c3400a 1608 mc_count, priv->multicast_size, ret);
bf164cc0
JK
1609 }
1610
12c3400a 1611set_filter:
bf164cc0
JK
1612 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1613 sizeof(filter));
1614 if (ret < 0) {
60b86755
JP
1615 netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1616 le32_to_cpu(filter));
bf164cc0
JK
1617 }
1618
60b86755
JP
1619 netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1620 le32_to_cpu(filter), ret);
bf164cc0
JK
1621}
1622
5a7d0583
JK
1623#ifdef DEBUG
1624static void debug_print_pmkids(struct usbnet *usbdev,
1625 struct ndis_80211_pmkid *pmkids,
1626 const char *func_str)
1627{
1628 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1629 int i, len, count, max_pmkids, entry_len;
1630
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);
1634
1635 entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1636
1637 netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1638 "%d)\n", func_str, count, len, entry_len);
1639
1640 if (count > max_pmkids)
1641 count = max_pmkids;
1642
1643 for (i = 0; i < count; i++) {
1644 u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1645
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]));
1651 }
1652}
1653#else
1654static void debug_print_pmkids(struct usbnet *usbdev,
1655 struct ndis_80211_pmkid *pmkids,
1656 const char *func_str)
1657{
1658 return;
1659}
1660#endif
1661
1662static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1663{
1664 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1665 struct ndis_80211_pmkid *pmkids;
1666 int len, ret, max_pmkids;
1667
1668 max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1669 len = sizeof(*pmkids) + max_pmkids * sizeof(pmkids->bssid_info[0]);
1670
1671 pmkids = kzalloc(len, GFP_KERNEL);
1672 if (!pmkids)
1673 return ERR_PTR(-ENOMEM);
1674
1675 pmkids->length = cpu_to_le32(len);
1676 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1677
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);
1682
1683 kfree(pmkids);
1684 return ERR_PTR(ret);
1685 }
1686
1687 if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1688 pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1689
1690 debug_print_pmkids(usbdev, pmkids, __func__);
1691
1692 return pmkids;
1693}
1694
1695static int set_device_pmkids(struct usbnet *usbdev,
1696 struct ndis_80211_pmkid *pmkids)
1697{
1698 int ret, len, num_pmkids;
1699
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);
1703
1704 debug_print_pmkids(usbdev, pmkids, __func__);
1705
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);
1711 }
1712
1713 kfree(pmkids);
1714 return ret;
1715}
1716
1717static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1718 struct ndis_80211_pmkid *pmkids,
1719 struct cfg80211_pmksa *pmksa,
1720 int max_pmkids)
1721{
1722 int i, len, count, newlen, err;
1723
1724 len = le32_to_cpu(pmkids->length);
1725 count = le32_to_cpu(pmkids->bssid_info_count);
1726
1727 if (count > max_pmkids)
1728 count = max_pmkids;
1729
1730 for (i = 0; i < count; i++)
1731 if (!compare_ether_addr(pmkids->bssid_info[i].bssid,
1732 pmksa->bssid))
1733 break;
1734
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;
1741 }
1742
1743 for (; i + 1 < count; i++)
1744 pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1745
1746 count--;
1747 newlen = sizeof(*pmkids) + count * sizeof(pmkids->bssid_info[0]);
1748
1749 pmkids->length = cpu_to_le32(newlen);
1750 pmkids->bssid_info_count = cpu_to_le32(count);
1751
1752 return pmkids;
1753error:
1754 kfree(pmkids);
1755 return ERR_PTR(err);
1756}
1757
1758static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1759 struct ndis_80211_pmkid *pmkids,
1760 struct cfg80211_pmksa *pmksa,
1761 int max_pmkids)
1762{
1763 int i, err, len, count, newlen;
1764
1765 len = le32_to_cpu(pmkids->length);
1766 count = le32_to_cpu(pmkids->bssid_info_count);
1767
1768 if (count > max_pmkids)
1769 count = max_pmkids;
1770
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;
1776
1777 memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1778 WLAN_PMKID_LEN);
1779
1780 return pmkids;
1781 }
1782
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;
1788 }
1789
1790 /* add new pmkid */
1791 newlen = sizeof(*pmkids) + (count + 1) * sizeof(pmkids->bssid_info[0]);
1792
1793 pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1794 if (!pmkids) {
1795 err = -ENOMEM;
1796 goto error;
1797 }
1798
1799 pmkids->length = cpu_to_le32(newlen);
1800 pmkids->bssid_info_count = cpu_to_le32(count + 1);
1801
1802 memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1803 memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1804
1805 return pmkids;
1806error:
1807 kfree(pmkids);
1808 return ERR_PTR(err);
1809}
1810
964c1d41
JL
1811/*
1812 * cfg80211 ops
1813 */
e36d56b6
JB
1814static int rndis_change_virtual_intf(struct wiphy *wiphy,
1815 struct net_device *dev,
964c1d41
JL
1816 enum nl80211_iftype type, u32 *flags,
1817 struct vif_params *params)
1818{
16139172
JK
1819 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1820 struct usbnet *usbdev = priv->usbdev;
964c1d41
JL
1821 int mode;
1822
964c1d41
JL
1823 switch (type) {
1824 case NL80211_IFTYPE_ADHOC:
aa18294a 1825 mode = NDIS_80211_INFRA_ADHOC;
964c1d41
JL
1826 break;
1827 case NL80211_IFTYPE_STATION:
aa18294a 1828 mode = NDIS_80211_INFRA_INFRA;
964c1d41
JL
1829 break;
1830 default:
1831 return -EINVAL;
1832 }
1833
16139172
JK
1834 priv->wdev.iftype = type;
1835
964c1d41
JL
1836 return set_infra_mode(usbdev, mode);
1837}
1838
d75ec2b7
JK
1839static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1840{
1841 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1842 struct usbnet *usbdev = priv->usbdev;
1843 int err;
1844
1845 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1846 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1847 if (err < 0)
1848 return err;
1849 }
1850
1851 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1852 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1853 if (err < 0)
1854 return err;
1855 }
1856
1857 return 0;
1858}
1859
fa61cf70
JO
1860static int rndis_set_tx_power(struct wiphy *wiphy,
1861 enum nl80211_tx_power_setting type,
1862 int mbm)
222ec50a
JK
1863{
1864 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1865 struct usbnet *usbdev = priv->usbdev;
1866
fa61cf70
JO
1867 netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1868 __func__, type, mbm);
1869
1870 if (mbm < 0 || (mbm % 100))
1871 return -ENOTSUPP;
222ec50a
JK
1872
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.
1876 */
fa61cf70
JO
1877 if (type == NL80211_TX_POWER_AUTOMATIC ||
1878 MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
222ec50a 1879 if (!priv->radio_on)
051ae0bf 1880 disassociate(usbdev, true); /* turn on radio */
222ec50a
JK
1881
1882 return 0;
1883 }
1884
1885 return -ENOTSUPP;
1886}
1887
222ec50a
JK
1888static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1889{
1890 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1891 struct usbnet *usbdev = priv->usbdev;
1892
1893 *dbm = get_bcm4320_power_dbm(priv);
1894
60b86755 1895 netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
222ec50a
JK
1896
1897 return 0;
1898}
1899
b1d25a67 1900#define SCAN_DELAY_JIFFIES (6 * HZ)
71a7b26d
JK
1901static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1902 struct cfg80211_scan_request *request)
1903{
1904 struct usbnet *usbdev = netdev_priv(dev);
582241a0 1905 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
71a7b26d
JK
1906 int ret;
1907 __le32 tmp;
1908
60b86755 1909 netdev_dbg(usbdev->net, "cfg80211.scan\n");
71a7b26d 1910
db0dd396
JK
1911 /* Get current bssid list from device before new scan, as new scan
1912 * clears internal bssid list.
1913 */
1914 rndis_check_bssid_list(usbdev);
1915
71a7b26d
JK
1916 if (!request)
1917 return -EINVAL;
1918
1919 if (priv->scan_request && priv->scan_request != request)
1920 return -EBUSY;
1921
1922 priv->scan_request = request;
1923
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);
1931 }
1932
1933 return ret;
1934}
1935
71a7b26d
JK
1936static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1937 struct ndis_80211_bssid_ex *bssid)
1938{
582241a0 1939 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
71a7b26d
JK
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;
1948
60b86755
JP
1949 netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
1950 bssid->ssid.essid, bssid->mac);
5fd8f250 1951
71a7b26d
JK
1952 /* parse bssid structure */
1953 bssid_len = le32_to_cpu(bssid->length);
1954
1955 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1956 sizeof(struct ndis_80211_fixed_ies))
1957 return NULL;
1958
1959 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1960
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;
1967
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;
1973
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);
1978
1979 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1980 timestamp, capability, beacon_interval, ie, ie_len, signal,
1981 GFP_KERNEL);
1982}
1983
71a7b26d
JK
1984static int rndis_check_bssid_list(struct usbnet *usbdev)
1985{
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;
5fd8f250 1990 bool resized = false;
71a7b26d 1991
60b86755 1992 netdev_dbg(usbdev->net, "check_bssid_list\n");
71a7b26d
JK
1993
1994 len = CONTROL_BUFFER_SIZE;
5fd8f250 1995resize_buf:
71a7b26d
JK
1996 buf = kmalloc(len, GFP_KERNEL);
1997 if (!buf) {
1998 ret = -ENOMEM;
1999 goto out;
2000 }
2001
2002 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2003 if (ret != 0)
2004 goto out;
2005
5fd8f250
JK
2006 if (!resized && len > CONTROL_BUFFER_SIZE) {
2007 resized = true;
2008 kfree(buf);
2009 goto resize_buf;
2010 }
2011
71a7b26d
JK
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);
60b86755
JP
2016 netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
2017 count, len);
71a7b26d
JK
2018
2019 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
2020 rndis_bss_info_update(usbdev, bssid);
2021
2022 bssid = (void *)bssid + bssid_len;
2023 bssid_len = le32_to_cpu(bssid->length);
2024 count--;
2025 }
2026
2027out:
2028 kfree(buf);
2029 return ret;
2030}
2031
71a7b26d
JK
2032static void rndis_get_scan_results(struct work_struct *work)
2033{
582241a0
JK
2034 struct rndis_wlan_private *priv =
2035 container_of(work, struct rndis_wlan_private, scan_work.work);
71a7b26d
JK
2036 struct usbnet *usbdev = priv->usbdev;
2037 int ret;
2038
60b86755 2039 netdev_dbg(usbdev->net, "get_scan_results\n");
71a7b26d 2040
005ba2f1
JK
2041 if (!priv->scan_request)
2042 return;
2043
71a7b26d
JK
2044 ret = rndis_check_bssid_list(usbdev);
2045
2046 cfg80211_scan_done(priv->scan_request, ret < 0);
2047
2048 priv->scan_request = NULL;
2049}
2050
5c52323e
JK
2051static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2052 struct cfg80211_connect_params *sme)
2053{
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;
2062
2063 if (channel)
2064 chan = ieee80211_frequency_to_channel(channel->center_freq);
2065
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]);
2070
2071 if (sme->crypto.n_ciphers_pairwise > 0 &&
2072 pairwise == RNDIS_WLAN_ALG_NONE) {
60b86755 2073 netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
5c52323e
JK
2074 return -ENOTSUPP;
2075 }
2076
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]);
2080
2081 if (sme->crypto.n_akm_suites > 0 &&
2082 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
60b86755 2083 netdev_err(usbdev->net, "Invalid keymgmt\n");
5c52323e
JK
2084 return -ENOTSUPP;
2085 }
2086
60b86755
JP
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);
5c52323e
JK
2091
2092 if (is_associated(usbdev))
2093 disassociate(usbdev, false);
2094
2095 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2096 if (ret < 0) {
60b86755
JP
2097 netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2098 ret);
5c52323e
JK
2099 goto err_turn_radio_on;
2100 }
2101
2102 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2103 keymgmt);
2104 if (ret < 0) {
60b86755
JP
2105 netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2106 ret);
5c52323e
JK
2107 goto err_turn_radio_on;
2108 }
2109
2110 set_priv_filter(usbdev);
2111
2112 ret = set_encr_mode(usbdev, pairwise, groupwise);
2113 if (ret < 0) {
60b86755
JP
2114 netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2115 ret);
5c52323e
JK
2116 goto err_turn_radio_on;
2117 }
2118
2119 if (channel) {
2120 ret = set_channel(usbdev, chan);
2121 if (ret < 0) {
60b86755
JP
2122 netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2123 ret);
5c52323e
JK
2124 goto err_turn_radio_on;
2125 }
2126 }
2127
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) {
60b86755
JP
2132 netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2133 ret, sme->key_len, sme->key_idx);
5c52323e
JK
2134 goto err_turn_radio_on;
2135 }
2136 }
2137
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) {
60b86755
JP
2142 netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2143 ret);
5c52323e
JK
2144 goto err_turn_radio_on;
2145 }
2146 } else
2147 clear_bssid(usbdev);
2148
2149 length = sme->ssid_len;
2150 if (length > NDIS_802_11_LENGTH_SSID)
2151 length = NDIS_802_11_LENGTH_SSID;
2152
2153 memset(&ssid, 0, sizeof(ssid));
2154 ssid.length = cpu_to_le32(length);
2155 memcpy(ssid.essid, sme->ssid, length);
2156
2157 /* Pause and purge rx queue, so we don't pass packets before
2158 * 'media connect'-indication.
2159 */
2160 usbnet_pause_rx(usbdev);
2161 usbnet_purge_paused_rxq(usbdev);
2162
2163 ret = set_essid(usbdev, &ssid);
2164 if (ret < 0)
60b86755 2165 netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
5c52323e
JK
2166 return ret;
2167
2168err_turn_radio_on:
051ae0bf 2169 disassociate(usbdev, true);
5c52323e
JK
2170
2171 return ret;
2172}
2173
2174static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2175 u16 reason_code)
2176{
2177 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2178 struct usbnet *usbdev = priv->usbdev;
2179
60b86755 2180 netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
5c52323e
JK
2181
2182 priv->connected = false;
8b89a288 2183 memset(priv->bssid, 0, ETH_ALEN);
5c52323e
JK
2184
2185 return deauthenticate(usbdev);
2186}
2187
2188static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2189 struct cfg80211_ibss_params *params)
2190{
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;
2197
2198 if (channel)
2199 chan = ieee80211_frequency_to_channel(channel->center_freq);
2200
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?
2205 */
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;
2212 }
2213
60b86755
JP
2214 netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2215 params->ssid, params->bssid, chan, params->privacy);
5c52323e
JK
2216
2217 if (is_associated(usbdev))
2218 disassociate(usbdev, false);
2219
2220 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2221 if (ret < 0) {
60b86755
JP
2222 netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2223 ret);
5c52323e
JK
2224 goto err_turn_radio_on;
2225 }
2226
2227 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2228 if (ret < 0) {
60b86755
JP
2229 netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2230 ret);
5c52323e
JK
2231 goto err_turn_radio_on;
2232 }
2233
2234 set_priv_filter(usbdev);
2235
2236 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2237 if (ret < 0) {
60b86755
JP
2238 netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2239 ret);
5c52323e
JK
2240 goto err_turn_radio_on;
2241 }
2242
2243 if (channel) {
2244 ret = set_channel(usbdev, chan);
2245 if (ret < 0) {
60b86755
JP
2246 netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2247 ret);
5c52323e
JK
2248 goto err_turn_radio_on;
2249 }
2250 }
2251
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) {
60b86755
JP
2256 netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2257 ret);
5c52323e
JK
2258 goto err_turn_radio_on;
2259 }
2260 } else
2261 clear_bssid(usbdev);
2262
2263 length = params->ssid_len;
2264 if (length > NDIS_802_11_LENGTH_SSID)
2265 length = NDIS_802_11_LENGTH_SSID;
2266
2267 memset(&ssid, 0, sizeof(ssid));
2268 ssid.length = cpu_to_le32(length);
2269 memcpy(ssid.essid, params->ssid, length);
2270
2271 /* Don't need to pause rx queue for ad-hoc. */
2272 usbnet_purge_paused_rxq(usbdev);
2273 usbnet_resume_rx(usbdev);
2274
2275 ret = set_essid(usbdev, &ssid);
2276 if (ret < 0)
60b86755
JP
2277 netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2278 ret);
5c52323e
JK
2279 return ret;
2280
2281err_turn_radio_on:
051ae0bf 2282 disassociate(usbdev, true);
5c52323e
JK
2283
2284 return ret;
2285}
2286
2287static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2288{
2289 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2290 struct usbnet *usbdev = priv->usbdev;
2291
60b86755 2292 netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
5c52323e
JK
2293
2294 priv->connected = false;
8b89a288 2295 memset(priv->bssid, 0, ETH_ALEN);
5c52323e
JK
2296
2297 return deauthenticate(usbdev);
2298}
2299
f444de05 2300static int rndis_set_channel(struct wiphy *wiphy, struct net_device *netdev,
5554adbe
JK
2301 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2302{
2303 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2304 struct usbnet *usbdev = priv->usbdev;
2305
2306 return set_channel(usbdev,
2307 ieee80211_frequency_to_channel(chan->center_freq));
2308}
71a7b26d 2309
84bf8400 2310static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
e31b8213
JB
2311 u8 key_index, bool pairwise, const u8 *mac_addr,
2312 struct key_params *params)
84bf8400
JK
2313{
2314 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2315 struct usbnet *usbdev = priv->usbdev;
53d27eaf 2316 __le32 flags;
84bf8400 2317
60b86755
JP
2318 netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2319 __func__, key_index, mac_addr, params->cipher);
84bf8400
JK
2320
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;
2329
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;
2335
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:
60b86755
JP
2340 netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2341 __func__, params->cipher);
84bf8400
JK
2342 return -ENOTSUPP;
2343 }
2344}
2345
2346static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
e31b8213 2347 u8 key_index, bool pairwise, const u8 *mac_addr)
84bf8400
JK
2348{
2349 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2350 struct usbnet *usbdev = priv->usbdev;
2351
60b86755 2352 netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
84bf8400
JK
2353
2354 return remove_key(usbdev, key_index, mac_addr);
2355}
2356
2357static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2358 u8 key_index)
2359{
2360 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2361 struct usbnet *usbdev = priv->usbdev;
2362 struct rndis_wlan_encr_key key;
2363
60b86755 2364 netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
84bf8400
JK
2365
2366 priv->encr_tx_key_index = key_index;
2367
2368 key = priv->encr_keys[key_index];
2369
2370 return add_wep_key(usbdev, key.material, key.len, key_index);
2371}
2372
8b89a288
JK
2373static void rndis_fill_station_info(struct usbnet *usbdev,
2374 struct station_info *sinfo)
2375{
2376 __le32 linkspeed, rssi;
2377 int ret, len;
2378
2379 memset(sinfo, 0, sizeof(*sinfo));
2380
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;
2386 }
2387
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;
2393 }
2394}
2395
2396static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2397 u8 *mac, struct station_info *sinfo)
2398{
2399 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2400 struct usbnet *usbdev = priv->usbdev;
2401
2402 if (compare_ether_addr(priv->bssid, mac))
2403 return -ENOENT;
2404
2405 rndis_fill_station_info(usbdev, sinfo);
2406
2407 return 0;
2408}
2409
d695df90
JK
2410static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2411 int idx, u8 *mac, struct station_info *sinfo)
2412{
2413 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2414 struct usbnet *usbdev = priv->usbdev;
2415
2416 if (idx != 0)
2417 return -ENOENT;
2418
2419 memcpy(mac, priv->bssid, ETH_ALEN);
2420
2421 rndis_fill_station_info(usbdev, sinfo);
2422
2423 return 0;
2424}
2425
5a7d0583
JK
2426static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2427 struct cfg80211_pmksa *pmksa)
2428{
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;
2433
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]));
2438
2439 pmkids = get_device_pmkids(usbdev);
2440 if (IS_ERR(pmkids)) {
2441 /* couldn't read PMKID cache from device */
2442 return PTR_ERR(pmkids);
2443 }
2444
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);
2449 }
2450
2451 return set_device_pmkids(usbdev, pmkids);
2452}
2453
2454static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2455 struct cfg80211_pmksa *pmksa)
2456{
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;
2461
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]));
2466
2467 pmkids = get_device_pmkids(usbdev);
2468 if (IS_ERR(pmkids)) {
2469 /* Couldn't read PMKID cache from device */
2470 return PTR_ERR(pmkids);
2471 }
2472
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);
2477 }
2478
2479 return set_device_pmkids(usbdev, pmkids);
2480}
2481
2482static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2483{
2484 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2485 struct usbnet *usbdev = priv->usbdev;
2486 struct ndis_80211_pmkid pmkid;
2487
2488 netdev_dbg(usbdev->net, "%s()\n", __func__);
2489
2490 memset(&pmkid, 0, sizeof(pmkid));
2491
2492 pmkid.length = cpu_to_le32(sizeof(pmkid));
2493 pmkid.bssid_info_count = cpu_to_le32(0);
2494
2495 return rndis_set_oid(usbdev, OID_802_11_PMKID, &pmkid, sizeof(pmkid));
2496}
2497
c5c4fe90
JK
2498/*
2499 * workers, indication handlers, device poller
2500 */
0848e6c6 2501static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
bf164cc0 2502{
5c52323e 2503 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3d1ca47e 2504 struct ndis_80211_assoc_info *info = NULL;
0848e6c6 2505 u8 bssid[ETH_ALEN];
5c52323e
JK
2506 int resp_ie_len, req_ie_len;
2507 u8 *req_ie, *resp_ie;
4364623c 2508 int ret, offset;
5c52323e 2509 bool roamed = false;
bf164cc0 2510
5c52323e
JK
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;
2515 }
0848e6c6 2516
5c52323e
JK
2517 req_ie_len = 0;
2518 resp_ie_len = 0;
2519 req_ie = NULL;
2520 resp_ie = NULL;
2521
2522 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
3d1ca47e
JK
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;
2529 }
5c52323e 2530
3d1ca47e
JK
2531 /* Get association info IEs from device. */
2532 ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
5c52323e
JK
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);
3d1ca47e
JK
2537
2538 if (offset > CONTROL_BUFFER_SIZE)
2539 offset = CONTROL_BUFFER_SIZE;
2540
5c52323e 2541 req_ie = (u8 *)info + offset;
3d1ca47e
JK
2542
2543 if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2544 req_ie_len =
2545 CONTROL_BUFFER_SIZE - offset;
5c52323e
JK
2546 }
2547
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);
3d1ca47e
JK
2551
2552 if (offset > CONTROL_BUFFER_SIZE)
2553 offset = CONTROL_BUFFER_SIZE;
2554
5c52323e 2555 resp_ie = (u8 *)info + offset;
3d1ca47e
JK
2556
2557 if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2558 resp_ie_len =
2559 CONTROL_BUFFER_SIZE - offset;
5c52323e 2560 }
4364623c 2561 }
5c52323e
JK
2562 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2563 return;
4364623c 2564
5c52323e
JK
2565 ret = get_bssid(usbdev, bssid);
2566 if (ret < 0)
2567 memset(bssid, 0, sizeof(bssid));
7834ddbc 2568
60b86755
JP
2569 netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2570 bssid, roamed ? " roamed" : "");
bf164cc0 2571
5c52323e
JK
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.
2577 */
2578 rndis_check_bssid_list(usbdev);
2579
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);
2590
3d1ca47e
JK
2591 if (info != NULL)
2592 kfree(info);
2593
5c52323e 2594 priv->connected = true;
8b89a288 2595 memcpy(priv->bssid, bssid, ETH_ALEN);
6010ce07 2596
0848e6c6 2597 usbnet_resume_rx(usbdev);
5c52323e 2598 netif_carrier_on(usbdev->net);
0848e6c6
JK
2599}
2600
2601static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2602{
b6411fc2 2603 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
0848e6c6 2604
b6411fc2
JK
2605 if (priv->connected) {
2606 priv->connected = false;
2607 memset(priv->bssid, 0, ETH_ALEN);
2608
2609 deauthenticate(usbdev);
0848e6c6 2610
b6411fc2
JK
2611 cfg80211_disconnected(usbdev->net, 0, NULL, 0, GFP_KERNEL);
2612 }
2613
2614 netif_carrier_off(usbdev->net);
0848e6c6
JK
2615}
2616
2617static void rndis_wlan_worker(struct work_struct *work)
2618{
2619 struct rndis_wlan_private *priv =
2620 container_of(work, struct rndis_wlan_private, work);
2621 struct usbnet *usbdev = priv->usbdev;
2622
2623 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2624 rndis_wlan_do_link_up_work(usbdev);
2625
2626 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2627 rndis_wlan_do_link_down_work(usbdev);
2628
bf164cc0
JK
2629 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2630 set_multicast_list(usbdev);
2631}
2632
582241a0 2633static void rndis_wlan_set_multicast_list(struct net_device *dev)
bf164cc0 2634{
524ad0a7 2635 struct usbnet *usbdev = netdev_priv(dev);
582241a0 2636 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0 2637
a67edb9e
JK
2638 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2639 return;
2640
bf164cc0
JK
2641 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2642 queue_work(priv->workqueue, &priv->work);
2643}
2644
030645ac
JK
2645static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2646 struct ndis_80211_status_indication *indication,
2647 int len)
2648{
2649 u8 *buf;
2650 const char *type;
a0f9ce2a 2651 int flags, buflen, key_id;
030645ac
JK
2652 bool pairwise_error, group_error;
2653 struct ndis_80211_auth_request *auth_req;
a0f9ce2a 2654 enum nl80211_key_type key_type;
030645ac
JK
2655
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)) {
60b86755
JP
2659 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2660 len);
030645ac
JK
2661 return;
2662 }
2663
2664 buf = (void *)&indication->u.auth_request[0];
2665 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2666
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;
2673
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";
2681 }
2682 if (flags & 0xe) {
2683 group_error = true;
2684 type = "group_error";
2685 }
2686
60b86755
JP
2687 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2688 type, le32_to_cpu(auth_req->flags));
030645ac 2689
a0f9ce2a
JK
2690 if (pairwise_error) {
2691 key_type = NL80211_KEYTYPE_PAIRWISE;
2692 key_id = -1;
030645ac 2693
a0f9ce2a
JK
2694 cfg80211_michael_mic_failure(usbdev->net,
2695 auth_req->bssid,
2696 key_type, key_id, NULL,
2697 GFP_KERNEL);
2698 }
030645ac 2699
a0f9ce2a
JK
2700 if (group_error) {
2701 key_type = NL80211_KEYTYPE_GROUP;
2702 key_id = -1;
030645ac 2703
a0f9ce2a
JK
2704 cfg80211_michael_mic_failure(usbdev->net,
2705 auth_req->bssid,
2706 key_type, key_id, NULL,
2707 GFP_KERNEL);
030645ac
JK
2708 }
2709
2710 buflen -= le32_to_cpu(auth_req->length);
2711 buf += le32_to_cpu(auth_req->length);
2712 }
2713}
2714
2715static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2716 struct ndis_80211_status_indication *indication,
2717 int len)
2718{
2719 struct ndis_80211_pmkid_cand_list *cand_list;
2720 int list_len, expected_len, i;
2721
2722 if (len < offsetof(struct ndis_80211_status_indication, u) +
2723 sizeof(struct ndis_80211_pmkid_cand_list)) {
60b86755
JP
2724 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2725 len);
030645ac
JK
2726 return;
2727 }
2728
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);
2733
2734 if (len < expected_len) {
60b86755
JP
2735 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2736 len, expected_len);
030645ac
JK
2737 return;
2738 }
2739
2740 cand_list = &indication->u.cand_list;
2741
60b86755
JP
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));
030645ac
JK
2745
2746 if (le32_to_cpu(cand_list->version) != 1)
2747 return;
2748
2749 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
030645ac
JK
2750 struct ndis_80211_pmkid_candidate *cand =
2751 &cand_list->candidate_list[i];
2752
60b86755
JP
2753 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2754 i, le32_to_cpu(cand->flags), cand->bssid);
030645ac 2755
21ec2d8d
JK
2756#if 0
2757 struct iw_pmkid_cand pcand;
2758 union iwreq_data wrqu;
2759
030645ac
JK
2760 memset(&pcand, 0, sizeof(pcand));
2761 if (le32_to_cpu(cand->flags) & 0x01)
2762 pcand.flags |= IW_PMKID_CAND_PREAUTH;
2763 pcand.index = i;
2764 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2765
2766 memset(&wrqu, 0, sizeof(wrqu));
2767 wrqu.data.length = sizeof(pcand);
2768 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2769 (u8 *)&pcand);
21ec2d8d 2770#endif
030645ac
JK
2771 }
2772}
2773
2774static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2775 struct rndis_indicate *msg, int buflen)
2776{
2777 struct ndis_80211_status_indication *indication;
2778 int len, offset;
2779
2780 offset = offsetof(struct rndis_indicate, status) +
2781 le32_to_cpu(msg->offset);
2782 len = le32_to_cpu(msg->length);
2783
2784 if (len < 8) {
60b86755
JP
2785 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2786 len);
030645ac
JK
2787 return;
2788 }
2789
2790 if (offset + len > buflen) {
60b86755
JP
2791 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2792 offset + len, buflen);
030645ac
JK
2793 return;
2794 }
2795
2796 indication = (void *)((u8 *)msg + offset);
2797
2798 switch (le32_to_cpu(indication->status_type)) {
2799 case NDIS_80211_STATUSTYPE_RADIOSTATE:
60b86755
JP
2800 netdev_info(usbdev->net, "radio state indication: %i\n",
2801 le32_to_cpu(indication->u.radio_status));
030645ac
JK
2802 return;
2803
2804 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
60b86755
JP
2805 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2806 le32_to_cpu(indication->u.media_stream_mode));
030645ac
JK
2807 return;
2808
2809 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2810 rndis_wlan_auth_indication(usbdev, indication, len);
2811 return;
2812
2813 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2814 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2815 return;
2816
2817 default:
60b86755
JP
2818 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2819 le32_to_cpu(indication->status_type));
030645ac
JK
2820 }
2821}
2822
2a4901bc 2823static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
bf164cc0 2824{
582241a0 2825 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2a4901bc 2826 struct rndis_indicate *msg = ind;
bf164cc0 2827
2a4901bc
JK
2828 switch (msg->status) {
2829 case RNDIS_STATUS_MEDIA_CONNECT:
5f81ff5a
JK
2830 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2831 /* OID_802_11_ADD_KEY causes sometimes extra
2832 * "media connect" indications which confuses driver
2833 * and userspace to think that device is
2834 * roaming/reassociating when it isn't.
2835 */
60b86755 2836 netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
5f81ff5a
JK
2837 return;
2838 }
2839
7834ddbc
JK
2840 usbnet_pause_rx(usbdev);
2841
60b86755 2842 netdev_info(usbdev->net, "media connect\n");
2a4901bc 2843
030645ac 2844 /* queue work to avoid recursive calls into rndis_command */
2a4901bc
JK
2845 set_bit(WORK_LINK_UP, &priv->work_pending);
2846 queue_work(priv->workqueue, &priv->work);
2847 break;
2848
2849 case RNDIS_STATUS_MEDIA_DISCONNECT:
60b86755 2850 netdev_info(usbdev->net, "media disconnect\n");
2a4901bc 2851
030645ac 2852 /* queue work to avoid recursive calls into rndis_command */
2a4901bc
JK
2853 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2854 queue_work(priv->workqueue, &priv->work);
2855 break;
2856
030645ac
JK
2857 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2858 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2859 break;
2860
2a4901bc 2861 default:
60b86755
JP
2862 netdev_info(usbdev->net, "indication: 0x%08x\n",
2863 le32_to_cpu(msg->status));
2a4901bc
JK
2864 break;
2865 }
bf164cc0
JK
2866}
2867
0308383f 2868static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
bf164cc0
JK
2869{
2870 struct {
2871 __le32 num_items;
2872 __le32 items[8];
2873 } networks_supported;
0308383f
JK
2874 struct ndis_80211_capability *caps;
2875 u8 caps_buf[sizeof(*caps) + sizeof(caps->auth_encr_pair) * 16];
bf164cc0 2876 int len, retval, i, n;
582241a0 2877 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0 2878
bf164cc0
JK
2879 /* determine supported modes */
2880 len = sizeof(networks_supported);
a19d7292 2881 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
bf164cc0
JK
2882 &networks_supported, &len);
2883 if (retval >= 0) {
2884 n = le32_to_cpu(networks_supported.num_items);
2885 if (n > 8)
2886 n = 8;
2887 for (i = 0; i < n; i++) {
2888 switch (le32_to_cpu(networks_supported.items[i])) {
aa18294a
JK
2889 case NDIS_80211_TYPE_FREQ_HOP:
2890 case NDIS_80211_TYPE_DIRECT_SEQ:
bf164cc0
JK
2891 priv->caps |= CAP_MODE_80211B;
2892 break;
aa18294a 2893 case NDIS_80211_TYPE_OFDM_A:
bf164cc0
JK
2894 priv->caps |= CAP_MODE_80211A;
2895 break;
aa18294a 2896 case NDIS_80211_TYPE_OFDM_G:
bf164cc0
JK
2897 priv->caps |= CAP_MODE_80211G;
2898 break;
2899 }
2900 }
bf164cc0
JK
2901 }
2902
0308383f
JK
2903 /* get device 802.11 capabilities, number of PMKIDs */
2904 caps = (struct ndis_80211_capability *)caps_buf;
2905 len = sizeof(caps_buf);
2906 retval = rndis_query_oid(usbdev, OID_802_11_CAPABILITY, caps, &len);
2907 if (retval >= 0) {
2908 netdev_dbg(usbdev->net, "OID_802_11_CAPABILITY -> len %d, "
2909 "ver %d, pmkids %d, auth-encr-pairs %d\n",
2910 le32_to_cpu(caps->length),
2911 le32_to_cpu(caps->version),
2912 le32_to_cpu(caps->num_pmkids),
2913 le32_to_cpu(caps->num_auth_encr_pair));
2914 wiphy->max_num_pmkids = le32_to_cpu(caps->num_pmkids);
2915 } else
2916 wiphy->max_num_pmkids = 0;
2917
bf164cc0
JK
2918 return retval;
2919}
2920
305e243e
JK
2921#define DEVICE_POLLER_JIFFIES (HZ)
2922static void rndis_device_poller(struct work_struct *work)
bf164cc0 2923{
582241a0 2924 struct rndis_wlan_private *priv =
305e243e
JK
2925 container_of(work, struct rndis_wlan_private,
2926 dev_poller_work.work);
bf164cc0 2927 struct usbnet *usbdev = priv->usbdev;
bf164cc0 2928 __le32 rssi, tmp;
a97b1f3d 2929 int len, ret, j;
305e243e 2930 int update_jiffies = DEVICE_POLLER_JIFFIES;
bf164cc0
JK
2931 void *buf;
2932
8b89a288
JK
2933 /* Only check/do workaround when connected. Calling is_associated()
2934 * also polls device with rndis_command() and catches for media link
2935 * indications.
2936 */
2937 if (!is_associated(usbdev))
bf164cc0 2938 goto end;
bf164cc0
JK
2939
2940 len = sizeof(rssi);
2941 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
8b89a288
JK
2942 if (ret == 0)
2943 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
bf164cc0 2944
60b86755
JP
2945 netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
2946 ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
bf164cc0 2947
a97b1f3d
JK
2948 /* Workaround transfer stalls on poor quality links.
2949 * TODO: find right way to fix these stalls (as stalls do not happen
2950 * with ndiswrapper/windows driver). */
77593ae2 2951 if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
bf164cc0
JK
2952 /* Decrease stats worker interval to catch stalls.
2953 * faster. Faster than 400-500ms causes packet loss,
2954 * Slower doesn't catch stalls fast enough.
2955 */
2956 j = msecs_to_jiffies(priv->param_workaround_interval);
305e243e
JK
2957 if (j > DEVICE_POLLER_JIFFIES)
2958 j = DEVICE_POLLER_JIFFIES;
bf164cc0
JK
2959 else if (j <= 0)
2960 j = 1;
2961 update_jiffies = j;
2962
2963 /* Send scan OID. Use of both OIDs is required to get device
2964 * working.
2965 */
35c26c2c 2966 tmp = cpu_to_le32(1);
bf164cc0
JK
2967 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2968 sizeof(tmp));
2969
2970 len = CONTROL_BUFFER_SIZE;
2971 buf = kmalloc(len, GFP_KERNEL);
2972 if (!buf)
2973 goto end;
2974
2975 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2976 kfree(buf);
2977 }
bf164cc0 2978
305e243e 2979end:
bf164cc0
JK
2980 if (update_jiffies >= HZ)
2981 update_jiffies = round_jiffies_relative(update_jiffies);
2982 else {
2983 j = round_jiffies_relative(update_jiffies);
2984 if (abs(j - update_jiffies) <= 10)
2985 update_jiffies = j;
2986 }
2987
305e243e
JK
2988 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2989 update_jiffies);
bf164cc0
JK
2990}
2991
c5c4fe90
JK
2992/*
2993 * driver/device initialization
2994 */
cef6e912 2995static void rndis_copy_module_params(struct usbnet *usbdev)
bf164cc0 2996{
582241a0 2997 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
2998
2999 priv->param_country[0] = modparam_country[0];
3000 priv->param_country[1] = modparam_country[1];
3001 priv->param_country[2] = 0;
3002 priv->param_frameburst = modparam_frameburst;
3003 priv->param_afterburner = modparam_afterburner;
3004 priv->param_power_save = modparam_power_save;
3005 priv->param_power_output = modparam_power_output;
3006 priv->param_roamtrigger = modparam_roamtrigger;
3007 priv->param_roamdelta = modparam_roamdelta;
bf164cc0
JK
3008
3009 priv->param_country[0] = toupper(priv->param_country[0]);
3010 priv->param_country[1] = toupper(priv->param_country[1]);
3011 /* doesn't support EU as country code, use FI instead */
3012 if (!strcmp(priv->param_country, "EU"))
3013 strcpy(priv->param_country, "FI");
3014
3015 if (priv->param_power_save < 0)
3016 priv->param_power_save = 0;
3017 else if (priv->param_power_save > 2)
3018 priv->param_power_save = 2;
3019
a7624837
JK
3020 if (priv->param_power_output < 0)
3021 priv->param_power_output = 0;
3022 else if (priv->param_power_output > 3)
3023 priv->param_power_output = 3;
3024
bf164cc0
JK
3025 if (priv->param_roamtrigger < -80)
3026 priv->param_roamtrigger = -80;
3027 else if (priv->param_roamtrigger > -60)
3028 priv->param_roamtrigger = -60;
3029
3030 if (priv->param_roamdelta < 0)
3031 priv->param_roamdelta = 0;
3032 else if (priv->param_roamdelta > 2)
3033 priv->param_roamdelta = 2;
3034
4d381ffb 3035 if (modparam_workaround_interval < 0)
bf164cc0 3036 priv->param_workaround_interval = 500;
4d381ffb
RK
3037 else
3038 priv->param_workaround_interval = modparam_workaround_interval;
cef6e912
JK
3039}
3040
3041static int bcm4320a_early_init(struct usbnet *usbdev)
3042{
6e850af5
JK
3043 /* copy module parameters for bcm4320a so that iwconfig reports txpower
3044 * and workaround parameter is copied to private structure correctly.
3045 */
3046 rndis_copy_module_params(usbdev);
3047
cef6e912
JK
3048 /* bcm4320a doesn't handle configuration parameters well. Try
3049 * set any and you get partially zeroed mac and broken device.
3050 */
3051
3052 return 0;
3053}
3054
3055static int bcm4320b_early_init(struct usbnet *usbdev)
3056{
3057 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3058 char buf[8];
3059
3060 rndis_copy_module_params(usbdev);
3061
3062 /* Early initialization settings, setting these won't have effect
3063 * if called after generic_rndis_bind().
3064 */
bf164cc0 3065
a19d7292
JK
3066 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3067 rndis_set_config_parameter_str(usbdev, "FrameBursting",
bf164cc0 3068 priv->param_frameburst ? "1" : "0");
a19d7292 3069 rndis_set_config_parameter_str(usbdev, "Afterburner",
bf164cc0
JK
3070 priv->param_afterburner ? "1" : "0");
3071 sprintf(buf, "%d", priv->param_power_save);
a19d7292 3072 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
bf164cc0 3073 sprintf(buf, "%d", priv->param_power_output);
a19d7292 3074 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
bf164cc0 3075 sprintf(buf, "%d", priv->param_roamtrigger);
a19d7292 3076 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
bf164cc0 3077 sprintf(buf, "%d", priv->param_roamdelta);
a19d7292 3078 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
bf164cc0
JK
3079
3080 return 0;
3081}
3082
23d12e2b 3083/* same as rndis_netdev_ops but with local multicast handler */
582241a0 3084static const struct net_device_ops rndis_wlan_netdev_ops = {
23d12e2b
DM
3085 .ndo_open = usbnet_open,
3086 .ndo_stop = usbnet_stop,
3087 .ndo_start_xmit = usbnet_start_xmit,
3088 .ndo_tx_timeout = usbnet_tx_timeout,
3089 .ndo_set_mac_address = eth_mac_addr,
3090 .ndo_validate_addr = eth_validate_addr,
582241a0 3091 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
23d12e2b
DM
3092};
3093
582241a0 3094static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
bf164cc0 3095{
5c8fa4f7 3096 struct wiphy *wiphy;
582241a0 3097 struct rndis_wlan_private *priv;
bf164cc0
JK
3098 int retval, len;
3099 __le32 tmp;
3100
5c8fa4f7
JL
3101 /* allocate wiphy and rndis private data
3102 * NOTE: We only support a single virtual interface, so wiphy
3103 * and wireless_dev are somewhat synonymous for this device.
3104 */
582241a0 3105 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
5c8fa4f7 3106 if (!wiphy)
bf164cc0
JK
3107 return -ENOMEM;
3108
5c8fa4f7
JL
3109 priv = wiphy_priv(wiphy);
3110 usbdev->net->ieee80211_ptr = &priv->wdev;
3111 priv->wdev.wiphy = wiphy;
3112 priv->wdev.iftype = NL80211_IFTYPE_STATION;
3113
bf164cc0 3114 /* These have to be initialized before calling generic_rndis_bind().
582241a0 3115 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
bf164cc0 3116 */
a19d7292 3117 usbdev->driver_priv = priv;
a19d7292 3118 priv->usbdev = usbdev;
bf164cc0
JK
3119
3120 mutex_init(&priv->command_lock);
bf164cc0 3121
8d4d99ae
JK
3122 /* because rndis_command() sleeps we need to use workqueue */
3123 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
582241a0 3124 INIT_WORK(&priv->work, rndis_wlan_worker);
305e243e 3125 INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
8d4d99ae
JK
3126 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3127
bf164cc0 3128 /* try bind rndis_host */
a19d7292 3129 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
bf164cc0
JK
3130 if (retval < 0)
3131 goto fail;
3132
3133 /* generic_rndis_bind set packet filter to multicast_all+
3134 * promisc mode which doesn't work well for our devices (device
3135 * picks up rssi to closest station instead of to access point).
3136 *
3137 * rndis_host wants to avoid all OID as much as possible
582241a0 3138 * so do promisc/multicast handling in rndis_wlan.
bf164cc0 3139 */
582241a0 3140 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
23d12e2b 3141
bf164cc0 3142 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
a19d7292 3143 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
bf164cc0
JK
3144 sizeof(tmp));
3145
3146 len = sizeof(tmp);
a19d7292
JK
3147 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
3148 &len);
bf164cc0
JK
3149 priv->multicast_size = le32_to_cpu(tmp);
3150 if (retval < 0 || priv->multicast_size < 0)
3151 priv->multicast_size = 0;
3152 if (priv->multicast_size > 0)
a19d7292 3153 usbdev->net->flags |= IFF_MULTICAST;
bf164cc0 3154 else
a19d7292 3155 usbdev->net->flags &= ~IFF_MULTICAST;
bf164cc0 3156
5c8fa4f7
JL
3157 /* fill-out wiphy structure and register w/ cfg80211 */
3158 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3159 wiphy->privid = rndis_wiphy_privid;
3160 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3161 | BIT(NL80211_IFTYPE_ADHOC);
3162 wiphy->max_scan_ssids = 1;
3163
4a7f13ee 3164 /* TODO: fill-out band/encr information based on priv->caps */
0308383f 3165 rndis_wlan_get_caps(usbdev, wiphy);
5c8fa4f7
JL
3166
3167 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3168 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3169 priv->band.channels = priv->channels;
3170 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3171 priv->band.bitrates = priv->rates;
3172 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3173 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
4d2a369e 3174 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
5c8fa4f7 3175
4a7f13ee
JK
3176 memcpy(priv->cipher_suites, rndis_cipher_suites,
3177 sizeof(rndis_cipher_suites));
3178 wiphy->cipher_suites = priv->cipher_suites;
3179 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3180
5c8fa4f7
JL
3181 set_wiphy_dev(wiphy, &usbdev->udev->dev);
3182
3183 if (wiphy_register(wiphy)) {
eb1a685e
JK
3184 retval = -ENODEV;
3185 goto fail;
5c8fa4f7
JL
3186 }
3187
a19d7292 3188 set_default_iw_params(usbdev);
bf164cc0 3189
d75ec2b7
JK
3190 /* set default rts/frag */
3191 rndis_set_wiphy_params(wiphy,
3192 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3193
bf164cc0 3194 /* turn radio on */
051ae0bf
JK
3195 priv->radio_on = true;
3196 disassociate(usbdev, true);
a19d7292 3197 netif_carrier_off(usbdev->net);
bf164cc0 3198
bf164cc0
JK
3199 return 0;
3200
3201fail:
305e243e 3202 cancel_delayed_work_sync(&priv->dev_poller_work);
8d4d99ae
JK
3203 cancel_delayed_work_sync(&priv->scan_work);
3204 cancel_work_sync(&priv->work);
3205 flush_workqueue(priv->workqueue);
3206 destroy_workqueue(priv->workqueue);
3207
eb1a685e 3208 wiphy_free(wiphy);
bf164cc0
JK
3209 return retval;
3210}
3211
582241a0 3212static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
bf164cc0 3213{
582241a0 3214 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
bf164cc0
JK
3215
3216 /* turn radio off */
051ae0bf 3217 disassociate(usbdev, false);
bf164cc0 3218
305e243e 3219 cancel_delayed_work_sync(&priv->dev_poller_work);
71a7b26d 3220 cancel_delayed_work_sync(&priv->scan_work);
bf164cc0
JK
3221 cancel_work_sync(&priv->work);
3222 flush_workqueue(priv->workqueue);
3223 destroy_workqueue(priv->workqueue);
3224
a19d7292 3225 rndis_unbind(usbdev, intf);
5c8fa4f7
JL
3226
3227 wiphy_unregister(priv->wdev.wiphy);
3228 wiphy_free(priv->wdev.wiphy);
bf164cc0
JK
3229}
3230
582241a0 3231static int rndis_wlan_reset(struct usbnet *usbdev)
bf164cc0 3232{
110736de 3233 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
7eaab708 3234 int retval;
110736de 3235
60b86755 3236 netdev_dbg(usbdev->net, "%s()\n", __func__);
110736de 3237
7eaab708
JK
3238 retval = rndis_reset(usbdev);
3239 if (retval)
60b86755 3240 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
7eaab708 3241
e5a11a82
JK
3242 /* rndis_reset cleared multicast list, so restore here.
3243 (set_multicast_list() also turns on current packet filter) */
7eaab708
JK
3244 set_multicast_list(usbdev);
3245
305e243e
JK
3246 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3247 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
110736de 3248
a19d7292 3249 return deauthenticate(usbdev);
bf164cc0
JK
3250}
3251
222ec50a
JK
3252static int rndis_wlan_stop(struct usbnet *usbdev)
3253{
110736de
JK
3254 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3255 int retval;
e5a11a82 3256 __le32 filter;
110736de 3257
60b86755 3258 netdev_dbg(usbdev->net, "%s()\n", __func__);
110736de 3259
051ae0bf 3260 retval = disassociate(usbdev, false);
110736de
JK
3261
3262 priv->work_pending = 0;
305e243e 3263 cancel_delayed_work_sync(&priv->dev_poller_work);
110736de
JK
3264 cancel_delayed_work_sync(&priv->scan_work);
3265 cancel_work_sync(&priv->work);
3266 flush_workqueue(priv->workqueue);
3267
005ba2f1
JK
3268 if (priv->scan_request) {
3269 cfg80211_scan_done(priv->scan_request, true);
3270 priv->scan_request = NULL;
3271 }
3272
e5a11a82
JK
3273 /* Set current packet filter zero to block receiving data packets from
3274 device. */
3275 filter = 0;
3276 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
3277 sizeof(filter));
3278
110736de 3279 return retval;
222ec50a
JK
3280}
3281
bf164cc0
JK
3282static const struct driver_info bcm4320b_info = {
3283 .description = "Wireless RNDIS device, BCM4320b based",
1487cd5e
JK
3284 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3285 FLAG_AVOID_UNLINK_URBS,
582241a0
JK
3286 .bind = rndis_wlan_bind,
3287 .unbind = rndis_wlan_unbind,
bf164cc0
JK
3288 .status = rndis_status,
3289 .rx_fixup = rndis_rx_fixup,
3290 .tx_fixup = rndis_tx_fixup,
582241a0 3291 .reset = rndis_wlan_reset,
222ec50a 3292 .stop = rndis_wlan_stop,
59620e9f 3293 .early_init = bcm4320b_early_init,
2a4901bc 3294 .indication = rndis_wlan_indication,
bf164cc0
JK
3295};
3296
3297static const struct driver_info bcm4320a_info = {
3298 .description = "Wireless RNDIS device, BCM4320a based",
1487cd5e
JK
3299 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3300 FLAG_AVOID_UNLINK_URBS,
582241a0
JK
3301 .bind = rndis_wlan_bind,
3302 .unbind = rndis_wlan_unbind,
bf164cc0
JK
3303 .status = rndis_status,
3304 .rx_fixup = rndis_rx_fixup,
3305 .tx_fixup = rndis_tx_fixup,
582241a0 3306 .reset = rndis_wlan_reset,
222ec50a 3307 .stop = rndis_wlan_stop,
59620e9f 3308 .early_init = bcm4320a_early_init,
2a4901bc 3309 .indication = rndis_wlan_indication,
bf164cc0
JK
3310};
3311
582241a0 3312static const struct driver_info rndis_wlan_info = {
bf164cc0 3313 .description = "Wireless RNDIS device",
1487cd5e
JK
3314 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3315 FLAG_AVOID_UNLINK_URBS,
582241a0
JK
3316 .bind = rndis_wlan_bind,
3317 .unbind = rndis_wlan_unbind,
bf164cc0
JK
3318 .status = rndis_status,
3319 .rx_fixup = rndis_rx_fixup,
3320 .tx_fixup = rndis_tx_fixup,
582241a0 3321 .reset = rndis_wlan_reset,
222ec50a 3322 .stop = rndis_wlan_stop,
59620e9f 3323 .early_init = bcm4320a_early_init,
2a4901bc 3324 .indication = rndis_wlan_indication,
bf164cc0
JK
3325};
3326
3327/*-------------------------------------------------------------------------*/
3328
3329static const struct usb_device_id products [] = {
3330#define RNDIS_MASTER_INTERFACE \
3331 .bInterfaceClass = USB_CLASS_COMM, \
3332 .bInterfaceSubClass = 2 /* ACM */, \
3333 .bInterfaceProtocol = 0x0ff
3334
3335/* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3336 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3337 */
3338{
3339 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3340 | USB_DEVICE_ID_MATCH_DEVICE,
3341 .idVendor = 0x0411,
3342 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
3343 RNDIS_MASTER_INTERFACE,
3344 .driver_info = (unsigned long) &bcm4320b_info,
3345}, {
3346 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3347 | USB_DEVICE_ID_MATCH_DEVICE,
3348 .idVendor = 0x0baf,
3349 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
3350 RNDIS_MASTER_INTERFACE,
3351 .driver_info = (unsigned long) &bcm4320b_info,
3352}, {
3353 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3354 | USB_DEVICE_ID_MATCH_DEVICE,
3355 .idVendor = 0x050d,
3356 .idProduct = 0x011b, /* Belkin F5D7051 */
3357 RNDIS_MASTER_INTERFACE,
3358 .driver_info = (unsigned long) &bcm4320b_info,
3359}, {
3360 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3361 | USB_DEVICE_ID_MATCH_DEVICE,
3362 .idVendor = 0x1799, /* Belkin has two vendor ids */
3363 .idProduct = 0x011b, /* Belkin F5D7051 */
3364 RNDIS_MASTER_INTERFACE,
3365 .driver_info = (unsigned long) &bcm4320b_info,
3366}, {
3367 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3368 | USB_DEVICE_ID_MATCH_DEVICE,
3369 .idVendor = 0x13b1,
3370 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
3371 RNDIS_MASTER_INTERFACE,
3372 .driver_info = (unsigned long) &bcm4320b_info,
3373}, {
3374 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3375 | USB_DEVICE_ID_MATCH_DEVICE,
3376 .idVendor = 0x13b1,
3377 .idProduct = 0x0026, /* Linksys WUSB54GSC */
3378 RNDIS_MASTER_INTERFACE,
3379 .driver_info = (unsigned long) &bcm4320b_info,
3380}, {
3381 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3382 | USB_DEVICE_ID_MATCH_DEVICE,
3383 .idVendor = 0x0b05,
3384 .idProduct = 0x1717, /* Asus WL169gE */
3385 RNDIS_MASTER_INTERFACE,
3386 .driver_info = (unsigned long) &bcm4320b_info,
3387}, {
3388 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3389 | USB_DEVICE_ID_MATCH_DEVICE,
3390 .idVendor = 0x0a5c,
3391 .idProduct = 0xd11b, /* Eminent EM4045 */
3392 RNDIS_MASTER_INTERFACE,
3393 .driver_info = (unsigned long) &bcm4320b_info,
3394}, {
3395 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3396 | USB_DEVICE_ID_MATCH_DEVICE,
3397 .idVendor = 0x1690,
3398 .idProduct = 0x0715, /* BT Voyager 1055 */
3399 RNDIS_MASTER_INTERFACE,
3400 .driver_info = (unsigned long) &bcm4320b_info,
3401},
3402/* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3403 * parameters available, hardware probably contain older firmware version with
3404 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3405 */
3406{
3407 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3408 | USB_DEVICE_ID_MATCH_DEVICE,
3409 .idVendor = 0x13b1,
3410 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3411 RNDIS_MASTER_INTERFACE,
3412 .driver_info = (unsigned long) &bcm4320a_info,
3413}, {
3414 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3415 | USB_DEVICE_ID_MATCH_DEVICE,
3416 .idVendor = 0x0baf,
3417 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3418 RNDIS_MASTER_INTERFACE,
3419 .driver_info = (unsigned long) &bcm4320a_info,
3420}, {
3421 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3422 | USB_DEVICE_ID_MATCH_DEVICE,
3423 .idVendor = 0x0411,
3424 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3425 RNDIS_MASTER_INTERFACE,
3426 .driver_info = (unsigned long) &bcm4320a_info,
3427},
3428/* Generic Wireless RNDIS devices that we don't have exact
3429 * idVendor/idProduct/chip yet.
3430 */
3431{
3432 /* RNDIS is MSFT's un-official variant of CDC ACM */
3433 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
582241a0 3434 .driver_info = (unsigned long) &rndis_wlan_info,
bf164cc0
JK
3435}, {
3436 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3437 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
582241a0 3438 .driver_info = (unsigned long) &rndis_wlan_info,
bf164cc0
JK
3439},
3440 { }, // END
3441};
3442MODULE_DEVICE_TABLE(usb, products);
3443
3444static struct usb_driver rndis_wlan_driver = {
3445 .name = "rndis_wlan",
3446 .id_table = products,
3447 .probe = usbnet_probe,
3448 .disconnect = usbnet_disconnect,
3449 .suspend = usbnet_suspend,
3450 .resume = usbnet_resume,
3451};
3452
3453static int __init rndis_wlan_init(void)
3454{
3455 return usb_register(&rndis_wlan_driver);
3456}
3457module_init(rndis_wlan_init);
3458
3459static void __exit rndis_wlan_exit(void)
3460{
3461 usb_deregister(&rndis_wlan_driver);
3462}
3463module_exit(rndis_wlan_exit);
3464
3465MODULE_AUTHOR("Bjorge Dijkstra");
3466MODULE_AUTHOR("Jussi Kivilinna");
3467MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3468MODULE_LICENSE("GPL");
3469
This page took 0.667878 seconds and 5 git commands to generate.