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