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