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