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