blob: 2887047069f217824c9d21cec142167ae1f42fbc [file] [log] [blame]
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001/*
2 * Driver for RNDIS based wireless USB devices.
3 *
4 * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
Jussi Kivilinna9656e852009-05-22 17:40:12 +03005 * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02006 *
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>
Johannes Berg2c7060022008-10-30 22:09:54 +010040#include <linux/ieee80211.h>
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +020041#include <linux/if_arp.h>
42#include <linux/ctype.h>
43#include <linux/spinlock.h>
44#include <net/iw_handler.h>
John W. Linville5c8fa4f2009-03-26 23:39:53 +020045#include <net/cfg80211.h>
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +020046#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
Jussi Kivilinna77593ae2009-11-22 20:16:47 +020086static int modparam_workaround_interval;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +020087module_param_named(workaround_interval, modparam_workaround_interval,
88 int, 0444);
89MODULE_PARM_DESC(workaround_interval,
Jussi Kivilinna77593ae2009-11-22 20:16:47 +020090 "set stall workaround interval in msecs (0=disabled) (default: 0)");
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +020091
92
93/* various RNDIS OID defs */
Harvey Harrison35c26c22009-02-14 22:56:56 -080094#define OID_GEN_LINK_SPEED cpu_to_le32(0x00010107)
95#define OID_GEN_RNDIS_CONFIG_PARAMETER cpu_to_le32(0x0001021b)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +020096
Harvey Harrison35c26c22009-02-14 22:56:56 -080097#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)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200102
Harvey Harrison35c26c22009-02-14 22:56:56 -0800103#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)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200106
Harvey Harrison35c26c22009-02-14 22:56:56 -0800107#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)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200131
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.
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300141 * 100% : 20 mW ~ 13dBm
142 * 75% : 15 mW ~ 12dBm
143 * 50% : 10 mW ~ 10dBm
144 * 25% : 5 mW ~ 7dBm
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200145 */
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300146#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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200150
151
152/* codes for "status" field of completion messages */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800153#define RNDIS_STATUS_ADAPTER_NOT_READY cpu_to_le32(0xc0010011)
154#define RNDIS_STATUS_ADAPTER_NOT_OPEN cpu_to_le32(0xc0010012)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200155
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
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200164enum ndis_80211_net_type {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300165 NDIS_80211_TYPE_FREQ_HOP,
166 NDIS_80211_TYPE_DIRECT_SEQ,
167 NDIS_80211_TYPE_OFDM_A,
168 NDIS_80211_TYPE_OFDM_G
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200169};
170
171enum ndis_80211_net_infra {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300172 NDIS_80211_INFRA_ADHOC,
173 NDIS_80211_INFRA_INFRA,
174 NDIS_80211_INFRA_AUTO_UNKNOWN
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200175};
176
177enum ndis_80211_auth_mode {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300178 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
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200186};
187
188enum ndis_80211_encr_status {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300189 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
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200197};
198
199enum ndis_80211_priv_filter {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300200 NDIS_80211_PRIV_ACCEPT_ALL,
201 NDIS_80211_PRIV_8021X_WEP
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200202};
203
Jussi Kivilinna030645a2009-07-30 19:41:58 +0300204enum 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
Jussi Kivilinnab4703a22009-05-22 17:40:20 +0300222enum ndis_80211_addkey_bits {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300223 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)
Jussi Kivilinnab4703a22009-05-22 17:40:20 +0300227};
228
229enum ndis_80211_addwep_bits {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +0300230 NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
231 NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
Jussi Kivilinnab4703a22009-05-22 17:40:20 +0300232};
233
Jussi Kivilinna030645a2009-07-30 19:41:58 +0300234struct 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 {
Jussi Kivilinna53d27ea2009-09-01 15:33:06 +0300256 __le32 media_stream_mode;
257 __le32 radio_status;
Jussi Kivilinna030645a2009-07-30 19:41:58 +0300258 struct ndis_80211_auth_request auth_request[0];
259 struct ndis_80211_pmkid_cand_list cand_list;
260 } u;
261} __attribute__((packed));
262
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200263struct ndis_80211_ssid {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200264 __le32 length;
265 u8 essid[NDIS_802_11_LENGTH_SSID];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200266} __attribute__((packed));
267
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200268struct ndis_80211_conf_freq_hop {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200269 __le32 length;
270 __le32 hop_pattern;
271 __le32 hop_set;
272 __le32 dwell_time;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200273} __attribute__((packed));
274
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200275struct ndis_80211_conf {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200276 __le32 length;
277 __le32 beacon_period;
278 __le32 atim_window;
279 __le32 ds_config;
280 struct ndis_80211_conf_freq_hop fh_config;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200281} __attribute__((packed));
282
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200283struct ndis_80211_bssid_ex {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200284 __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];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200296} __attribute__((packed));
297
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200298struct ndis_80211_bssid_list_ex {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200299 __le32 num_items;
300 struct ndis_80211_bssid_ex bssid[0];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200301} __attribute__((packed));
302
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200303struct ndis_80211_fixed_ies {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200304 u8 timestamp[8];
305 __le16 beacon_interval;
306 __le16 capabilities;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200307} __attribute__((packed));
308
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200309struct ndis_80211_wep_key {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200310 __le32 size;
311 __le32 index;
312 __le32 length;
313 u8 material[32];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200314} __attribute__((packed));
315
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200316struct ndis_80211_key {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200317 __le32 size;
318 __le32 index;
319 __le32 length;
320 u8 bssid[6];
321 u8 padding[6];
322 u8 rsc[8];
323 u8 material[32];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200324} __attribute__((packed));
325
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200326struct ndis_80211_remove_key {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200327 __le32 size;
328 __le32 index;
329 u8 bssid[6];
Jussi Kivilinna9d409342009-07-30 19:42:03 +0300330 u8 padding[2];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200331} __attribute__((packed));
332
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200333struct ndis_config_param {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200334 __le32 name_offs;
335 __le32 name_length;
336 __le32 type;
337 __le32 value_offs;
338 __le32 value_length;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200339} __attribute__((packed));
340
Scott Ashcroft43646232008-05-27 00:06:15 +0300341struct 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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200361/*
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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200370
Jussi Kivilinna6010ce02008-06-02 18:35:21 +0300371#define WORK_LINK_UP (1<<0)
372#define WORK_LINK_DOWN (1<<1)
373#define WORK_SET_MULTICAST_LIST (1<<2)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200374
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300375#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
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300384#define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
385
John W. Linville5c8fa4f2009-03-26 23:39:53 +0200386static 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
Jussi Kivilinna4a7f13e2009-08-26 15:53:02 +0300418static 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
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +0300425struct rndis_wlan_encr_key {
426 int len;
Jussi Kivilinna84bf8402009-08-28 13:27:58 +0300427 u32 cipher;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +0300428 u8 material[32];
429 u8 bssid[ETH_ALEN];
430 bool pairwise;
431 bool tx_key;
432};
433
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200434/* RNDIS device private data */
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300435struct rndis_wlan_private {
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200436 struct usbnet *usbdev;
437
John W. Linville5c8fa4f2009-03-26 23:39:53 +0200438 struct wireless_dev wdev;
439
Jussi Kivilinna71a7b262009-03-26 23:40:31 +0200440 struct cfg80211_scan_request *scan_request;
441
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200442 struct workqueue_struct *workqueue;
Jussi Kivilinna305e2432009-08-28 13:28:14 +0300443 struct delayed_work dev_poller_work;
Jussi Kivilinna71a7b262009-03-26 23:40:31 +0200444 struct delayed_work scan_work;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200445 struct work_struct work;
446 struct mutex command_lock;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200447 unsigned long work_pending;
Jussi Kivilinna8b89a282009-08-28 13:28:03 +0300448 int last_qual;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200449
John W. Linville5c8fa4f2009-03-26 23:39:53 +0200450 struct ieee80211_supported_band band;
451 struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
452 struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
Jussi Kivilinna4a7f13e2009-08-26 15:53:02 +0300453 u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
John W. Linville5c8fa4f2009-03-26 23:39:53 +0200454
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200455 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 */
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +0300469 bool radio_on;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200470 int infra_mode;
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300471 bool connected;
Jussi Kivilinna8b89a282009-08-28 13:28:03 +0300472 u8 bssid[ETH_ALEN];
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200473 struct ndis_80211_ssid essid;
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300474 __le32 current_command_oid;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200475
476 /* encryption stuff */
477 int encr_tx_key_index;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +0300478 struct rndis_wlan_encr_key encr_keys[4];
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300479 enum nl80211_auth_type wpa_auth_type;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200480 int wpa_version;
481 int wpa_keymgmt;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200482 int wpa_ie_len;
483 u8 *wpa_ie;
484 int wpa_cipher_pair;
485 int wpa_cipher_group;
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300486
487 u8 command_buffer[COMMAND_BUFFER_SIZE];
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200488};
489
John W. Linville964c1d42009-03-26 23:40:01 +0200490/*
491 * cfg80211 ops
492 */
Johannes Berge36d56b2009-06-09 21:04:43 +0200493static int rndis_change_virtual_intf(struct wiphy *wiphy,
494 struct net_device *dev,
John W. Linville964c1d42009-03-26 23:40:01 +0200495 enum nl80211_iftype type, u32 *flags,
496 struct vif_params *params);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200497
Jussi Kivilinna71a7b262009-03-26 23:40:31 +0200498static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
499 struct cfg80211_scan_request *request);
500
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +0300501static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
502
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300503static 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
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300507static 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
Jussi Kivilinna5554adb2009-08-28 13:27:53 +0300518static int rndis_set_channel(struct wiphy *wiphy,
519 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type);
520
Jussi Kivilinna84bf8402009-08-28 13:27:58 +0300521static 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
Jussi Kivilinna8b89a282009-08-28 13:28:03 +0300531static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
532 u8 *mac, struct station_info *sinfo);
533
Jussi Kivilinnad695df92009-08-28 13:28:09 +0300534static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
535 int idx, u8 *mac, struct station_info *sinfo);
536
Johannes Bergcaa6dfa2009-04-21 10:55:10 +0200537static struct cfg80211_ops rndis_config_ops = {
John W. Linville964c1d42009-03-26 23:40:01 +0200538 .change_virtual_intf = rndis_change_virtual_intf,
Jussi Kivilinna71a7b262009-03-26 23:40:31 +0200539 .scan = rndis_scan,
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +0300540 .set_wiphy_params = rndis_set_wiphy_params,
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300541 .set_tx_power = rndis_set_tx_power,
542 .get_tx_power = rndis_get_tx_power,
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300543 .connect = rndis_connect,
544 .disconnect = rndis_disconnect,
545 .join_ibss = rndis_join_ibss,
546 .leave_ibss = rndis_leave_ibss,
Jussi Kivilinna5554adb2009-08-28 13:27:53 +0300547 .set_channel = rndis_set_channel,
Jussi Kivilinna84bf8402009-08-28 13:27:58 +0300548 .add_key = rndis_add_key,
549 .del_key = rndis_del_key,
550 .set_default_key = rndis_set_default_key,
Jussi Kivilinna8b89a282009-08-28 13:28:03 +0300551 .get_station = rndis_get_station,
Jussi Kivilinnad695df92009-08-28 13:28:09 +0300552 .dump_station = rndis_dump_station,
John W. Linville964c1d42009-03-26 23:40:01 +0200553};
554
Johannes Bergcaa6dfa2009-04-21 10:55:10 +0200555static void *rndis_wiphy_privid = &rndis_wiphy_privid;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200556
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200557
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300558static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200559{
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300560 return (struct rndis_wlan_private *)dev->driver_priv;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200561}
562
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300563static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200564{
Jussi Kivilinna222ec502009-06-16 17:17:32 +0300565 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 }
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200576}
577
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +0300578static 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
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300586static 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
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300613#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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200674/* 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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200697static int rndis_query_oid(struct usbnet *dev, __le32 oid, void *data, int *len)
698{
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300699 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200700 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;
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300711
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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200722 memset(u.get, 0, sizeof *u.get);
723 u.get->msg_type = RNDIS_MSG_QUERY;
Harvey Harrison35c26c22009-02-14 22:56:56 -0800724 u.get->msg_len = cpu_to_le32(sizeof *u.get);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200725 u.get->oid = oid;
726
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300727 priv->current_command_oid = oid;
Jussi Kivilinna818727b2008-06-18 15:40:12 +0300728 ret = rndis_command(dev, u.header, buflen);
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300729 priv->current_command_oid = 0;
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300730 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000731 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
732 __func__, oid_to_string(oid), ret,
733 le32_to_cpu(u.get_c->status));
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300734
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200735 if (ret == 0) {
Jussi Kivilinnac1f8ca12009-11-22 20:16:42 +0200736 memcpy(data, u.buf + le32_to_cpu(u.get_c->offset) + 8, *len);
737
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200738 ret = le32_to_cpu(u.get_c->len);
Jussi Kivilinna5fd8f252009-08-28 12:59:00 +0300739 if (ret > *len)
740 *len = ret;
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300741
Jussi Kivilinnac1f8ca12009-11-22 20:16:42 +0200742 ret = rndis_error_status(u.get_c->status);
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300743 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000744 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
745 __func__, oid_to_string(oid),
746 le32_to_cpu(u.get_c->status), ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200747 }
748
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300749 mutex_unlock(&priv->command_lock);
750
751 if (u.buf != priv->command_buffer)
752 kfree(u.buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200753 return ret;
754}
755
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200756static int rndis_set_oid(struct usbnet *dev, __le32 oid, void *data, int len)
757{
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300758 struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200759 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;
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300770
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);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200780
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);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800786 u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
787 u.set->handle = cpu_to_le32(0);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200788 memcpy(u.buf + sizeof(*u.set), data, len);
789
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300790 priv->current_command_oid = oid;
Jussi Kivilinna818727b2008-06-18 15:40:12 +0300791 ret = rndis_command(dev, u.header, buflen);
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300792 priv->current_command_oid = 0;
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300793 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000794 netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
795 __func__, oid_to_string(oid), ret,
796 le32_to_cpu(u.set_c->status));
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300797
798 if (ret == 0) {
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200799 ret = rndis_error_status(u.set_c->status);
800
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300801 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000802 netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
803 __func__, oid_to_string(oid),
804 le32_to_cpu(u.set_c->status), ret);
Jussi Kivilinna27b7b5c2009-07-30 19:41:47 +0300805 }
806
Jussi Kivilinna90d07342008-06-12 20:19:19 +0300807 mutex_unlock(&priv->command_lock);
808
809 if (u.buf != priv->command_buffer)
810 kfree(u.buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200811 return ret;
812}
813
Jussi Kivilinna7eaab702009-07-30 19:41:37 +0300814static 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));
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +0300826 priv->current_command_oid = 0;
Jussi Kivilinna7eaab702009-07-30 19:41:37 +0300827 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
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200836/*
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{
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200844 struct ndis_config_param *infobuf;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200845 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)
Joe Perches60b86752010-02-17 10:30:23 +0000873 netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
874 param, (u8 *)value);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200875 else
Joe Perches60b86752010-02-17 10:30:23 +0000876 netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
877 param, *(u32 *)value);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200878
Jussi Kivilinna461b3f22008-03-08 01:23:25 +0200879 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);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200884
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
Joe Perches60b86752010-02-17 10:30:23 +0000900 netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200901 for (i = 0; i < info_len; i += 12) {
902 u32 *tmp = (u32 *)((u8 *)infobuf + i);
Joe Perches60b86752010-02-17 10:30:23 +0000903 netdev_dbg(dev->net, "%08X:%08X:%08X\n",
904 cpu_to_be32(tmp[0]),
905 cpu_to_be32(tmp[1]),
906 cpu_to_be32(tmp[2]));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200907 }
908#endif
909
910 ret = rndis_set_oid(dev, OID_GEN_RNDIS_CONFIG_PARAMETER,
911 infobuf, info_len);
912 if (ret != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000913 netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
914 ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200915
916 kfree(infobuf);
917 return ret;
918}
919
920static int rndis_set_config_parameter_str(struct usbnet *dev,
921 char *param, char *value)
922{
Jussi Kivilinnac5c4fe92009-09-01 15:33:00 +0300923 return rndis_set_config_parameter(dev, param, 2, value);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200924}
925
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200926/*
927 * data conversion functions
928 */
929static int level_to_qual(int level)
930{
931 int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
932 return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
933}
934
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200935/*
936 * common functions
937 */
Jussi Kivilinna9f77cca2009-08-28 12:59:26 +0300938static int set_infra_mode(struct usbnet *usbdev, int mode);
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +0300939static void restore_keys(struct usbnet *usbdev);
Jussi Kivilinnadb0dd392009-08-28 12:58:55 +0300940static int rndis_check_bssid_list(struct usbnet *usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200941
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +0200942static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200943{
Jussi Kivilinna582241a02009-06-04 20:13:25 +0300944 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200945 int ret;
946
947 ret = rndis_set_oid(usbdev, OID_802_11_SSID, ssid, sizeof(*ssid));
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300948 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000949 netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300950 return ret;
951 }
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200952 if (ret == 0) {
953 memcpy(&priv->essid, ssid, sizeof(priv->essid));
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +0300954 priv->radio_on = true;
Joe Perches60b86752010-02-17 10:30:23 +0000955 netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200956 }
957
958 return ret;
959}
960
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300961static int set_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
962{
963 int ret;
964
965 ret = rndis_set_oid(usbdev, OID_802_11_BSSID, bssid, ETH_ALEN);
966 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000967 netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
968 bssid, ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +0300969 return ret;
970 }
971
972 return ret;
973}
974
975static int clear_bssid(struct usbnet *usbdev)
976{
977 u8 broadcast_mac[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
978
979 return set_bssid(usbdev, broadcast_mac);
980}
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +0200981
982static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
983{
984 int ret, len;
985
986 len = ETH_ALEN;
987 ret = rndis_query_oid(usbdev, OID_802_11_BSSID, bssid, &len);
988
989 if (ret != 0)
990 memset(bssid, 0, ETH_ALEN);
991
992 return ret;
993}
994
Scott Ashcroft43646232008-05-27 00:06:15 +0300995static int get_association_info(struct usbnet *usbdev,
996 struct ndis_80211_assoc_info *info, int len)
997{
998 return rndis_query_oid(usbdev, OID_802_11_ASSOCIATION_INFORMATION,
999 info, &len);
1000}
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001001
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001002static bool is_associated(struct usbnet *usbdev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001003{
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001004 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001005 u8 bssid[ETH_ALEN];
1006 int ret;
1007
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001008 if (!priv->radio_on)
1009 return false;
1010
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001011 ret = get_bssid(usbdev, bssid);
1012
Jussi Kivilinna7b1fff92009-08-28 12:59:15 +03001013 return (ret == 0 && !is_zero_ether_addr(bssid));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001014}
1015
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03001016static int disassociate(struct usbnet *usbdev, bool reset_ssid)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001017{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001018 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +02001019 struct ndis_80211_ssid ssid;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001020 int i, ret = 0;
1021
1022 if (priv->radio_on) {
1023 ret = rndis_set_oid(usbdev, OID_802_11_DISASSOCIATE, NULL, 0);
1024 if (ret == 0) {
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03001025 priv->radio_on = false;
Joe Perches60b86752010-02-17 10:30:23 +00001026 netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1027 __func__);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001028
1029 if (reset_ssid)
1030 msleep(100);
1031 }
1032 }
1033
1034 /* disassociate causes radio to be turned off; if reset_ssid
1035 * is given, set random ssid to enable radio */
1036 if (reset_ssid) {
Jussi Kivilinna9f77cca2009-08-28 12:59:26 +03001037 /* Set device to infrastructure mode so we don't get ad-hoc
1038 * 'media connect' indications with the random ssid.
1039 */
1040 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1041
Jussi Kivilinna461b3f22008-03-08 01:23:25 +02001042 ssid.length = cpu_to_le32(sizeof(ssid.essid));
1043 get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1044 ssid.essid[0] = 0x1;
1045 ssid.essid[1] = 0xff;
1046 for (i = 2; i < sizeof(ssid.essid); i++)
1047 ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001048 ret = set_essid(usbdev, &ssid);
1049 }
1050 return ret;
1051}
1052
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001053static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1054 enum nl80211_auth_type auth_type, int keymgmt)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001055{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001056 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001057 __le32 tmp;
1058 int auth_mode, ret;
1059
Joe Perches60b86752010-02-17 10:30:23 +00001060 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1061 __func__, wpa_version, auth_type, keymgmt);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001062
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001063 if (wpa_version & NL80211_WPA_VERSION_2) {
1064 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001065 auth_mode = NDIS_80211_AUTH_WPA2;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001066 else
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001067 auth_mode = NDIS_80211_AUTH_WPA2_PSK;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001068 } else if (wpa_version & NL80211_WPA_VERSION_1) {
1069 if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001070 auth_mode = NDIS_80211_AUTH_WPA;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001071 else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001072 auth_mode = NDIS_80211_AUTH_WPA_PSK;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001073 else
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001074 auth_mode = NDIS_80211_AUTH_WPA_NONE;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001075 } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1076 auth_mode = NDIS_80211_AUTH_SHARED;
1077 else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001078 auth_mode = NDIS_80211_AUTH_OPEN;
Jussi Kivilinna634a5552009-11-16 12:49:43 +02001079 else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1080 auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001081 else
1082 return -ENOTSUPP;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001083
1084 tmp = cpu_to_le32(auth_mode);
1085 ret = rndis_set_oid(usbdev, OID_802_11_AUTHENTICATION_MODE, &tmp,
1086 sizeof(tmp));
1087 if (ret != 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001088 netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1089 ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001090 return ret;
1091 }
1092
1093 priv->wpa_version = wpa_version;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001094 priv->wpa_auth_type = auth_type;
1095 priv->wpa_keymgmt = keymgmt;
1096
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001097 return 0;
1098}
1099
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001100static int set_priv_filter(struct usbnet *usbdev)
1101{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001102 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001103 __le32 tmp;
1104
Joe Perches60b86752010-02-17 10:30:23 +00001105 netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1106 __func__, priv->wpa_version);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001107
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001108 if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1109 priv->wpa_version & NL80211_WPA_VERSION_1)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001110 tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001111 else
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001112 tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001113
1114 return rndis_set_oid(usbdev, OID_802_11_PRIVACY_FILTER, &tmp,
1115 sizeof(tmp));
1116}
1117
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001118static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1119{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001120 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001121 __le32 tmp;
1122 int encr_mode, ret;
1123
Joe Perches60b86752010-02-17 10:30:23 +00001124 netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1125 __func__, pairwise, groupwise);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001126
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001127 if (pairwise & RNDIS_WLAN_ALG_CCMP)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001128 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001129 else if (pairwise & RNDIS_WLAN_ALG_TKIP)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001130 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001131 else if (pairwise & RNDIS_WLAN_ALG_WEP)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001132 encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001133 else if (groupwise & RNDIS_WLAN_ALG_CCMP)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001134 encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001135 else if (groupwise & RNDIS_WLAN_ALG_TKIP)
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001136 encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001137 else
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001138 encr_mode = NDIS_80211_ENCR_DISABLED;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001139
1140 tmp = cpu_to_le32(encr_mode);
1141 ret = rndis_set_oid(usbdev, OID_802_11_ENCRYPTION_STATUS, &tmp,
1142 sizeof(tmp));
1143 if (ret != 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001144 netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1145 ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001146 return ret;
1147 }
1148
1149 priv->wpa_cipher_pair = pairwise;
1150 priv->wpa_cipher_group = groupwise;
1151 return 0;
1152}
1153
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001154static int set_infra_mode(struct usbnet *usbdev, int mode)
1155{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001156 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001157 __le32 tmp;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001158 int ret;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001159
Joe Perches60b86752010-02-17 10:30:23 +00001160 netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1161 __func__, priv->infra_mode);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001162
1163 tmp = cpu_to_le32(mode);
1164 ret = rndis_set_oid(usbdev, OID_802_11_INFRASTRUCTURE_MODE, &tmp,
1165 sizeof(tmp));
1166 if (ret != 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001167 netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1168 ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001169 return ret;
1170 }
1171
1172 /* NDIS drivers clear keys when infrastructure mode is
1173 * changed. But Linux tools assume otherwise. So set the
1174 * keys */
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001175 restore_keys(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001176
1177 priv->infra_mode = mode;
1178 return 0;
1179}
1180
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03001181static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1182{
1183 __le32 tmp;
1184
Joe Perches60b86752010-02-17 10:30:23 +00001185 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03001186
1187 if (rts_threshold < 0 || rts_threshold > 2347)
1188 rts_threshold = 2347;
1189
1190 tmp = cpu_to_le32(rts_threshold);
1191 return rndis_set_oid(usbdev, OID_802_11_RTS_THRESHOLD, &tmp,
1192 sizeof(tmp));
1193}
1194
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03001195static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1196{
1197 __le32 tmp;
1198
Joe Perches60b86752010-02-17 10:30:23 +00001199 netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03001200
1201 if (frag_threshold < 256 || frag_threshold > 2346)
1202 frag_threshold = 2346;
1203
1204 tmp = cpu_to_le32(frag_threshold);
1205 return rndis_set_oid(usbdev, OID_802_11_FRAGMENTATION_THRESHOLD, &tmp,
1206 sizeof(tmp));
1207}
1208
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001209static void set_default_iw_params(struct usbnet *usbdev)
1210{
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001211 set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001212 set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1213 RNDIS_WLAN_KEY_MGMT_NONE);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001214 set_priv_filter(usbdev);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001215 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001216}
1217
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001218static int deauthenticate(struct usbnet *usbdev)
1219{
1220 int ret;
1221
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03001222 ret = disassociate(usbdev, true);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001223 set_default_iw_params(usbdev);
1224 return ret;
1225}
1226
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001227static int set_channel(struct usbnet *usbdev, int channel)
1228{
1229 struct ndis_80211_conf config;
1230 unsigned int dsconfig;
1231 int len, ret;
1232
Joe Perches60b86752010-02-17 10:30:23 +00001233 netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001234
1235 /* this OID is valid only when not associated */
1236 if (is_associated(usbdev))
1237 return 0;
1238
1239 dsconfig = ieee80211_dsss_chan_to_freq(channel) * 1000;
1240
1241 len = sizeof(config);
1242 ret = rndis_query_oid(usbdev, OID_802_11_CONFIGURATION, &config, &len);
1243 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001244 netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1245 __func__);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001246 return ret;
1247 }
1248
1249 config.ds_config = cpu_to_le32(dsconfig);
1250 ret = rndis_set_oid(usbdev, OID_802_11_CONFIGURATION, &config,
1251 sizeof(config));
1252
Joe Perches60b86752010-02-17 10:30:23 +00001253 netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001254
1255 return ret;
1256}
1257
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001258/* index must be 0 - N, as per NDIS */
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001259static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1260 int index)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001261{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001262 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +02001263 struct ndis_80211_wep_key ndis_key;
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001264 u32 cipher;
1265 int ret;
1266
Joe Perches60b86752010-02-17 10:30:23 +00001267 netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1268 __func__, index, key_len);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001269
Jussi Kivilinna40ba60d2009-08-20 21:00:34 +03001270 if ((key_len != 5 && key_len != 13) || index < 0 || index > 3)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001271 return -EINVAL;
1272
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001273 if (key_len == 5)
1274 cipher = WLAN_CIPHER_SUITE_WEP40;
1275 else
1276 cipher = WLAN_CIPHER_SUITE_WEP104;
1277
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001278 memset(&ndis_key, 0, sizeof(ndis_key));
1279
Jussi Kivilinna461b3f22008-03-08 01:23:25 +02001280 ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1281 ndis_key.length = cpu_to_le32(key_len);
1282 ndis_key.index = cpu_to_le32(index);
1283 memcpy(&ndis_key.material, key, key_len);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001284
1285 if (index == priv->encr_tx_key_index) {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001286 ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001287 ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1288 RNDIS_WLAN_ALG_NONE);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001289 if (ret)
Joe Perches60b86752010-02-17 10:30:23 +00001290 netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1291 ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001292 }
1293
1294 ret = rndis_set_oid(usbdev, OID_802_11_ADD_WEP, &ndis_key,
1295 sizeof(ndis_key));
1296 if (ret != 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001297 netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1298 index + 1, ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001299 return ret;
1300 }
1301
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001302 priv->encr_keys[index].len = key_len;
1303 priv->encr_keys[index].cipher = cipher;
1304 memcpy(&priv->encr_keys[index].material, key, key_len);
1305 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001306
1307 return 0;
1308}
1309
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001310static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001311 int index, const u8 *addr, const u8 *rx_seq,
Jussi Kivilinna53d27ea2009-09-01 15:33:06 +03001312 int seq_len, u32 cipher, __le32 flags)
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001313{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001314 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001315 struct ndis_80211_key ndis_key;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001316 bool is_addr_ok;
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001317 int ret;
1318
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001319 if (index < 0 || index >= 4) {
Joe Perches60b86752010-02-17 10:30:23 +00001320 netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1321 __func__, index);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001322 return -EINVAL;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001323 }
1324 if (key_len > sizeof(ndis_key.material) || key_len < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001325 netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1326 __func__, key_len);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001327 return -EINVAL;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001328 }
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001329 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1330 if (!rx_seq || seq_len <= 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001331 netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1332 __func__);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001333 return -EINVAL;
1334 }
1335 if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
Joe Perches60b86752010-02-17 10:30:23 +00001336 netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001337 return -EINVAL;
1338 }
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001339 }
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001340
Jussi Kivilinna7b1fff92009-08-28 12:59:15 +03001341 is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1342 !is_broadcast_ether_addr(addr);
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001343 if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
Joe Perches60b86752010-02-17 10:30:23 +00001344 netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1345 __func__, addr);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001346 return -EINVAL;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001347 }
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001348
Joe Perches60b86752010-02-17 10:30:23 +00001349 netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1350 __func__, index,
1351 !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1352 !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1353 !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001354
1355 memset(&ndis_key, 0, sizeof(ndis_key));
1356
1357 ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1358 sizeof(ndis_key.material) + key_len);
1359 ndis_key.length = cpu_to_le32(key_len);
1360 ndis_key.index = cpu_to_le32(index) | flags;
1361
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001362 if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001363 /* wpa_supplicant gives us the Michael MIC RX/TX keys in
1364 * different order than NDIS spec, so swap the order here. */
1365 memcpy(ndis_key.material, key, 16);
1366 memcpy(ndis_key.material + 16, key + 24, 8);
1367 memcpy(ndis_key.material + 24, key + 16, 8);
1368 } else
1369 memcpy(ndis_key.material, key, key_len);
1370
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001371 if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001372 memcpy(ndis_key.rsc, rx_seq, seq_len);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001373
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001374 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001375 /* pairwise key */
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001376 memcpy(ndis_key.bssid, addr, ETH_ALEN);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001377 } else {
1378 /* group key */
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001379 if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001380 memset(ndis_key.bssid, 0xff, ETH_ALEN);
1381 else
1382 get_bssid(usbdev, ndis_key.bssid);
1383 }
1384
1385 ret = rndis_set_oid(usbdev, OID_802_11_ADD_KEY, &ndis_key,
1386 le32_to_cpu(ndis_key.size));
Joe Perches60b86752010-02-17 10:30:23 +00001387 netdev_dbg(usbdev->net, "%s(): OID_802_11_ADD_KEY -> %08X\n",
1388 __func__, ret);
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001389 if (ret != 0)
1390 return ret;
1391
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001392 memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1393 priv->encr_keys[index].len = key_len;
1394 priv->encr_keys[index].cipher = cipher;
1395 memcpy(&priv->encr_keys[index].material, key, key_len);
1396 if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1397 memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1398 else
1399 memset(&priv->encr_keys[index].bssid, 0xff, ETH_ALEN);
Jussi Kivilinna98391782009-05-22 17:40:34 +03001400
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001401 if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
Jussi Kivilinnab145ee02009-05-22 17:40:27 +03001402 priv->encr_tx_key_index = index;
1403
1404 return 0;
1405}
1406
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001407static int restore_key(struct usbnet *usbdev, int key_idx)
1408{
1409 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1410 struct rndis_wlan_encr_key key;
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001411
1412 if (is_wpa_key(priv, key_idx))
1413 return 0;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001414
1415 key = priv->encr_keys[key_idx];
1416
Joe Perches60b86752010-02-17 10:30:23 +00001417 netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001418
1419 if (key.len == 0)
1420 return 0;
1421
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001422 return add_wep_key(usbdev, key.material, key.len, key_idx);
1423}
1424
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001425static void restore_keys(struct usbnet *usbdev)
1426{
1427 int i;
1428
1429 for (i = 0; i < 4; i++)
1430 restore_key(usbdev, i);
1431}
1432
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001433static void clear_key(struct rndis_wlan_private *priv, int idx)
1434{
1435 memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1436}
1437
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001438/* remove_key is for both wep and wpa */
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03001439static int remove_key(struct usbnet *usbdev, int index, const u8 *bssid)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001440{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001441 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna1e41e1d2008-03-08 01:23:17 +02001442 struct ndis_80211_remove_key remove_key;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001443 __le32 keyindex;
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001444 bool is_wpa;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001445 int ret;
1446
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001447 if (priv->encr_keys[index].len == 0)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001448 return 0;
1449
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001450 is_wpa = is_wpa_key(priv, index);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001451
Joe Perches60b86752010-02-17 10:30:23 +00001452 netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1453 __func__, index, is_wpa ? "wpa" : "wep",
1454 priv->encr_keys[index].len);
Jussi Kivilinnab7cfc5b2009-07-30 19:42:08 +03001455
1456 clear_key(priv, index);
1457
1458 if (is_wpa) {
Jussi Kivilinna461b3f22008-03-08 01:23:25 +02001459 remove_key.size = cpu_to_le32(sizeof(remove_key));
1460 remove_key.index = cpu_to_le32(index);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001461 if (bssid) {
1462 /* pairwise key */
Jussi Kivilinna7b1fff92009-08-28 12:59:15 +03001463 if (!is_broadcast_ether_addr(bssid))
Jussi Kivilinnab4703a22009-05-22 17:40:20 +03001464 remove_key.index |=
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001465 NDIS_80211_ADDKEY_PAIRWISE_KEY;
Jussi Kivilinna461b3f22008-03-08 01:23:25 +02001466 memcpy(remove_key.bssid, bssid,
1467 sizeof(remove_key.bssid));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001468 } else
Jussi Kivilinna461b3f22008-03-08 01:23:25 +02001469 memset(remove_key.bssid, 0xff,
1470 sizeof(remove_key.bssid));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001471
1472 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_KEY, &remove_key,
1473 sizeof(remove_key));
1474 if (ret != 0)
1475 return ret;
1476 } else {
1477 keyindex = cpu_to_le32(index);
1478 ret = rndis_set_oid(usbdev, OID_802_11_REMOVE_WEP, &keyindex,
1479 sizeof(keyindex));
1480 if (ret != 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001481 netdev_warn(usbdev->net,
1482 "removing encryption key %d failed (%08X)\n",
1483 index, ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001484 return ret;
1485 }
1486 }
1487
1488 /* if it is transmit key, disable encryption */
1489 if (index == priv->encr_tx_key_index)
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001490 set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001491
1492 return 0;
1493}
1494
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001495static void set_multicast_list(struct usbnet *usbdev)
1496{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001497 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001498 struct dev_mc_list *mclist;
Jiri Pirko12c34002010-03-04 03:32:16 -08001499 __le32 filter, basefilter;
1500 int ret;
1501 char *mc_addrs = NULL;
1502 int mc_count;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001503
Jiri Pirko12c34002010-03-04 03:32:16 -08001504 basefilter = filter = RNDIS_PACKET_TYPE_DIRECTED |
1505 RNDIS_PACKET_TYPE_BROADCAST;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001506
1507 if (usbdev->net->flags & IFF_PROMISC) {
1508 filter |= RNDIS_PACKET_TYPE_PROMISCUOUS |
1509 RNDIS_PACKET_TYPE_ALL_LOCAL;
Jiri Pirko12c34002010-03-04 03:32:16 -08001510 } else if (usbdev->net->flags & IFF_ALLMULTI) {
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001511 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
Jiri Pirko12c34002010-03-04 03:32:16 -08001512 }
1513
1514 if (filter != basefilter)
1515 goto set_filter;
1516
1517 /*
1518 * mc_list should be accessed holding the lock, so copy addresses to
1519 * local buffer first.
1520 */
1521 netif_addr_lock_bh(usbdev->net);
1522 mc_count = netdev_mc_count(usbdev->net);
1523 if (mc_count > priv->multicast_size) {
1524 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1525 } else if (mc_count) {
1526 int i = 0;
1527
1528 mc_addrs = kmalloc(mc_count * ETH_ALEN, GFP_ATOMIC);
1529 if (!mc_addrs) {
Joe Perches60b86752010-02-17 10:30:23 +00001530 netdev_warn(usbdev->net,
1531 "couldn't alloc %d bytes of memory\n",
Jiri Pirko12c34002010-03-04 03:32:16 -08001532 mc_count * ETH_ALEN);
Jiri Pirko655ffee2010-02-27 07:35:45 +00001533 netif_addr_unlock_bh(usbdev->net);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001534 return;
1535 }
1536
Jiri Pirko12c34002010-03-04 03:32:16 -08001537 netdev_for_each_mc_addr(mclist, usbdev->net)
1538 memcpy(mc_addrs + i++ * ETH_ALEN,
1539 mclist->dmi_addr, ETH_ALEN);
1540 }
1541 netif_addr_unlock_bh(usbdev->net);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001542
Jiri Pirko12c34002010-03-04 03:32:16 -08001543 if (filter != basefilter)
1544 goto set_filter;
1545
1546 if (mc_count) {
1547 ret = rndis_set_oid(usbdev, OID_802_3_MULTICAST_LIST, mc_addrs,
1548 mc_count * ETH_ALEN);
1549 kfree(mc_addrs);
1550 if (ret == 0)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001551 filter |= RNDIS_PACKET_TYPE_MULTICAST;
1552 else
1553 filter |= RNDIS_PACKET_TYPE_ALL_MULTICAST;
1554
Joe Perches60b86752010-02-17 10:30:23 +00001555 netdev_dbg(usbdev->net, "OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
Jiri Pirko12c34002010-03-04 03:32:16 -08001556 mc_count, priv->multicast_size, ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001557 }
1558
Jiri Pirko12c34002010-03-04 03:32:16 -08001559set_filter:
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001560 ret = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
1561 sizeof(filter));
1562 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001563 netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1564 le32_to_cpu(filter));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001565 }
1566
Joe Perches60b86752010-02-17 10:30:23 +00001567 netdev_dbg(usbdev->net, "OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1568 le32_to_cpu(filter), ret);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001569}
1570
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02001571/*
John W. Linville964c1d42009-03-26 23:40:01 +02001572 * cfg80211 ops
1573 */
Johannes Berge36d56b2009-06-09 21:04:43 +02001574static int rndis_change_virtual_intf(struct wiphy *wiphy,
1575 struct net_device *dev,
John W. Linville964c1d42009-03-26 23:40:01 +02001576 enum nl80211_iftype type, u32 *flags,
1577 struct vif_params *params)
1578{
Jussi Kivilinna16139172009-08-28 12:59:21 +03001579 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1580 struct usbnet *usbdev = priv->usbdev;
John W. Linville964c1d42009-03-26 23:40:01 +02001581 int mode;
1582
John W. Linville964c1d42009-03-26 23:40:01 +02001583 switch (type) {
1584 case NL80211_IFTYPE_ADHOC:
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001585 mode = NDIS_80211_INFRA_ADHOC;
John W. Linville964c1d42009-03-26 23:40:01 +02001586 break;
1587 case NL80211_IFTYPE_STATION:
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03001588 mode = NDIS_80211_INFRA_INFRA;
John W. Linville964c1d42009-03-26 23:40:01 +02001589 break;
1590 default:
1591 return -EINVAL;
1592 }
1593
Jussi Kivilinna16139172009-08-28 12:59:21 +03001594 priv->wdev.iftype = type;
1595
John W. Linville964c1d42009-03-26 23:40:01 +02001596 return set_infra_mode(usbdev, mode);
1597}
1598
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03001599static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1600{
1601 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1602 struct usbnet *usbdev = priv->usbdev;
1603 int err;
1604
1605 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1606 err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1607 if (err < 0)
1608 return err;
1609 }
1610
1611 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1612 err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1613 if (err < 0)
1614 return err;
1615 }
1616
1617 return 0;
1618}
1619
Jussi Kivilinna222ec502009-06-16 17:17:32 +03001620static int rndis_set_tx_power(struct wiphy *wiphy, enum tx_power_setting type,
1621 int dbm)
1622{
1623 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1624 struct usbnet *usbdev = priv->usbdev;
1625
Joe Perches60b86752010-02-17 10:30:23 +00001626 netdev_dbg(usbdev->net, "%s(): type:0x%x dbm:%i\n",
1627 __func__, type, dbm);
Jussi Kivilinna222ec502009-06-16 17:17:32 +03001628
1629 /* Device doesn't support changing txpower after initialization, only
1630 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1631 * currently used.
1632 */
1633 if (type == TX_POWER_AUTOMATIC || dbm == get_bcm4320_power_dbm(priv)) {
1634 if (!priv->radio_on)
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03001635 disassociate(usbdev, true); /* turn on radio */
Jussi Kivilinna222ec502009-06-16 17:17:32 +03001636
1637 return 0;
1638 }
1639
1640 return -ENOTSUPP;
1641}
1642
Jussi Kivilinna222ec502009-06-16 17:17:32 +03001643static int rndis_get_tx_power(struct wiphy *wiphy, int *dbm)
1644{
1645 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1646 struct usbnet *usbdev = priv->usbdev;
1647
1648 *dbm = get_bcm4320_power_dbm(priv);
1649
Joe Perches60b86752010-02-17 10:30:23 +00001650 netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
Jussi Kivilinna222ec502009-06-16 17:17:32 +03001651
1652 return 0;
1653}
1654
Jussi Kivilinnab1d25a62009-08-28 12:59:05 +03001655#define SCAN_DELAY_JIFFIES (6 * HZ)
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001656static int rndis_scan(struct wiphy *wiphy, struct net_device *dev,
1657 struct cfg80211_scan_request *request)
1658{
1659 struct usbnet *usbdev = netdev_priv(dev);
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001660 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001661 int ret;
1662 __le32 tmp;
1663
Joe Perches60b86752010-02-17 10:30:23 +00001664 netdev_dbg(usbdev->net, "cfg80211.scan\n");
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001665
Jussi Kivilinnadb0dd392009-08-28 12:58:55 +03001666 /* Get current bssid list from device before new scan, as new scan
1667 * clears internal bssid list.
1668 */
1669 rndis_check_bssid_list(usbdev);
1670
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001671 if (!request)
1672 return -EINVAL;
1673
1674 if (priv->scan_request && priv->scan_request != request)
1675 return -EBUSY;
1676
1677 priv->scan_request = request;
1678
1679 tmp = cpu_to_le32(1);
1680 ret = rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
1681 sizeof(tmp));
1682 if (ret == 0) {
1683 /* Wait before retrieving scan results from device */
1684 queue_delayed_work(priv->workqueue, &priv->scan_work,
1685 SCAN_DELAY_JIFFIES);
1686 }
1687
1688 return ret;
1689}
1690
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001691static struct cfg80211_bss *rndis_bss_info_update(struct usbnet *usbdev,
1692 struct ndis_80211_bssid_ex *bssid)
1693{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001694 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001695 struct ieee80211_channel *channel;
1696 s32 signal;
1697 u64 timestamp;
1698 u16 capability;
1699 u16 beacon_interval;
1700 struct ndis_80211_fixed_ies *fixed;
1701 int ie_len, bssid_len;
1702 u8 *ie;
1703
Joe Perches60b86752010-02-17 10:30:23 +00001704 netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM]\n",
1705 bssid->ssid.essid, bssid->mac);
Jussi Kivilinna5fd8f252009-08-28 12:59:00 +03001706
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001707 /* parse bssid structure */
1708 bssid_len = le32_to_cpu(bssid->length);
1709
1710 if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1711 sizeof(struct ndis_80211_fixed_ies))
1712 return NULL;
1713
1714 fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1715
1716 ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1717 ie_len = min(bssid_len - (int)sizeof(*bssid),
1718 (int)le32_to_cpu(bssid->ie_length));
1719 ie_len -= sizeof(struct ndis_80211_fixed_ies);
1720 if (ie_len < 0)
1721 return NULL;
1722
1723 /* extract data for cfg80211_inform_bss */
1724 channel = ieee80211_get_channel(priv->wdev.wiphy,
1725 KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1726 if (!channel)
1727 return NULL;
1728
1729 signal = level_to_qual(le32_to_cpu(bssid->rssi));
1730 timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1731 capability = le16_to_cpu(fixed->capabilities);
1732 beacon_interval = le16_to_cpu(fixed->beacon_interval);
1733
1734 return cfg80211_inform_bss(priv->wdev.wiphy, channel, bssid->mac,
1735 timestamp, capability, beacon_interval, ie, ie_len, signal,
1736 GFP_KERNEL);
1737}
1738
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001739static int rndis_check_bssid_list(struct usbnet *usbdev)
1740{
1741 void *buf = NULL;
1742 struct ndis_80211_bssid_list_ex *bssid_list;
1743 struct ndis_80211_bssid_ex *bssid;
1744 int ret = -EINVAL, len, count, bssid_len;
Jussi Kivilinna5fd8f252009-08-28 12:59:00 +03001745 bool resized = false;
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001746
Joe Perches60b86752010-02-17 10:30:23 +00001747 netdev_dbg(usbdev->net, "check_bssid_list\n");
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001748
1749 len = CONTROL_BUFFER_SIZE;
Jussi Kivilinna5fd8f252009-08-28 12:59:00 +03001750resize_buf:
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001751 buf = kmalloc(len, GFP_KERNEL);
1752 if (!buf) {
1753 ret = -ENOMEM;
1754 goto out;
1755 }
1756
1757 ret = rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
1758 if (ret != 0)
1759 goto out;
1760
Jussi Kivilinna5fd8f252009-08-28 12:59:00 +03001761 if (!resized && len > CONTROL_BUFFER_SIZE) {
1762 resized = true;
1763 kfree(buf);
1764 goto resize_buf;
1765 }
1766
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001767 bssid_list = buf;
1768 bssid = bssid_list->bssid;
1769 bssid_len = le32_to_cpu(bssid->length);
1770 count = le32_to_cpu(bssid_list->num_items);
Joe Perches60b86752010-02-17 10:30:23 +00001771 netdev_dbg(usbdev->net, "check_bssid_list: %d BSSIDs found (buflen: %d)\n",
1772 count, len);
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001773
1774 while (count && ((void *)bssid + bssid_len) <= (buf + len)) {
1775 rndis_bss_info_update(usbdev, bssid);
1776
1777 bssid = (void *)bssid + bssid_len;
1778 bssid_len = le32_to_cpu(bssid->length);
1779 count--;
1780 }
1781
1782out:
1783 kfree(buf);
1784 return ret;
1785}
1786
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001787static void rndis_get_scan_results(struct work_struct *work)
1788{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03001789 struct rndis_wlan_private *priv =
1790 container_of(work, struct rndis_wlan_private, scan_work.work);
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001791 struct usbnet *usbdev = priv->usbdev;
1792 int ret;
1793
Joe Perches60b86752010-02-17 10:30:23 +00001794 netdev_dbg(usbdev->net, "get_scan_results\n");
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001795
Jussi Kivilinna005ba2f2009-07-30 19:41:31 +03001796 if (!priv->scan_request)
1797 return;
1798
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02001799 ret = rndis_check_bssid_list(usbdev);
1800
1801 cfg80211_scan_done(priv->scan_request, ret < 0);
1802
1803 priv->scan_request = NULL;
1804}
1805
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001806static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
1807 struct cfg80211_connect_params *sme)
1808{
1809 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1810 struct usbnet *usbdev = priv->usbdev;
1811 struct ieee80211_channel *channel = sme->channel;
1812 struct ndis_80211_ssid ssid;
1813 int pairwise = RNDIS_WLAN_ALG_NONE;
1814 int groupwise = RNDIS_WLAN_ALG_NONE;
1815 int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
1816 int length, i, ret, chan = -1;
1817
1818 if (channel)
1819 chan = ieee80211_frequency_to_channel(channel->center_freq);
1820
1821 groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
1822 for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
1823 pairwise |=
1824 rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
1825
1826 if (sme->crypto.n_ciphers_pairwise > 0 &&
1827 pairwise == RNDIS_WLAN_ALG_NONE) {
Joe Perches60b86752010-02-17 10:30:23 +00001828 netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001829 return -ENOTSUPP;
1830 }
1831
1832 for (i = 0; i < sme->crypto.n_akm_suites; i++)
1833 keymgmt |=
1834 rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
1835
1836 if (sme->crypto.n_akm_suites > 0 &&
1837 keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
Joe Perches60b86752010-02-17 10:30:23 +00001838 netdev_err(usbdev->net, "Invalid keymgmt\n");
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001839 return -ENOTSUPP;
1840 }
1841
Joe Perches60b86752010-02-17 10:30:23 +00001842 netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
1843 sme->ssid, sme->bssid, chan,
1844 sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
1845 groupwise, pairwise, keymgmt);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001846
1847 if (is_associated(usbdev))
1848 disassociate(usbdev, false);
1849
1850 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1851 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001852 netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
1853 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001854 goto err_turn_radio_on;
1855 }
1856
1857 ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
1858 keymgmt);
1859 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001860 netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
1861 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001862 goto err_turn_radio_on;
1863 }
1864
1865 set_priv_filter(usbdev);
1866
1867 ret = set_encr_mode(usbdev, pairwise, groupwise);
1868 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001869 netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
1870 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001871 goto err_turn_radio_on;
1872 }
1873
1874 if (channel) {
1875 ret = set_channel(usbdev, chan);
1876 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001877 netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
1878 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001879 goto err_turn_radio_on;
1880 }
1881 }
1882
1883 if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
1884 priv->encr_tx_key_index = sme->key_idx;
1885 ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
1886 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001887 netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
1888 ret, sme->key_len, sme->key_idx);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001889 goto err_turn_radio_on;
1890 }
1891 }
1892
1893 if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
1894 !is_broadcast_ether_addr(sme->bssid)) {
1895 ret = set_bssid(usbdev, sme->bssid);
1896 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001897 netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
1898 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001899 goto err_turn_radio_on;
1900 }
1901 } else
1902 clear_bssid(usbdev);
1903
1904 length = sme->ssid_len;
1905 if (length > NDIS_802_11_LENGTH_SSID)
1906 length = NDIS_802_11_LENGTH_SSID;
1907
1908 memset(&ssid, 0, sizeof(ssid));
1909 ssid.length = cpu_to_le32(length);
1910 memcpy(ssid.essid, sme->ssid, length);
1911
1912 /* Pause and purge rx queue, so we don't pass packets before
1913 * 'media connect'-indication.
1914 */
1915 usbnet_pause_rx(usbdev);
1916 usbnet_purge_paused_rxq(usbdev);
1917
1918 ret = set_essid(usbdev, &ssid);
1919 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +00001920 netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001921 return ret;
1922
1923err_turn_radio_on:
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03001924 disassociate(usbdev, true);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001925
1926 return ret;
1927}
1928
1929static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
1930 u16 reason_code)
1931{
1932 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1933 struct usbnet *usbdev = priv->usbdev;
1934
Joe Perches60b86752010-02-17 10:30:23 +00001935 netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001936
1937 priv->connected = false;
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03001938 memset(priv->bssid, 0, ETH_ALEN);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001939
1940 return deauthenticate(usbdev);
1941}
1942
1943static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1944 struct cfg80211_ibss_params *params)
1945{
1946 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1947 struct usbnet *usbdev = priv->usbdev;
1948 struct ieee80211_channel *channel = params->channel;
1949 struct ndis_80211_ssid ssid;
1950 enum nl80211_auth_type auth_type;
1951 int ret, alg, length, chan = -1;
1952
1953 if (channel)
1954 chan = ieee80211_frequency_to_channel(channel->center_freq);
1955
1956 /* TODO: How to handle ad-hoc encryption?
1957 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
1958 * pre-shared for encryption (open/shared/wpa), is key set before
1959 * join_ibss? Which auth_type to use (not in params)? What about WPA?
1960 */
1961 if (params->privacy) {
1962 auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1963 alg = RNDIS_WLAN_ALG_WEP;
1964 } else {
1965 auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1966 alg = RNDIS_WLAN_ALG_NONE;
1967 }
1968
Joe Perches60b86752010-02-17 10:30:23 +00001969 netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
1970 params->ssid, params->bssid, chan, params->privacy);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001971
1972 if (is_associated(usbdev))
1973 disassociate(usbdev, false);
1974
1975 ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
1976 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001977 netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
1978 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001979 goto err_turn_radio_on;
1980 }
1981
1982 ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
1983 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001984 netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
1985 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001986 goto err_turn_radio_on;
1987 }
1988
1989 set_priv_filter(usbdev);
1990
1991 ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
1992 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001993 netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
1994 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03001995 goto err_turn_radio_on;
1996 }
1997
1998 if (channel) {
1999 ret = set_channel(usbdev, chan);
2000 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00002001 netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2002 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002003 goto err_turn_radio_on;
2004 }
2005 }
2006
2007 if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2008 !is_broadcast_ether_addr(params->bssid)) {
2009 ret = set_bssid(usbdev, params->bssid);
2010 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00002011 netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2012 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002013 goto err_turn_radio_on;
2014 }
2015 } else
2016 clear_bssid(usbdev);
2017
2018 length = params->ssid_len;
2019 if (length > NDIS_802_11_LENGTH_SSID)
2020 length = NDIS_802_11_LENGTH_SSID;
2021
2022 memset(&ssid, 0, sizeof(ssid));
2023 ssid.length = cpu_to_le32(length);
2024 memcpy(ssid.essid, params->ssid, length);
2025
2026 /* Don't need to pause rx queue for ad-hoc. */
2027 usbnet_purge_paused_rxq(usbdev);
2028 usbnet_resume_rx(usbdev);
2029
2030 ret = set_essid(usbdev, &ssid);
2031 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +00002032 netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2033 ret);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002034 return ret;
2035
2036err_turn_radio_on:
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03002037 disassociate(usbdev, true);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002038
2039 return ret;
2040}
2041
2042static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2043{
2044 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2045 struct usbnet *usbdev = priv->usbdev;
2046
Joe Perches60b86752010-02-17 10:30:23 +00002047 netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002048
2049 priv->connected = false;
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03002050 memset(priv->bssid, 0, ETH_ALEN);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002051
2052 return deauthenticate(usbdev);
2053}
2054
Jussi Kivilinna5554adb2009-08-28 13:27:53 +03002055static int rndis_set_channel(struct wiphy *wiphy,
2056 struct ieee80211_channel *chan, enum nl80211_channel_type channel_type)
2057{
2058 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2059 struct usbnet *usbdev = priv->usbdev;
2060
2061 return set_channel(usbdev,
2062 ieee80211_frequency_to_channel(chan->center_freq));
2063}
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02002064
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002065static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2066 u8 key_index, const u8 *mac_addr,
2067 struct key_params *params)
2068{
2069 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2070 struct usbnet *usbdev = priv->usbdev;
Jussi Kivilinna53d27ea2009-09-01 15:33:06 +03002071 __le32 flags;
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002072
Joe Perches60b86752010-02-17 10:30:23 +00002073 netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2074 __func__, key_index, mac_addr, params->cipher);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002075
2076 switch (params->cipher) {
2077 case WLAN_CIPHER_SUITE_WEP40:
2078 case WLAN_CIPHER_SUITE_WEP104:
2079 return add_wep_key(usbdev, params->key, params->key_len,
2080 key_index);
2081 case WLAN_CIPHER_SUITE_TKIP:
2082 case WLAN_CIPHER_SUITE_CCMP:
2083 flags = 0;
2084
2085 if (params->seq && params->seq_len > 0)
2086 flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2087 if (mac_addr)
2088 flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2089 NDIS_80211_ADDKEY_TRANSMIT_KEY;
2090
2091 return add_wpa_key(usbdev, params->key, params->key_len,
2092 key_index, mac_addr, params->seq,
2093 params->seq_len, params->cipher, flags);
2094 default:
Joe Perches60b86752010-02-17 10:30:23 +00002095 netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2096 __func__, params->cipher);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002097 return -ENOTSUPP;
2098 }
2099}
2100
2101static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2102 u8 key_index, const u8 *mac_addr)
2103{
2104 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2105 struct usbnet *usbdev = priv->usbdev;
2106
Joe Perches60b86752010-02-17 10:30:23 +00002107 netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002108
2109 return remove_key(usbdev, key_index, mac_addr);
2110}
2111
2112static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2113 u8 key_index)
2114{
2115 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2116 struct usbnet *usbdev = priv->usbdev;
2117 struct rndis_wlan_encr_key key;
2118
Joe Perches60b86752010-02-17 10:30:23 +00002119 netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
Jussi Kivilinna84bf8402009-08-28 13:27:58 +03002120
2121 priv->encr_tx_key_index = key_index;
2122
2123 key = priv->encr_keys[key_index];
2124
2125 return add_wep_key(usbdev, key.material, key.len, key_index);
2126}
2127
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03002128static void rndis_fill_station_info(struct usbnet *usbdev,
2129 struct station_info *sinfo)
2130{
2131 __le32 linkspeed, rssi;
2132 int ret, len;
2133
2134 memset(sinfo, 0, sizeof(*sinfo));
2135
2136 len = sizeof(linkspeed);
2137 ret = rndis_query_oid(usbdev, OID_GEN_LINK_SPEED, &linkspeed, &len);
2138 if (ret == 0) {
2139 sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2140 sinfo->filled |= STATION_INFO_TX_BITRATE;
2141 }
2142
2143 len = sizeof(rssi);
2144 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
2145 if (ret == 0) {
2146 sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2147 sinfo->filled |= STATION_INFO_SIGNAL;
2148 }
2149}
2150
2151static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2152 u8 *mac, struct station_info *sinfo)
2153{
2154 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2155 struct usbnet *usbdev = priv->usbdev;
2156
2157 if (compare_ether_addr(priv->bssid, mac))
2158 return -ENOENT;
2159
2160 rndis_fill_station_info(usbdev, sinfo);
2161
2162 return 0;
2163}
2164
Jussi Kivilinnad695df92009-08-28 13:28:09 +03002165static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2166 int idx, u8 *mac, struct station_info *sinfo)
2167{
2168 struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2169 struct usbnet *usbdev = priv->usbdev;
2170
2171 if (idx != 0)
2172 return -ENOENT;
2173
2174 memcpy(mac, priv->bssid, ETH_ALEN);
2175
2176 rndis_fill_station_info(usbdev, sinfo);
2177
2178 return 0;
2179}
2180
Jussi Kivilinnac5c4fe92009-09-01 15:33:00 +03002181/*
2182 * workers, indication handlers, device poller
2183 */
Jussi Kivilinna0848e6c2009-08-28 12:59:10 +03002184static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002185{
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002186 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Scott Ashcroft43646232008-05-27 00:06:15 +03002187 struct ndis_80211_assoc_info *info;
Jussi Kivilinna0848e6c2009-08-28 12:59:10 +03002188 u8 assoc_buf[sizeof(*info) + IW_CUSTOM_MAX + 32];
2189 u8 bssid[ETH_ALEN];
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002190 int resp_ie_len, req_ie_len;
2191 u8 *req_ie, *resp_ie;
Scott Ashcroft43646232008-05-27 00:06:15 +03002192 int ret, offset;
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002193 bool roamed = false;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002194
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002195 if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2196 /* received media connect indication while connected, either
2197 * device reassociated with same AP or roamed to new. */
2198 roamed = true;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002199 }
2200
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002201 req_ie_len = 0;
2202 resp_ie_len = 0;
2203 req_ie = NULL;
2204 resp_ie = NULL;
2205
2206 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2207 memset(assoc_buf, 0, sizeof(assoc_buf));
2208 info = (void *)assoc_buf;
2209
2210 /* Get association info IEs from device and send them back to
2211 * userspace. */
2212 ret = get_association_info(usbdev, info, sizeof(assoc_buf));
2213 if (!ret) {
2214 req_ie_len = le32_to_cpu(info->req_ie_length);
2215 if (req_ie_len > 0) {
2216 offset = le32_to_cpu(info->offset_req_ies);
2217 req_ie = (u8 *)info + offset;
2218 }
2219
2220 resp_ie_len = le32_to_cpu(info->resp_ie_length);
2221 if (resp_ie_len > 0) {
2222 offset = le32_to_cpu(info->offset_resp_ies);
2223 resp_ie = (u8 *)info + offset;
2224 }
2225 }
2226 } else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2227 return;
2228
Jussi Kivilinna0848e6c2009-08-28 12:59:10 +03002229 ret = get_bssid(usbdev, bssid);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002230 if (ret < 0)
2231 memset(bssid, 0, sizeof(bssid));
2232
Joe Perches60b86752010-02-17 10:30:23 +00002233 netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2234 bssid, roamed ? " roamed" : "");
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002235
2236 /* Internal bss list in device always contains at least the currently
2237 * connected bss and we can get it to cfg80211 with
2238 * rndis_check_bssid_list().
2239 * NOTE: This is true for Broadcom chip, but not mentioned in RNDIS
2240 * spec.
2241 */
2242 rndis_check_bssid_list(usbdev);
2243
2244 if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2245 if (!roamed)
2246 cfg80211_connect_result(usbdev->net, bssid, req_ie,
2247 req_ie_len, resp_ie,
2248 resp_ie_len, 0, GFP_KERNEL);
2249 else
2250 cfg80211_roamed(usbdev->net, bssid, req_ie, req_ie_len,
2251 resp_ie, resp_ie_len, GFP_KERNEL);
2252 } else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2253 cfg80211_ibss_joined(usbdev->net, bssid, GFP_KERNEL);
2254
2255 priv->connected = true;
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03002256 memcpy(priv->bssid, bssid, ETH_ALEN);
Jussi Kivilinna6010ce02008-06-02 18:35:21 +03002257
Jussi Kivilinna0848e6c2009-08-28 12:59:10 +03002258 usbnet_resume_rx(usbdev);
Jussi Kivilinna5c523232009-08-28 13:27:47 +03002259 netif_carrier_on(usbdev->net);
Jussi Kivilinna0848e6c2009-08-28 12:59:10 +03002260}
2261
2262static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2263{
2264 union iwreq_data evt;
2265
2266 netif_carrier_off(usbdev->net);
2267
2268 evt.data.flags = 0;
2269 evt.data.length = 0;
2270 memset(evt.ap_addr.sa_data, 0, ETH_ALEN);
2271 wireless_send_event(usbdev->net, SIOCGIWAP, &evt, NULL);
2272}
2273
2274static void rndis_wlan_worker(struct work_struct *work)
2275{
2276 struct rndis_wlan_private *priv =
2277 container_of(work, struct rndis_wlan_private, work);
2278 struct usbnet *usbdev = priv->usbdev;
2279
2280 if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2281 rndis_wlan_do_link_up_work(usbdev);
2282
2283 if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2284 rndis_wlan_do_link_down_work(usbdev);
2285
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002286 if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2287 set_multicast_list(usbdev);
2288}
2289
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002290static void rndis_wlan_set_multicast_list(struct net_device *dev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002291{
Wang Chen524ad0a2008-11-12 23:39:10 -08002292 struct usbnet *usbdev = netdev_priv(dev);
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002293 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002294
Jussi Kivilinnaa67edb92008-06-02 18:35:36 +03002295 if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2296 return;
2297
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002298 set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2299 queue_work(priv->workqueue, &priv->work);
2300}
2301
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002302static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2303 struct ndis_80211_status_indication *indication,
2304 int len)
2305{
2306 u8 *buf;
2307 const char *type;
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002308 int flags, buflen, key_id;
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002309 bool pairwise_error, group_error;
2310 struct ndis_80211_auth_request *auth_req;
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002311 enum nl80211_key_type key_type;
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002312
2313 /* must have at least one array entry */
2314 if (len < offsetof(struct ndis_80211_status_indication, u) +
2315 sizeof(struct ndis_80211_auth_request)) {
Joe Perches60b86752010-02-17 10:30:23 +00002316 netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2317 len);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002318 return;
2319 }
2320
2321 buf = (void *)&indication->u.auth_request[0];
2322 buflen = len - offsetof(struct ndis_80211_status_indication, u);
2323
2324 while (buflen >= sizeof(*auth_req)) {
2325 auth_req = (void *)buf;
2326 type = "unknown";
2327 flags = le32_to_cpu(auth_req->flags);
2328 pairwise_error = false;
2329 group_error = false;
2330
2331 if (flags & 0x1)
2332 type = "reauth request";
2333 if (flags & 0x2)
2334 type = "key update request";
2335 if (flags & 0x6) {
2336 pairwise_error = true;
2337 type = "pairwise_error";
2338 }
2339 if (flags & 0xe) {
2340 group_error = true;
2341 type = "group_error";
2342 }
2343
Joe Perches60b86752010-02-17 10:30:23 +00002344 netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2345 type, le32_to_cpu(auth_req->flags));
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002346
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002347 if (pairwise_error) {
2348 key_type = NL80211_KEYTYPE_PAIRWISE;
2349 key_id = -1;
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002350
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002351 cfg80211_michael_mic_failure(usbdev->net,
2352 auth_req->bssid,
2353 key_type, key_id, NULL,
2354 GFP_KERNEL);
2355 }
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002356
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002357 if (group_error) {
2358 key_type = NL80211_KEYTYPE_GROUP;
2359 key_id = -1;
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002360
Jussi Kivilinnaa0f9ce22009-08-28 13:28:24 +03002361 cfg80211_michael_mic_failure(usbdev->net,
2362 auth_req->bssid,
2363 key_type, key_id, NULL,
2364 GFP_KERNEL);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002365 }
2366
2367 buflen -= le32_to_cpu(auth_req->length);
2368 buf += le32_to_cpu(auth_req->length);
2369 }
2370}
2371
2372static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2373 struct ndis_80211_status_indication *indication,
2374 int len)
2375{
2376 struct ndis_80211_pmkid_cand_list *cand_list;
2377 int list_len, expected_len, i;
2378
2379 if (len < offsetof(struct ndis_80211_status_indication, u) +
2380 sizeof(struct ndis_80211_pmkid_cand_list)) {
Joe Perches60b86752010-02-17 10:30:23 +00002381 netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2382 len);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002383 return;
2384 }
2385
2386 list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2387 sizeof(struct ndis_80211_pmkid_candidate);
2388 expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2389 offsetof(struct ndis_80211_status_indication, u);
2390
2391 if (len < expected_len) {
Joe Perches60b86752010-02-17 10:30:23 +00002392 netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2393 len, expected_len);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002394 return;
2395 }
2396
2397 cand_list = &indication->u.cand_list;
2398
Joe Perches60b86752010-02-17 10:30:23 +00002399 netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2400 le32_to_cpu(cand_list->version),
2401 le32_to_cpu(cand_list->num_candidates));
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002402
2403 if (le32_to_cpu(cand_list->version) != 1)
2404 return;
2405
2406 for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002407 struct ndis_80211_pmkid_candidate *cand =
2408 &cand_list->candidate_list[i];
2409
Joe Perches60b86752010-02-17 10:30:23 +00002410 netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, bssid: %pM\n",
2411 i, le32_to_cpu(cand->flags), cand->bssid);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002412
Jussi Kivilinna21ec2d82009-08-28 13:28:30 +03002413#if 0
2414 struct iw_pmkid_cand pcand;
2415 union iwreq_data wrqu;
2416
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002417 memset(&pcand, 0, sizeof(pcand));
2418 if (le32_to_cpu(cand->flags) & 0x01)
2419 pcand.flags |= IW_PMKID_CAND_PREAUTH;
2420 pcand.index = i;
2421 memcpy(pcand.bssid.sa_data, cand->bssid, ETH_ALEN);
2422
2423 memset(&wrqu, 0, sizeof(wrqu));
2424 wrqu.data.length = sizeof(pcand);
2425 wireless_send_event(usbdev->net, IWEVPMKIDCAND, &wrqu,
2426 (u8 *)&pcand);
Jussi Kivilinna21ec2d82009-08-28 13:28:30 +03002427#endif
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002428 }
2429}
2430
2431static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
2432 struct rndis_indicate *msg, int buflen)
2433{
2434 struct ndis_80211_status_indication *indication;
2435 int len, offset;
2436
2437 offset = offsetof(struct rndis_indicate, status) +
2438 le32_to_cpu(msg->offset);
2439 len = le32_to_cpu(msg->length);
2440
2441 if (len < 8) {
Joe Perches60b86752010-02-17 10:30:23 +00002442 netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
2443 len);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002444 return;
2445 }
2446
2447 if (offset + len > buflen) {
Joe Perches60b86752010-02-17 10:30:23 +00002448 netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
2449 offset + len, buflen);
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002450 return;
2451 }
2452
2453 indication = (void *)((u8 *)msg + offset);
2454
2455 switch (le32_to_cpu(indication->status_type)) {
2456 case NDIS_80211_STATUSTYPE_RADIOSTATE:
Joe Perches60b86752010-02-17 10:30:23 +00002457 netdev_info(usbdev->net, "radio state indication: %i\n",
2458 le32_to_cpu(indication->u.radio_status));
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002459 return;
2460
2461 case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
Joe Perches60b86752010-02-17 10:30:23 +00002462 netdev_info(usbdev->net, "media stream mode indication: %i\n",
2463 le32_to_cpu(indication->u.media_stream_mode));
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002464 return;
2465
2466 case NDIS_80211_STATUSTYPE_AUTHENTICATION:
2467 rndis_wlan_auth_indication(usbdev, indication, len);
2468 return;
2469
2470 case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
2471 rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
2472 return;
2473
2474 default:
Joe Perches60b86752010-02-17 10:30:23 +00002475 netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
2476 le32_to_cpu(indication->status_type));
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002477 }
2478}
2479
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002480static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002481{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002482 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002483 struct rndis_indicate *msg = ind;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002484
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002485 switch (msg->status) {
2486 case RNDIS_STATUS_MEDIA_CONNECT:
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +03002487 if (priv->current_command_oid == OID_802_11_ADD_KEY) {
2488 /* OID_802_11_ADD_KEY causes sometimes extra
2489 * "media connect" indications which confuses driver
2490 * and userspace to think that device is
2491 * roaming/reassociating when it isn't.
2492 */
Joe Perches60b86752010-02-17 10:30:23 +00002493 netdev_dbg(usbdev->net, "ignored OID_802_11_ADD_KEY triggered 'media connect'\n");
Jussi Kivilinna5f81ff52009-08-28 12:58:49 +03002494 return;
2495 }
2496
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03002497 usbnet_pause_rx(usbdev);
2498
Joe Perches60b86752010-02-17 10:30:23 +00002499 netdev_info(usbdev->net, "media connect\n");
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002500
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002501 /* queue work to avoid recursive calls into rndis_command */
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002502 set_bit(WORK_LINK_UP, &priv->work_pending);
2503 queue_work(priv->workqueue, &priv->work);
2504 break;
2505
2506 case RNDIS_STATUS_MEDIA_DISCONNECT:
Joe Perches60b86752010-02-17 10:30:23 +00002507 netdev_info(usbdev->net, "media disconnect\n");
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002508
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002509 /* queue work to avoid recursive calls into rndis_command */
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002510 set_bit(WORK_LINK_DOWN, &priv->work_pending);
2511 queue_work(priv->workqueue, &priv->work);
2512 break;
2513
Jussi Kivilinna030645a2009-07-30 19:41:58 +03002514 case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
2515 rndis_wlan_media_specific_indication(usbdev, msg, buflen);
2516 break;
2517
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002518 default:
Joe Perches60b86752010-02-17 10:30:23 +00002519 netdev_info(usbdev->net, "indication: 0x%08x\n",
2520 le32_to_cpu(msg->status));
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002521 break;
2522 }
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002523}
2524
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002525static int rndis_wlan_get_caps(struct usbnet *usbdev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002526{
2527 struct {
2528 __le32 num_items;
2529 __le32 items[8];
2530 } networks_supported;
2531 int len, retval, i, n;
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002532 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002533
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002534 /* determine supported modes */
2535 len = sizeof(networks_supported);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002536 retval = rndis_query_oid(usbdev, OID_802_11_NETWORK_TYPES_SUPPORTED,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002537 &networks_supported, &len);
2538 if (retval >= 0) {
2539 n = le32_to_cpu(networks_supported.num_items);
2540 if (n > 8)
2541 n = 8;
2542 for (i = 0; i < n; i++) {
2543 switch (le32_to_cpu(networks_supported.items[i])) {
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03002544 case NDIS_80211_TYPE_FREQ_HOP:
2545 case NDIS_80211_TYPE_DIRECT_SEQ:
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002546 priv->caps |= CAP_MODE_80211B;
2547 break;
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03002548 case NDIS_80211_TYPE_OFDM_A:
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002549 priv->caps |= CAP_MODE_80211A;
2550 break;
Jussi Kivilinnaaa182942009-06-04 20:13:19 +03002551 case NDIS_80211_TYPE_OFDM_G:
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002552 priv->caps |= CAP_MODE_80211G;
2553 break;
2554 }
2555 }
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002556 }
2557
2558 return retval;
2559}
2560
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002561#define DEVICE_POLLER_JIFFIES (HZ)
2562static void rndis_device_poller(struct work_struct *work)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002563{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002564 struct rndis_wlan_private *priv =
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002565 container_of(work, struct rndis_wlan_private,
2566 dev_poller_work.work);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002567 struct usbnet *usbdev = priv->usbdev;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002568 __le32 rssi, tmp;
Jussi Kivilinnaa97b1f32008-02-06 15:36:10 +02002569 int len, ret, j;
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002570 int update_jiffies = DEVICE_POLLER_JIFFIES;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002571 void *buf;
2572
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03002573 /* Only check/do workaround when connected. Calling is_associated()
2574 * also polls device with rndis_command() and catches for media link
2575 * indications.
2576 */
2577 if (!is_associated(usbdev))
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002578 goto end;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002579
2580 len = sizeof(rssi);
2581 ret = rndis_query_oid(usbdev, OID_802_11_RSSI, &rssi, &len);
Jussi Kivilinna8b89a282009-08-28 13:28:03 +03002582 if (ret == 0)
2583 priv->last_qual = level_to_qual(le32_to_cpu(rssi));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002584
Joe Perches60b86752010-02-17 10:30:23 +00002585 netdev_dbg(usbdev->net, "dev-poller: OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
2586 ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002587
Jussi Kivilinnaa97b1f32008-02-06 15:36:10 +02002588 /* Workaround transfer stalls on poor quality links.
2589 * TODO: find right way to fix these stalls (as stalls do not happen
2590 * with ndiswrapper/windows driver). */
Jussi Kivilinna77593ae2009-11-22 20:16:47 +02002591 if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002592 /* Decrease stats worker interval to catch stalls.
2593 * faster. Faster than 400-500ms causes packet loss,
2594 * Slower doesn't catch stalls fast enough.
2595 */
2596 j = msecs_to_jiffies(priv->param_workaround_interval);
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002597 if (j > DEVICE_POLLER_JIFFIES)
2598 j = DEVICE_POLLER_JIFFIES;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002599 else if (j <= 0)
2600 j = 1;
2601 update_jiffies = j;
2602
2603 /* Send scan OID. Use of both OIDs is required to get device
2604 * working.
2605 */
Harvey Harrison35c26c22009-02-14 22:56:56 -08002606 tmp = cpu_to_le32(1);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002607 rndis_set_oid(usbdev, OID_802_11_BSSID_LIST_SCAN, &tmp,
2608 sizeof(tmp));
2609
2610 len = CONTROL_BUFFER_SIZE;
2611 buf = kmalloc(len, GFP_KERNEL);
2612 if (!buf)
2613 goto end;
2614
2615 rndis_query_oid(usbdev, OID_802_11_BSSID_LIST, buf, &len);
2616 kfree(buf);
2617 }
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002618
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002619end:
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002620 if (update_jiffies >= HZ)
2621 update_jiffies = round_jiffies_relative(update_jiffies);
2622 else {
2623 j = round_jiffies_relative(update_jiffies);
2624 if (abs(j - update_jiffies) <= 10)
2625 update_jiffies = j;
2626 }
2627
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002628 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2629 update_jiffies);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002630}
2631
Jussi Kivilinnac5c4fe92009-09-01 15:33:00 +03002632/*
2633 * driver/device initialization
2634 */
Jussi Kivilinnacef6e912009-11-30 21:50:07 +02002635static void rndis_copy_module_params(struct usbnet *usbdev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002636{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002637 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002638
2639 priv->param_country[0] = modparam_country[0];
2640 priv->param_country[1] = modparam_country[1];
2641 priv->param_country[2] = 0;
2642 priv->param_frameburst = modparam_frameburst;
2643 priv->param_afterburner = modparam_afterburner;
2644 priv->param_power_save = modparam_power_save;
2645 priv->param_power_output = modparam_power_output;
2646 priv->param_roamtrigger = modparam_roamtrigger;
2647 priv->param_roamdelta = modparam_roamdelta;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002648
2649 priv->param_country[0] = toupper(priv->param_country[0]);
2650 priv->param_country[1] = toupper(priv->param_country[1]);
2651 /* doesn't support EU as country code, use FI instead */
2652 if (!strcmp(priv->param_country, "EU"))
2653 strcpy(priv->param_country, "FI");
2654
2655 if (priv->param_power_save < 0)
2656 priv->param_power_save = 0;
2657 else if (priv->param_power_save > 2)
2658 priv->param_power_save = 2;
2659
Jussi Kivilinnaa7624832008-05-27 11:15:08 +03002660 if (priv->param_power_output < 0)
2661 priv->param_power_output = 0;
2662 else if (priv->param_power_output > 3)
2663 priv->param_power_output = 3;
2664
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002665 if (priv->param_roamtrigger < -80)
2666 priv->param_roamtrigger = -80;
2667 else if (priv->param_roamtrigger > -60)
2668 priv->param_roamtrigger = -60;
2669
2670 if (priv->param_roamdelta < 0)
2671 priv->param_roamdelta = 0;
2672 else if (priv->param_roamdelta > 2)
2673 priv->param_roamdelta = 2;
2674
Roel Kluin4d381ff2008-04-23 22:10:29 +02002675 if (modparam_workaround_interval < 0)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002676 priv->param_workaround_interval = 500;
Roel Kluin4d381ff2008-04-23 22:10:29 +02002677 else
2678 priv->param_workaround_interval = modparam_workaround_interval;
Jussi Kivilinnacef6e912009-11-30 21:50:07 +02002679}
2680
2681static int bcm4320a_early_init(struct usbnet *usbdev)
2682{
Jussi Kivilinna6e850af2009-11-30 21:50:13 +02002683 /* copy module parameters for bcm4320a so that iwconfig reports txpower
2684 * and workaround parameter is copied to private structure correctly.
2685 */
2686 rndis_copy_module_params(usbdev);
2687
Jussi Kivilinnacef6e912009-11-30 21:50:07 +02002688 /* bcm4320a doesn't handle configuration parameters well. Try
2689 * set any and you get partially zeroed mac and broken device.
2690 */
2691
2692 return 0;
2693}
2694
2695static int bcm4320b_early_init(struct usbnet *usbdev)
2696{
2697 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2698 char buf[8];
2699
2700 rndis_copy_module_params(usbdev);
2701
2702 /* Early initialization settings, setting these won't have effect
2703 * if called after generic_rndis_bind().
2704 */
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002705
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002706 rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
2707 rndis_set_config_parameter_str(usbdev, "FrameBursting",
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002708 priv->param_frameburst ? "1" : "0");
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002709 rndis_set_config_parameter_str(usbdev, "Afterburner",
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002710 priv->param_afterburner ? "1" : "0");
2711 sprintf(buf, "%d", priv->param_power_save);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002712 rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002713 sprintf(buf, "%d", priv->param_power_output);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002714 rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002715 sprintf(buf, "%d", priv->param_roamtrigger);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002716 rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002717 sprintf(buf, "%d", priv->param_roamdelta);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002718 rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002719
2720 return 0;
2721}
2722
David S. Miller23d12e22009-03-25 00:03:16 -07002723/* same as rndis_netdev_ops but with local multicast handler */
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002724static const struct net_device_ops rndis_wlan_netdev_ops = {
David S. Miller23d12e22009-03-25 00:03:16 -07002725 .ndo_open = usbnet_open,
2726 .ndo_stop = usbnet_stop,
2727 .ndo_start_xmit = usbnet_start_xmit,
2728 .ndo_tx_timeout = usbnet_tx_timeout,
2729 .ndo_set_mac_address = eth_mac_addr,
2730 .ndo_validate_addr = eth_validate_addr,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002731 .ndo_set_multicast_list = rndis_wlan_set_multicast_list,
David S. Miller23d12e22009-03-25 00:03:16 -07002732};
2733
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002734static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002735{
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002736 struct wiphy *wiphy;
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002737 struct rndis_wlan_private *priv;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002738 int retval, len;
2739 __le32 tmp;
2740
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002741 /* allocate wiphy and rndis private data
2742 * NOTE: We only support a single virtual interface, so wiphy
2743 * and wireless_dev are somewhat synonymous for this device.
2744 */
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002745 wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002746 if (!wiphy)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002747 return -ENOMEM;
2748
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002749 priv = wiphy_priv(wiphy);
2750 usbdev->net->ieee80211_ptr = &priv->wdev;
2751 priv->wdev.wiphy = wiphy;
2752 priv->wdev.iftype = NL80211_IFTYPE_STATION;
2753
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002754 /* These have to be initialized before calling generic_rndis_bind().
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002755 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002756 */
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002757 usbdev->driver_priv = priv;
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002758 priv->usbdev = usbdev;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002759
2760 mutex_init(&priv->command_lock);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002761
Jussi Kivilinna8d4d99a2009-04-21 19:48:07 +03002762 /* because rndis_command() sleeps we need to use workqueue */
2763 priv->workqueue = create_singlethread_workqueue("rndis_wlan");
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002764 INIT_WORK(&priv->work, rndis_wlan_worker);
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002765 INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
Jussi Kivilinna8d4d99a2009-04-21 19:48:07 +03002766 INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
2767
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002768 /* try bind rndis_host */
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002769 retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002770 if (retval < 0)
2771 goto fail;
2772
2773 /* generic_rndis_bind set packet filter to multicast_all+
2774 * promisc mode which doesn't work well for our devices (device
2775 * picks up rssi to closest station instead of to access point).
2776 *
2777 * rndis_host wants to avoid all OID as much as possible
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002778 * so do promisc/multicast handling in rndis_wlan.
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002779 */
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002780 usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
David S. Miller23d12e22009-03-25 00:03:16 -07002781
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002782 tmp = RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST;
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002783 retval = rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &tmp,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002784 sizeof(tmp));
2785
2786 len = sizeof(tmp);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002787 retval = rndis_query_oid(usbdev, OID_802_3_MAXIMUM_LIST_SIZE, &tmp,
2788 &len);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002789 priv->multicast_size = le32_to_cpu(tmp);
2790 if (retval < 0 || priv->multicast_size < 0)
2791 priv->multicast_size = 0;
2792 if (priv->multicast_size > 0)
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002793 usbdev->net->flags |= IFF_MULTICAST;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002794 else
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002795 usbdev->net->flags &= ~IFF_MULTICAST;
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002796
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002797 /* fill-out wiphy structure and register w/ cfg80211 */
2798 memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
2799 wiphy->privid = rndis_wiphy_privid;
2800 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
2801 | BIT(NL80211_IFTYPE_ADHOC);
2802 wiphy->max_scan_ssids = 1;
2803
Jussi Kivilinna4a7f13e2009-08-26 15:53:02 +03002804 /* TODO: fill-out band/encr information based on priv->caps */
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002805 rndis_wlan_get_caps(usbdev);
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002806
2807 memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
2808 memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
2809 priv->band.channels = priv->channels;
2810 priv->band.n_channels = ARRAY_SIZE(rndis_channels);
2811 priv->band.bitrates = priv->rates;
2812 priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
2813 wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
Jussi Kivilinna4d2a3692009-03-26 23:40:23 +02002814 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002815
Jussi Kivilinna4a7f13e2009-08-26 15:53:02 +03002816 memcpy(priv->cipher_suites, rndis_cipher_suites,
2817 sizeof(rndis_cipher_suites));
2818 wiphy->cipher_suites = priv->cipher_suites;
2819 wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
2820
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002821 set_wiphy_dev(wiphy, &usbdev->udev->dev);
2822
2823 if (wiphy_register(wiphy)) {
Jussi Kivilinnaeb1a6852009-04-21 19:48:15 +03002824 retval = -ENODEV;
2825 goto fail;
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002826 }
2827
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002828 set_default_iw_params(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002829
Jussi Kivilinnad75ec2b2009-06-16 17:17:21 +03002830 /* set default rts/frag */
2831 rndis_set_wiphy_params(wiphy,
2832 WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
2833
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002834 /* turn radio on */
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03002835 priv->radio_on = true;
2836 disassociate(usbdev, true);
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002837 netif_carrier_off(usbdev->net);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002838
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002839 return 0;
2840
2841fail:
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002842 cancel_delayed_work_sync(&priv->dev_poller_work);
Jussi Kivilinna8d4d99a2009-04-21 19:48:07 +03002843 cancel_delayed_work_sync(&priv->scan_work);
2844 cancel_work_sync(&priv->work);
2845 flush_workqueue(priv->workqueue);
2846 destroy_workqueue(priv->workqueue);
2847
Jussi Kivilinnaeb1a6852009-04-21 19:48:15 +03002848 wiphy_free(wiphy);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002849 return retval;
2850}
2851
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002852static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002853{
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002854 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002855
2856 /* turn radio off */
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03002857 disassociate(usbdev, false);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002858
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002859 cancel_delayed_work_sync(&priv->dev_poller_work);
Jussi Kivilinna71a7b262009-03-26 23:40:31 +02002860 cancel_delayed_work_sync(&priv->scan_work);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002861 cancel_work_sync(&priv->work);
2862 flush_workqueue(priv->workqueue);
2863 destroy_workqueue(priv->workqueue);
2864
2865 if (priv && priv->wpa_ie_len)
2866 kfree(priv->wpa_ie);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002867
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002868 rndis_unbind(usbdev, intf);
John W. Linville5c8fa4f2009-03-26 23:39:53 +02002869
2870 wiphy_unregister(priv->wdev.wiphy);
2871 wiphy_free(priv->wdev.wiphy);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002872}
2873
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002874static int rndis_wlan_reset(struct usbnet *usbdev)
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002875{
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002876 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
Jussi Kivilinna7eaab702009-07-30 19:41:37 +03002877 int retval;
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002878
Joe Perches60b86752010-02-17 10:30:23 +00002879 netdev_dbg(usbdev->net, "%s()\n", __func__);
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002880
Jussi Kivilinna7eaab702009-07-30 19:41:37 +03002881 retval = rndis_reset(usbdev);
2882 if (retval)
Joe Perches60b86752010-02-17 10:30:23 +00002883 netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
Jussi Kivilinna7eaab702009-07-30 19:41:37 +03002884
Jussi Kivilinnae5a11a82009-07-30 19:41:42 +03002885 /* rndis_reset cleared multicast list, so restore here.
2886 (set_multicast_list() also turns on current packet filter) */
Jussi Kivilinna7eaab702009-07-30 19:41:37 +03002887 set_multicast_list(usbdev);
2888
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002889 queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
2890 round_jiffies_relative(DEVICE_POLLER_JIFFIES));
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002891
Jussi Kivilinnaa19d7292008-06-02 18:35:44 +03002892 return deauthenticate(usbdev);
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002893}
2894
Jussi Kivilinna222ec502009-06-16 17:17:32 +03002895static int rndis_wlan_stop(struct usbnet *usbdev)
2896{
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002897 struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2898 int retval;
Jussi Kivilinnae5a11a82009-07-30 19:41:42 +03002899 __le32 filter;
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002900
Joe Perches60b86752010-02-17 10:30:23 +00002901 netdev_dbg(usbdev->net, "%s()\n", __func__);
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002902
Jussi Kivilinna051ae0b2009-09-01 15:32:55 +03002903 retval = disassociate(usbdev, false);
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002904
2905 priv->work_pending = 0;
Jussi Kivilinna305e2432009-08-28 13:28:14 +03002906 cancel_delayed_work_sync(&priv->dev_poller_work);
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002907 cancel_delayed_work_sync(&priv->scan_work);
2908 cancel_work_sync(&priv->work);
2909 flush_workqueue(priv->workqueue);
2910
Jussi Kivilinna005ba2f2009-07-30 19:41:31 +03002911 if (priv->scan_request) {
2912 cfg80211_scan_done(priv->scan_request, true);
2913 priv->scan_request = NULL;
2914 }
2915
Jussi Kivilinnae5a11a82009-07-30 19:41:42 +03002916 /* Set current packet filter zero to block receiving data packets from
2917 device. */
2918 filter = 0;
2919 rndis_set_oid(usbdev, OID_GEN_CURRENT_PACKET_FILTER, &filter,
2920 sizeof(filter));
2921
Jussi Kivilinna110736d2009-07-30 19:41:26 +03002922 return retval;
Jussi Kivilinna222ec502009-06-16 17:17:32 +03002923}
2924
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002925static const struct driver_info bcm4320b_info = {
2926 .description = "Wireless RNDIS device, BCM4320b based",
Jussi Kivilinna1487cd52009-07-30 19:41:20 +03002927 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2928 FLAG_AVOID_UNLINK_URBS,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002929 .bind = rndis_wlan_bind,
2930 .unbind = rndis_wlan_unbind,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002931 .status = rndis_status,
2932 .rx_fixup = rndis_rx_fixup,
2933 .tx_fixup = rndis_tx_fixup,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002934 .reset = rndis_wlan_reset,
Jussi Kivilinna222ec502009-06-16 17:17:32 +03002935 .stop = rndis_wlan_stop,
Jussi Kivilinna59620e92009-05-22 11:58:36 +03002936 .early_init = bcm4320b_early_init,
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002937 .indication = rndis_wlan_indication,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002938};
2939
2940static const struct driver_info bcm4320a_info = {
2941 .description = "Wireless RNDIS device, BCM4320a based",
Jussi Kivilinna1487cd52009-07-30 19:41:20 +03002942 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2943 FLAG_AVOID_UNLINK_URBS,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002944 .bind = rndis_wlan_bind,
2945 .unbind = rndis_wlan_unbind,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002946 .status = rndis_status,
2947 .rx_fixup = rndis_rx_fixup,
2948 .tx_fixup = rndis_tx_fixup,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002949 .reset = rndis_wlan_reset,
Jussi Kivilinna222ec502009-06-16 17:17:32 +03002950 .stop = rndis_wlan_stop,
Jussi Kivilinna59620e92009-05-22 11:58:36 +03002951 .early_init = bcm4320a_early_init,
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002952 .indication = rndis_wlan_indication,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002953};
2954
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002955static const struct driver_info rndis_wlan_info = {
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002956 .description = "Wireless RNDIS device",
Jussi Kivilinna1487cd52009-07-30 19:41:20 +03002957 .flags = FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
2958 FLAG_AVOID_UNLINK_URBS,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002959 .bind = rndis_wlan_bind,
2960 .unbind = rndis_wlan_unbind,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002961 .status = rndis_status,
2962 .rx_fixup = rndis_rx_fixup,
2963 .tx_fixup = rndis_tx_fixup,
Jussi Kivilinna582241a02009-06-04 20:13:25 +03002964 .reset = rndis_wlan_reset,
Jussi Kivilinna222ec502009-06-16 17:17:32 +03002965 .stop = rndis_wlan_stop,
Jussi Kivilinna59620e92009-05-22 11:58:36 +03002966 .early_init = bcm4320a_early_init,
Jussi Kivilinna2a4901b2009-07-30 19:41:52 +03002967 .indication = rndis_wlan_indication,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02002968};
2969
2970/*-------------------------------------------------------------------------*/
2971
2972static const struct usb_device_id products [] = {
2973#define RNDIS_MASTER_INTERFACE \
2974 .bInterfaceClass = USB_CLASS_COMM, \
2975 .bInterfaceSubClass = 2 /* ACM */, \
2976 .bInterfaceProtocol = 0x0ff
2977
2978/* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
2979 * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
2980 */
2981{
2982 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2983 | USB_DEVICE_ID_MATCH_DEVICE,
2984 .idVendor = 0x0411,
2985 .idProduct = 0x00bc, /* Buffalo WLI-U2-KG125S */
2986 RNDIS_MASTER_INTERFACE,
2987 .driver_info = (unsigned long) &bcm4320b_info,
2988}, {
2989 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2990 | USB_DEVICE_ID_MATCH_DEVICE,
2991 .idVendor = 0x0baf,
2992 .idProduct = 0x011b, /* U.S. Robotics USR5421 */
2993 RNDIS_MASTER_INTERFACE,
2994 .driver_info = (unsigned long) &bcm4320b_info,
2995}, {
2996 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2997 | USB_DEVICE_ID_MATCH_DEVICE,
2998 .idVendor = 0x050d,
2999 .idProduct = 0x011b, /* Belkin F5D7051 */
3000 RNDIS_MASTER_INTERFACE,
3001 .driver_info = (unsigned long) &bcm4320b_info,
3002}, {
3003 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3004 | USB_DEVICE_ID_MATCH_DEVICE,
3005 .idVendor = 0x1799, /* Belkin has two vendor ids */
3006 .idProduct = 0x011b, /* Belkin F5D7051 */
3007 RNDIS_MASTER_INTERFACE,
3008 .driver_info = (unsigned long) &bcm4320b_info,
3009}, {
3010 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3011 | USB_DEVICE_ID_MATCH_DEVICE,
3012 .idVendor = 0x13b1,
3013 .idProduct = 0x0014, /* Linksys WUSB54GSv2 */
3014 RNDIS_MASTER_INTERFACE,
3015 .driver_info = (unsigned long) &bcm4320b_info,
3016}, {
3017 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3018 | USB_DEVICE_ID_MATCH_DEVICE,
3019 .idVendor = 0x13b1,
3020 .idProduct = 0x0026, /* Linksys WUSB54GSC */
3021 RNDIS_MASTER_INTERFACE,
3022 .driver_info = (unsigned long) &bcm4320b_info,
3023}, {
3024 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3025 | USB_DEVICE_ID_MATCH_DEVICE,
3026 .idVendor = 0x0b05,
3027 .idProduct = 0x1717, /* Asus WL169gE */
3028 RNDIS_MASTER_INTERFACE,
3029 .driver_info = (unsigned long) &bcm4320b_info,
3030}, {
3031 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3032 | USB_DEVICE_ID_MATCH_DEVICE,
3033 .idVendor = 0x0a5c,
3034 .idProduct = 0xd11b, /* Eminent EM4045 */
3035 RNDIS_MASTER_INTERFACE,
3036 .driver_info = (unsigned long) &bcm4320b_info,
3037}, {
3038 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3039 | USB_DEVICE_ID_MATCH_DEVICE,
3040 .idVendor = 0x1690,
3041 .idProduct = 0x0715, /* BT Voyager 1055 */
3042 RNDIS_MASTER_INTERFACE,
3043 .driver_info = (unsigned long) &bcm4320b_info,
3044},
3045/* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3046 * parameters available, hardware probably contain older firmware version with
3047 * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3048 */
3049{
3050 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3051 | USB_DEVICE_ID_MATCH_DEVICE,
3052 .idVendor = 0x13b1,
3053 .idProduct = 0x000e, /* Linksys WUSB54GSv1 */
3054 RNDIS_MASTER_INTERFACE,
3055 .driver_info = (unsigned long) &bcm4320a_info,
3056}, {
3057 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3058 | USB_DEVICE_ID_MATCH_DEVICE,
3059 .idVendor = 0x0baf,
3060 .idProduct = 0x0111, /* U.S. Robotics USR5420 */
3061 RNDIS_MASTER_INTERFACE,
3062 .driver_info = (unsigned long) &bcm4320a_info,
3063}, {
3064 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
3065 | USB_DEVICE_ID_MATCH_DEVICE,
3066 .idVendor = 0x0411,
3067 .idProduct = 0x004b, /* BUFFALO WLI-USB-G54 */
3068 RNDIS_MASTER_INTERFACE,
3069 .driver_info = (unsigned long) &bcm4320a_info,
3070},
3071/* Generic Wireless RNDIS devices that we don't have exact
3072 * idVendor/idProduct/chip yet.
3073 */
3074{
3075 /* RNDIS is MSFT's un-official variant of CDC ACM */
3076 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
Jussi Kivilinna582241a02009-06-04 20:13:25 +03003077 .driver_info = (unsigned long) &rndis_wlan_info,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02003078}, {
3079 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3080 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
Jussi Kivilinna582241a02009-06-04 20:13:25 +03003081 .driver_info = (unsigned long) &rndis_wlan_info,
Jussi Kivilinnabf164cc2008-01-26 00:51:51 +02003082},
3083 { }, // END
3084};
3085MODULE_DEVICE_TABLE(usb, products);
3086
3087static struct usb_driver rndis_wlan_driver = {
3088 .name = "rndis_wlan",
3089 .id_table = products,
3090 .probe = usbnet_probe,
3091 .disconnect = usbnet_disconnect,
3092 .suspend = usbnet_suspend,
3093 .resume = usbnet_resume,
3094};
3095
3096static int __init rndis_wlan_init(void)
3097{
3098 return usb_register(&rndis_wlan_driver);
3099}
3100module_init(rndis_wlan_init);
3101
3102static void __exit rndis_wlan_exit(void)
3103{
3104 usb_deregister(&rndis_wlan_driver);
3105}
3106module_exit(rndis_wlan_exit);
3107
3108MODULE_AUTHOR("Bjorge Dijkstra");
3109MODULE_AUTHOR("Jussi Kivilinna");
3110MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3111MODULE_LICENSE("GPL");
3112