xref: /openbmc/linux/drivers/net/wireless/legacy/rndis_wlan.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1298e50adSKalle Valo // SPDX-License-Identifier: GPL-2.0-or-later
2298e50adSKalle Valo /*
3*5d7cf67fSAlan Stern  * Driver for RNDIS based USB wireless devices.
4298e50adSKalle Valo  *
5298e50adSKalle Valo  * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
6298e50adSKalle Valo  * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@iki.fi>
7298e50adSKalle Valo  *
8298e50adSKalle Valo  *  Portions of this file are based on NDISwrapper project,
9298e50adSKalle Valo  *  Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
10298e50adSKalle Valo  *  http://ndiswrapper.sourceforge.net/
11298e50adSKalle Valo  */
12298e50adSKalle Valo 
13298e50adSKalle Valo // #define	DEBUG			// error path messages, extra info
14298e50adSKalle Valo // #define	VERBOSE			// more; success messages
15298e50adSKalle Valo 
16298e50adSKalle Valo #include <linux/module.h>
17298e50adSKalle Valo #include <linux/netdevice.h>
18298e50adSKalle Valo #include <linux/etherdevice.h>
19298e50adSKalle Valo #include <linux/ethtool.h>
20298e50adSKalle Valo #include <linux/workqueue.h>
21298e50adSKalle Valo #include <linux/mutex.h>
22298e50adSKalle Valo #include <linux/mii.h>
23298e50adSKalle Valo #include <linux/usb.h>
24298e50adSKalle Valo #include <linux/usb/cdc.h>
25298e50adSKalle Valo #include <linux/ieee80211.h>
26298e50adSKalle Valo #include <linux/if_arp.h>
27298e50adSKalle Valo #include <linux/ctype.h>
28298e50adSKalle Valo #include <linux/spinlock.h>
29298e50adSKalle Valo #include <linux/slab.h>
30298e50adSKalle Valo #include <net/cfg80211.h>
31298e50adSKalle Valo #include <linux/usb/usbnet.h>
32298e50adSKalle Valo #include <linux/usb/rndis_host.h>
33298e50adSKalle Valo 
34298e50adSKalle Valo 
35298e50adSKalle Valo /* NOTE: All these are settings for Broadcom chipset */
36298e50adSKalle Valo static char modparam_country[4] = "EU";
37298e50adSKalle Valo module_param_string(country, modparam_country, 4, 0444);
38298e50adSKalle Valo MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
39298e50adSKalle Valo 
40298e50adSKalle Valo static int modparam_frameburst = 1;
41298e50adSKalle Valo module_param_named(frameburst, modparam_frameburst, int, 0444);
42298e50adSKalle Valo MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
43298e50adSKalle Valo 
44298e50adSKalle Valo static int modparam_afterburner = 0;
45298e50adSKalle Valo module_param_named(afterburner, modparam_afterburner, int, 0444);
46298e50adSKalle Valo MODULE_PARM_DESC(afterburner,
47298e50adSKalle Valo 	"enable afterburner aka '125 High Speed Mode' (default: off)");
48298e50adSKalle Valo 
49298e50adSKalle Valo static int modparam_power_save = 0;
50298e50adSKalle Valo module_param_named(power_save, modparam_power_save, int, 0444);
51298e50adSKalle Valo MODULE_PARM_DESC(power_save,
52298e50adSKalle Valo 	"set power save mode: 0=off, 1=on, 2=fast (default: off)");
53298e50adSKalle Valo 
54298e50adSKalle Valo static int modparam_power_output = 3;
55298e50adSKalle Valo module_param_named(power_output, modparam_power_output, int, 0444);
56298e50adSKalle Valo MODULE_PARM_DESC(power_output,
57298e50adSKalle Valo 	"set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
58298e50adSKalle Valo 
59298e50adSKalle Valo static int modparam_roamtrigger = -70;
60298e50adSKalle Valo module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
61298e50adSKalle Valo MODULE_PARM_DESC(roamtrigger,
62298e50adSKalle Valo 	"set roaming dBm trigger: -80=optimize for distance, "
63298e50adSKalle Valo 				"-60=bandwidth (default: -70)");
64298e50adSKalle Valo 
65298e50adSKalle Valo static int modparam_roamdelta = 1;
66298e50adSKalle Valo module_param_named(roamdelta, modparam_roamdelta, int, 0444);
67298e50adSKalle Valo MODULE_PARM_DESC(roamdelta,
68298e50adSKalle Valo 	"set roaming tendency: 0=aggressive, 1=moderate, "
69298e50adSKalle Valo 				"2=conservative (default: moderate)");
70298e50adSKalle Valo 
71298e50adSKalle Valo static int modparam_workaround_interval;
72298e50adSKalle Valo module_param_named(workaround_interval, modparam_workaround_interval,
73298e50adSKalle Valo 							int, 0444);
74298e50adSKalle Valo MODULE_PARM_DESC(workaround_interval,
75298e50adSKalle Valo 	"set stall workaround interval in msecs (0=disabled) (default: 0)");
76298e50adSKalle Valo 
77298e50adSKalle Valo /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
78298e50adSKalle Valo #define	WL_NOISE	-96	/* typical noise level in dBm */
79298e50adSKalle Valo #define	WL_SIGMAX	-32	/* typical maximum signal level in dBm */
80298e50adSKalle Valo 
81298e50adSKalle Valo 
82298e50adSKalle Valo /* Assume that Broadcom 4320 (only chipset at time of writing known to be
83298e50adSKalle Valo  * based on wireless rndis) has default txpower of 13dBm.
84298e50adSKalle Valo  * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
85298e50adSKalle Valo  *  100% : 20 mW ~ 13dBm
86298e50adSKalle Valo  *   75% : 15 mW ~ 12dBm
87298e50adSKalle Valo  *   50% : 10 mW ~ 10dBm
88298e50adSKalle Valo  *   25% :  5 mW ~  7dBm
89298e50adSKalle Valo  */
90298e50adSKalle Valo #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
91298e50adSKalle Valo #define BCM4320_DEFAULT_TXPOWER_DBM_75  12
92298e50adSKalle Valo #define BCM4320_DEFAULT_TXPOWER_DBM_50  10
93298e50adSKalle Valo #define BCM4320_DEFAULT_TXPOWER_DBM_25  7
94298e50adSKalle Valo 
95298e50adSKalle Valo /* Known device types */
96298e50adSKalle Valo #define RNDIS_UNKNOWN	0
97298e50adSKalle Valo #define RNDIS_BCM4320A	1
98298e50adSKalle Valo #define RNDIS_BCM4320B	2
99298e50adSKalle Valo 
100298e50adSKalle Valo 
101298e50adSKalle Valo /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
102298e50adSKalle Valo  * slightly modified for datatype endianess, etc
103298e50adSKalle Valo  */
104298e50adSKalle Valo #define NDIS_802_11_LENGTH_SSID 32
105298e50adSKalle Valo #define NDIS_802_11_LENGTH_RATES 8
106298e50adSKalle Valo #define NDIS_802_11_LENGTH_RATES_EX 16
107298e50adSKalle Valo 
108298e50adSKalle Valo enum ndis_80211_net_type {
109298e50adSKalle Valo 	NDIS_80211_TYPE_FREQ_HOP,
110298e50adSKalle Valo 	NDIS_80211_TYPE_DIRECT_SEQ,
111298e50adSKalle Valo 	NDIS_80211_TYPE_OFDM_A,
112298e50adSKalle Valo 	NDIS_80211_TYPE_OFDM_G
113298e50adSKalle Valo };
114298e50adSKalle Valo 
115298e50adSKalle Valo enum ndis_80211_net_infra {
116298e50adSKalle Valo 	NDIS_80211_INFRA_ADHOC,
117298e50adSKalle Valo 	NDIS_80211_INFRA_INFRA,
118298e50adSKalle Valo 	NDIS_80211_INFRA_AUTO_UNKNOWN
119298e50adSKalle Valo };
120298e50adSKalle Valo 
121298e50adSKalle Valo enum ndis_80211_auth_mode {
122298e50adSKalle Valo 	NDIS_80211_AUTH_OPEN,
123298e50adSKalle Valo 	NDIS_80211_AUTH_SHARED,
124298e50adSKalle Valo 	NDIS_80211_AUTH_AUTO_SWITCH,
125298e50adSKalle Valo 	NDIS_80211_AUTH_WPA,
126298e50adSKalle Valo 	NDIS_80211_AUTH_WPA_PSK,
127298e50adSKalle Valo 	NDIS_80211_AUTH_WPA_NONE,
128298e50adSKalle Valo 	NDIS_80211_AUTH_WPA2,
129298e50adSKalle Valo 	NDIS_80211_AUTH_WPA2_PSK
130298e50adSKalle Valo };
131298e50adSKalle Valo 
132298e50adSKalle Valo enum ndis_80211_encr_status {
133298e50adSKalle Valo 	NDIS_80211_ENCR_WEP_ENABLED,
134298e50adSKalle Valo 	NDIS_80211_ENCR_DISABLED,
135298e50adSKalle Valo 	NDIS_80211_ENCR_WEP_KEY_ABSENT,
136298e50adSKalle Valo 	NDIS_80211_ENCR_NOT_SUPPORTED,
137298e50adSKalle Valo 	NDIS_80211_ENCR_TKIP_ENABLED,
138298e50adSKalle Valo 	NDIS_80211_ENCR_TKIP_KEY_ABSENT,
139298e50adSKalle Valo 	NDIS_80211_ENCR_CCMP_ENABLED,
140298e50adSKalle Valo 	NDIS_80211_ENCR_CCMP_KEY_ABSENT
141298e50adSKalle Valo };
142298e50adSKalle Valo 
143298e50adSKalle Valo enum ndis_80211_priv_filter {
144298e50adSKalle Valo 	NDIS_80211_PRIV_ACCEPT_ALL,
145298e50adSKalle Valo 	NDIS_80211_PRIV_8021X_WEP
146298e50adSKalle Valo };
147298e50adSKalle Valo 
148298e50adSKalle Valo enum ndis_80211_status_type {
149298e50adSKalle Valo 	NDIS_80211_STATUSTYPE_AUTHENTICATION,
150298e50adSKalle Valo 	NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
151298e50adSKalle Valo 	NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
152298e50adSKalle Valo 	NDIS_80211_STATUSTYPE_RADIOSTATE,
153298e50adSKalle Valo };
154298e50adSKalle Valo 
155298e50adSKalle Valo enum ndis_80211_media_stream_mode {
156298e50adSKalle Valo 	NDIS_80211_MEDIA_STREAM_OFF,
157298e50adSKalle Valo 	NDIS_80211_MEDIA_STREAM_ON
158298e50adSKalle Valo };
159298e50adSKalle Valo 
160298e50adSKalle Valo enum ndis_80211_radio_status {
161298e50adSKalle Valo 	NDIS_80211_RADIO_STATUS_ON,
162298e50adSKalle Valo 	NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
163298e50adSKalle Valo 	NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
164298e50adSKalle Valo };
165298e50adSKalle Valo 
166298e50adSKalle Valo enum ndis_80211_addkey_bits {
167298e50adSKalle Valo 	NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
168298e50adSKalle Valo 	NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
169298e50adSKalle Valo 	NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
170298e50adSKalle Valo 	NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
171298e50adSKalle Valo };
172298e50adSKalle Valo 
173298e50adSKalle Valo enum ndis_80211_addwep_bits {
174298e50adSKalle Valo 	NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
175298e50adSKalle Valo 	NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
176298e50adSKalle Valo };
177298e50adSKalle Valo 
178298e50adSKalle Valo enum ndis_80211_power_mode {
179298e50adSKalle Valo 	NDIS_80211_POWER_MODE_CAM,
180298e50adSKalle Valo 	NDIS_80211_POWER_MODE_MAX_PSP,
181298e50adSKalle Valo 	NDIS_80211_POWER_MODE_FAST_PSP,
182298e50adSKalle Valo };
183298e50adSKalle Valo 
184298e50adSKalle Valo enum ndis_80211_pmkid_cand_list_flag_bits {
185298e50adSKalle Valo 	NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
186298e50adSKalle Valo };
187298e50adSKalle Valo 
188298e50adSKalle Valo struct ndis_80211_auth_request {
189298e50adSKalle Valo 	__le32 length;
190298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
191298e50adSKalle Valo 	u8 padding[2];
192298e50adSKalle Valo 	__le32 flags;
193298e50adSKalle Valo } __packed;
194298e50adSKalle Valo 
195298e50adSKalle Valo struct ndis_80211_pmkid_candidate {
196298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
197298e50adSKalle Valo 	u8 padding[2];
198298e50adSKalle Valo 	__le32 flags;
199298e50adSKalle Valo } __packed;
200298e50adSKalle Valo 
201298e50adSKalle Valo struct ndis_80211_pmkid_cand_list {
202298e50adSKalle Valo 	__le32 version;
203298e50adSKalle Valo 	__le32 num_candidates;
204298e50adSKalle Valo 	struct ndis_80211_pmkid_candidate candidate_list[];
205298e50adSKalle Valo } __packed;
206298e50adSKalle Valo 
207298e50adSKalle Valo struct ndis_80211_status_indication {
208298e50adSKalle Valo 	__le32 status_type;
209298e50adSKalle Valo 	union {
210298e50adSKalle Valo 		__le32					media_stream_mode;
211298e50adSKalle Valo 		__le32					radio_status;
21206dabcccSGustavo A. R. Silva 		DECLARE_FLEX_ARRAY(struct ndis_80211_auth_request, auth_request);
213298e50adSKalle Valo 		struct ndis_80211_pmkid_cand_list	cand_list;
214298e50adSKalle Valo 	} u;
215298e50adSKalle Valo } __packed;
216298e50adSKalle Valo 
217298e50adSKalle Valo struct ndis_80211_ssid {
218298e50adSKalle Valo 	__le32 length;
219298e50adSKalle Valo 	u8 essid[NDIS_802_11_LENGTH_SSID];
220298e50adSKalle Valo } __packed;
221298e50adSKalle Valo 
222298e50adSKalle Valo struct ndis_80211_conf_freq_hop {
223298e50adSKalle Valo 	__le32 length;
224298e50adSKalle Valo 	__le32 hop_pattern;
225298e50adSKalle Valo 	__le32 hop_set;
226298e50adSKalle Valo 	__le32 dwell_time;
227298e50adSKalle Valo } __packed;
228298e50adSKalle Valo 
229298e50adSKalle Valo struct ndis_80211_conf {
230298e50adSKalle Valo 	__le32 length;
231298e50adSKalle Valo 	__le32 beacon_period;
232298e50adSKalle Valo 	__le32 atim_window;
233298e50adSKalle Valo 	__le32 ds_config;
234298e50adSKalle Valo 	struct ndis_80211_conf_freq_hop fh_config;
235298e50adSKalle Valo } __packed;
236298e50adSKalle Valo 
237298e50adSKalle Valo struct ndis_80211_bssid_ex {
238298e50adSKalle Valo 	__le32 length;
239298e50adSKalle Valo 	u8 mac[ETH_ALEN];
240298e50adSKalle Valo 	u8 padding[2];
241298e50adSKalle Valo 	struct ndis_80211_ssid ssid;
242298e50adSKalle Valo 	__le32 privacy;
243298e50adSKalle Valo 	__le32 rssi;
244298e50adSKalle Valo 	__le32 net_type;
245298e50adSKalle Valo 	struct ndis_80211_conf config;
246298e50adSKalle Valo 	__le32 net_infra;
247298e50adSKalle Valo 	u8 rates[NDIS_802_11_LENGTH_RATES_EX];
248298e50adSKalle Valo 	__le32 ie_length;
249298e50adSKalle Valo 	u8 ies[];
250298e50adSKalle Valo } __packed;
251298e50adSKalle Valo 
252298e50adSKalle Valo struct ndis_80211_bssid_list_ex {
253298e50adSKalle Valo 	__le32 num_items;
254298e50adSKalle Valo 	u8 bssid_data[];
255298e50adSKalle Valo } __packed;
256298e50adSKalle Valo 
257298e50adSKalle Valo struct ndis_80211_fixed_ies {
258298e50adSKalle Valo 	u8 timestamp[8];
259298e50adSKalle Valo 	__le16 beacon_interval;
260298e50adSKalle Valo 	__le16 capabilities;
261298e50adSKalle Valo } __packed;
262298e50adSKalle Valo 
263298e50adSKalle Valo struct ndis_80211_wep_key {
264298e50adSKalle Valo 	__le32 size;
265298e50adSKalle Valo 	__le32 index;
266298e50adSKalle Valo 	__le32 length;
267298e50adSKalle Valo 	u8 material[32];
268298e50adSKalle Valo } __packed;
269298e50adSKalle Valo 
270298e50adSKalle Valo struct ndis_80211_key {
271298e50adSKalle Valo 	__le32 size;
272298e50adSKalle Valo 	__le32 index;
273298e50adSKalle Valo 	__le32 length;
274298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
275298e50adSKalle Valo 	u8 padding[6];
276298e50adSKalle Valo 	u8 rsc[8];
277298e50adSKalle Valo 	u8 material[32];
278298e50adSKalle Valo } __packed;
279298e50adSKalle Valo 
280298e50adSKalle Valo struct ndis_80211_remove_key {
281298e50adSKalle Valo 	__le32 size;
282298e50adSKalle Valo 	__le32 index;
283298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
284298e50adSKalle Valo 	u8 padding[2];
285298e50adSKalle Valo } __packed;
286298e50adSKalle Valo 
287298e50adSKalle Valo struct ndis_config_param {
288298e50adSKalle Valo 	__le32 name_offs;
289298e50adSKalle Valo 	__le32 name_length;
290298e50adSKalle Valo 	__le32 type;
291298e50adSKalle Valo 	__le32 value_offs;
292298e50adSKalle Valo 	__le32 value_length;
293298e50adSKalle Valo } __packed;
294298e50adSKalle Valo 
295298e50adSKalle Valo struct ndis_80211_assoc_info {
296298e50adSKalle Valo 	__le32 length;
297298e50adSKalle Valo 	__le16 req_ies;
298298e50adSKalle Valo 	struct req_ie {
299298e50adSKalle Valo 		__le16 capa;
300298e50adSKalle Valo 		__le16 listen_interval;
301298e50adSKalle Valo 		u8 cur_ap_address[ETH_ALEN];
302298e50adSKalle Valo 	} req_ie;
303298e50adSKalle Valo 	__le32 req_ie_length;
304298e50adSKalle Valo 	__le32 offset_req_ies;
305298e50adSKalle Valo 	__le16 resp_ies;
306298e50adSKalle Valo 	struct resp_ie {
307298e50adSKalle Valo 		__le16 capa;
308298e50adSKalle Valo 		__le16 status_code;
309298e50adSKalle Valo 		__le16 assoc_id;
310298e50adSKalle Valo 	} resp_ie;
311298e50adSKalle Valo 	__le32 resp_ie_length;
312298e50adSKalle Valo 	__le32 offset_resp_ies;
313298e50adSKalle Valo } __packed;
314298e50adSKalle Valo 
315298e50adSKalle Valo struct ndis_80211_capability {
316298e50adSKalle Valo 	__le32 length;
317298e50adSKalle Valo 	__le32 version;
318298e50adSKalle Valo 	__le32 num_pmkids;
319298e50adSKalle Valo 	__le32 num_auth_encr_pair;
320298e50adSKalle Valo } __packed;
321298e50adSKalle Valo 
322298e50adSKalle Valo struct ndis_80211_bssid_info {
323298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
324298e50adSKalle Valo 	u8 pmkid[16];
325298e50adSKalle Valo } __packed;
326298e50adSKalle Valo 
327298e50adSKalle Valo struct ndis_80211_pmkid {
328298e50adSKalle Valo 	__le32 length;
329298e50adSKalle Valo 	__le32 bssid_info_count;
330298e50adSKalle Valo 	struct ndis_80211_bssid_info bssid_info[];
331298e50adSKalle Valo } __packed;
332298e50adSKalle Valo 
333298e50adSKalle Valo /*
334298e50adSKalle Valo  *  private data
335298e50adSKalle Valo  */
336298e50adSKalle Valo #define CAP_MODE_80211A		1
337298e50adSKalle Valo #define CAP_MODE_80211B		2
338298e50adSKalle Valo #define CAP_MODE_80211G		4
339298e50adSKalle Valo #define CAP_MODE_MASK		7
340298e50adSKalle Valo 
341298e50adSKalle Valo #define WORK_LINK_UP		0
342298e50adSKalle Valo #define WORK_LINK_DOWN		1
343298e50adSKalle Valo #define WORK_SET_MULTICAST_LIST	2
344298e50adSKalle Valo 
345298e50adSKalle Valo #define RNDIS_WLAN_ALG_NONE	0
346298e50adSKalle Valo #define RNDIS_WLAN_ALG_WEP	(1<<0)
347298e50adSKalle Valo #define RNDIS_WLAN_ALG_TKIP	(1<<1)
348298e50adSKalle Valo #define RNDIS_WLAN_ALG_CCMP	(1<<2)
349298e50adSKalle Valo 
350298e50adSKalle Valo #define RNDIS_WLAN_NUM_KEYS		4
351298e50adSKalle Valo #define RNDIS_WLAN_KEY_MGMT_NONE	0
352298e50adSKalle Valo #define RNDIS_WLAN_KEY_MGMT_802_1X	(1<<0)
353298e50adSKalle Valo #define RNDIS_WLAN_KEY_MGMT_PSK		(1<<1)
354298e50adSKalle Valo 
355298e50adSKalle Valo #define COMMAND_BUFFER_SIZE	(CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
356298e50adSKalle Valo 
357298e50adSKalle Valo static const struct ieee80211_channel rndis_channels[] = {
358298e50adSKalle Valo 	{ .center_freq = 2412 },
359298e50adSKalle Valo 	{ .center_freq = 2417 },
360298e50adSKalle Valo 	{ .center_freq = 2422 },
361298e50adSKalle Valo 	{ .center_freq = 2427 },
362298e50adSKalle Valo 	{ .center_freq = 2432 },
363298e50adSKalle Valo 	{ .center_freq = 2437 },
364298e50adSKalle Valo 	{ .center_freq = 2442 },
365298e50adSKalle Valo 	{ .center_freq = 2447 },
366298e50adSKalle Valo 	{ .center_freq = 2452 },
367298e50adSKalle Valo 	{ .center_freq = 2457 },
368298e50adSKalle Valo 	{ .center_freq = 2462 },
369298e50adSKalle Valo 	{ .center_freq = 2467 },
370298e50adSKalle Valo 	{ .center_freq = 2472 },
371298e50adSKalle Valo 	{ .center_freq = 2484 },
372298e50adSKalle Valo };
373298e50adSKalle Valo 
374298e50adSKalle Valo static const struct ieee80211_rate rndis_rates[] = {
375298e50adSKalle Valo 	{ .bitrate = 10 },
376298e50adSKalle Valo 	{ .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
377298e50adSKalle Valo 	{ .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
378298e50adSKalle Valo 	{ .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
379298e50adSKalle Valo 	{ .bitrate = 60 },
380298e50adSKalle Valo 	{ .bitrate = 90 },
381298e50adSKalle Valo 	{ .bitrate = 120 },
382298e50adSKalle Valo 	{ .bitrate = 180 },
383298e50adSKalle Valo 	{ .bitrate = 240 },
384298e50adSKalle Valo 	{ .bitrate = 360 },
385298e50adSKalle Valo 	{ .bitrate = 480 },
386298e50adSKalle Valo 	{ .bitrate = 540 }
387298e50adSKalle Valo };
388298e50adSKalle Valo 
389298e50adSKalle Valo static const u32 rndis_cipher_suites[] = {
390298e50adSKalle Valo 	WLAN_CIPHER_SUITE_WEP40,
391298e50adSKalle Valo 	WLAN_CIPHER_SUITE_WEP104,
392298e50adSKalle Valo 	WLAN_CIPHER_SUITE_TKIP,
393298e50adSKalle Valo 	WLAN_CIPHER_SUITE_CCMP,
394298e50adSKalle Valo };
395298e50adSKalle Valo 
396298e50adSKalle Valo struct rndis_wlan_encr_key {
397298e50adSKalle Valo 	int len;
398298e50adSKalle Valo 	u32 cipher;
399298e50adSKalle Valo 	u8 material[32];
400298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
401298e50adSKalle Valo 	bool pairwise;
402298e50adSKalle Valo 	bool tx_key;
403298e50adSKalle Valo };
404298e50adSKalle Valo 
405298e50adSKalle Valo /* RNDIS device private data */
406298e50adSKalle Valo struct rndis_wlan_private {
407298e50adSKalle Valo 	struct usbnet *usbdev;
408298e50adSKalle Valo 
409298e50adSKalle Valo 	struct wireless_dev wdev;
410298e50adSKalle Valo 
411298e50adSKalle Valo 	struct cfg80211_scan_request *scan_request;
412298e50adSKalle Valo 
413298e50adSKalle Valo 	struct workqueue_struct *workqueue;
414298e50adSKalle Valo 	struct delayed_work dev_poller_work;
415298e50adSKalle Valo 	struct delayed_work scan_work;
416298e50adSKalle Valo 	struct work_struct work;
417298e50adSKalle Valo 	struct mutex command_lock;
418298e50adSKalle Valo 	unsigned long work_pending;
419298e50adSKalle Valo 	int last_qual;
420298e50adSKalle Valo 	s32 cqm_rssi_thold;
421298e50adSKalle Valo 	u32 cqm_rssi_hyst;
422298e50adSKalle Valo 	int last_cqm_event_rssi;
423298e50adSKalle Valo 
424298e50adSKalle Valo 	struct ieee80211_supported_band band;
425298e50adSKalle Valo 	struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
426298e50adSKalle Valo 	struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
427298e50adSKalle Valo 	u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
428298e50adSKalle Valo 
429298e50adSKalle Valo 	int device_type;
430298e50adSKalle Valo 	int caps;
431298e50adSKalle Valo 	int multicast_size;
432298e50adSKalle Valo 
433298e50adSKalle Valo 	/* module parameters */
434298e50adSKalle Valo 	char param_country[4];
435298e50adSKalle Valo 	int  param_frameburst;
436298e50adSKalle Valo 	int  param_afterburner;
437298e50adSKalle Valo 	int  param_power_save;
438298e50adSKalle Valo 	int  param_power_output;
439298e50adSKalle Valo 	int  param_roamtrigger;
440298e50adSKalle Valo 	int  param_roamdelta;
441298e50adSKalle Valo 	u32  param_workaround_interval;
442298e50adSKalle Valo 
443298e50adSKalle Valo 	/* hardware state */
444298e50adSKalle Valo 	bool radio_on;
445298e50adSKalle Valo 	int power_mode;
446298e50adSKalle Valo 	int infra_mode;
447298e50adSKalle Valo 	bool connected;
448298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
449298e50adSKalle Valo 	u32 current_command_oid;
450298e50adSKalle Valo 
451298e50adSKalle Valo 	/* encryption stuff */
452298e50adSKalle Valo 	u8 encr_tx_key_index;
453298e50adSKalle Valo 	struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
454298e50adSKalle Valo 	int  wpa_version;
455298e50adSKalle Valo 
456298e50adSKalle Valo 	u8 command_buffer[COMMAND_BUFFER_SIZE];
457298e50adSKalle Valo };
458298e50adSKalle Valo 
459298e50adSKalle Valo /*
460298e50adSKalle Valo  * cfg80211 ops
461298e50adSKalle Valo  */
462298e50adSKalle Valo static int rndis_change_virtual_intf(struct wiphy *wiphy,
463298e50adSKalle Valo 					struct net_device *dev,
464298e50adSKalle Valo 					enum nl80211_iftype type,
465298e50adSKalle Valo 					struct vif_params *params);
466298e50adSKalle Valo 
467298e50adSKalle Valo static int rndis_scan(struct wiphy *wiphy,
468298e50adSKalle Valo 			struct cfg80211_scan_request *request);
469298e50adSKalle Valo 
470298e50adSKalle Valo static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
471298e50adSKalle Valo 
472298e50adSKalle Valo static int rndis_set_tx_power(struct wiphy *wiphy,
473298e50adSKalle Valo 			      struct wireless_dev *wdev,
474298e50adSKalle Valo 			      enum nl80211_tx_power_setting type,
475298e50adSKalle Valo 			      int mbm);
476298e50adSKalle Valo static int rndis_get_tx_power(struct wiphy *wiphy,
477298e50adSKalle Valo 			      struct wireless_dev *wdev,
478298e50adSKalle Valo 			      int *dbm);
479298e50adSKalle Valo 
480298e50adSKalle Valo static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
481298e50adSKalle Valo 				struct cfg80211_connect_params *sme);
482298e50adSKalle Valo 
483298e50adSKalle Valo static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
484298e50adSKalle Valo 				u16 reason_code);
485298e50adSKalle Valo 
486298e50adSKalle Valo static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
487298e50adSKalle Valo 					struct cfg80211_ibss_params *params);
488298e50adSKalle Valo 
489298e50adSKalle Valo static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
490298e50adSKalle Valo 
491298e50adSKalle Valo static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
492298e50adSKalle Valo 			 int link_id,  u8 key_index, bool pairwise,
493298e50adSKalle Valo 			 const u8 *mac_addr, struct key_params *params);
494298e50adSKalle Valo 
495298e50adSKalle Valo static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
496298e50adSKalle Valo 			 int link_id, u8 key_index, bool pairwise,
497298e50adSKalle Valo 			 const u8 *mac_addr);
498298e50adSKalle Valo 
499298e50adSKalle Valo static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
500298e50adSKalle Valo 				 int link_id, u8 key_index, bool unicast,
501298e50adSKalle Valo 				 bool multicast);
502298e50adSKalle Valo 
503298e50adSKalle Valo static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
504298e50adSKalle Valo 			     const u8 *mac, struct station_info *sinfo);
505298e50adSKalle Valo 
506298e50adSKalle Valo static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
507298e50adSKalle Valo 			       int idx, u8 *mac, struct station_info *sinfo);
508298e50adSKalle Valo 
509298e50adSKalle Valo static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
510298e50adSKalle Valo 				struct cfg80211_pmksa *pmksa);
511298e50adSKalle Valo 
512298e50adSKalle Valo static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
513298e50adSKalle Valo 				struct cfg80211_pmksa *pmksa);
514298e50adSKalle Valo 
515298e50adSKalle Valo static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
516298e50adSKalle Valo 
517298e50adSKalle Valo static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
518298e50adSKalle Valo 				bool enabled, int timeout);
519298e50adSKalle Valo 
520298e50adSKalle Valo static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
521298e50adSKalle Valo 					struct net_device *dev,
522298e50adSKalle Valo 					s32 rssi_thold, u32 rssi_hyst);
523298e50adSKalle Valo 
524298e50adSKalle Valo static const struct cfg80211_ops rndis_config_ops = {
525298e50adSKalle Valo 	.change_virtual_intf = rndis_change_virtual_intf,
526298e50adSKalle Valo 	.scan = rndis_scan,
527298e50adSKalle Valo 	.set_wiphy_params = rndis_set_wiphy_params,
528298e50adSKalle Valo 	.set_tx_power = rndis_set_tx_power,
529298e50adSKalle Valo 	.get_tx_power = rndis_get_tx_power,
530298e50adSKalle Valo 	.connect = rndis_connect,
531298e50adSKalle Valo 	.disconnect = rndis_disconnect,
532298e50adSKalle Valo 	.join_ibss = rndis_join_ibss,
533298e50adSKalle Valo 	.leave_ibss = rndis_leave_ibss,
534298e50adSKalle Valo 	.add_key = rndis_add_key,
535298e50adSKalle Valo 	.del_key = rndis_del_key,
536298e50adSKalle Valo 	.set_default_key = rndis_set_default_key,
537298e50adSKalle Valo 	.get_station = rndis_get_station,
538298e50adSKalle Valo 	.dump_station = rndis_dump_station,
539298e50adSKalle Valo 	.set_pmksa = rndis_set_pmksa,
540298e50adSKalle Valo 	.del_pmksa = rndis_del_pmksa,
541298e50adSKalle Valo 	.flush_pmksa = rndis_flush_pmksa,
542298e50adSKalle Valo 	.set_power_mgmt = rndis_set_power_mgmt,
543298e50adSKalle Valo 	.set_cqm_rssi_config = rndis_set_cqm_rssi_config,
544298e50adSKalle Valo };
545298e50adSKalle Valo 
546298e50adSKalle Valo static void *rndis_wiphy_privid = &rndis_wiphy_privid;
547298e50adSKalle Valo 
548298e50adSKalle Valo 
get_rndis_wlan_priv(struct usbnet * dev)549298e50adSKalle Valo static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
550298e50adSKalle Valo {
551298e50adSKalle Valo 	return (struct rndis_wlan_private *)dev->driver_priv;
552298e50adSKalle Valo }
553298e50adSKalle Valo 
get_bcm4320_power_dbm(struct rndis_wlan_private * priv)554298e50adSKalle Valo static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
555298e50adSKalle Valo {
556298e50adSKalle Valo 	switch (priv->param_power_output) {
557298e50adSKalle Valo 	default:
558298e50adSKalle Valo 	case 3:
559298e50adSKalle Valo 		return BCM4320_DEFAULT_TXPOWER_DBM_100;
560298e50adSKalle Valo 	case 2:
561298e50adSKalle Valo 		return BCM4320_DEFAULT_TXPOWER_DBM_75;
562298e50adSKalle Valo 	case 1:
563298e50adSKalle Valo 		return BCM4320_DEFAULT_TXPOWER_DBM_50;
564298e50adSKalle Valo 	case 0:
565298e50adSKalle Valo 		return BCM4320_DEFAULT_TXPOWER_DBM_25;
566298e50adSKalle Valo 	}
567298e50adSKalle Valo }
568298e50adSKalle Valo 
is_wpa_key(struct rndis_wlan_private * priv,u8 idx)569298e50adSKalle Valo static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
570298e50adSKalle Valo {
571298e50adSKalle Valo 	int cipher = priv->encr_keys[idx].cipher;
572298e50adSKalle Valo 
573298e50adSKalle Valo 	return (cipher == WLAN_CIPHER_SUITE_CCMP ||
574298e50adSKalle Valo 		cipher == WLAN_CIPHER_SUITE_TKIP);
575298e50adSKalle Valo }
576298e50adSKalle Valo 
rndis_cipher_to_alg(u32 cipher)577298e50adSKalle Valo static int rndis_cipher_to_alg(u32 cipher)
578298e50adSKalle Valo {
579298e50adSKalle Valo 	switch (cipher) {
580298e50adSKalle Valo 	default:
581298e50adSKalle Valo 		return RNDIS_WLAN_ALG_NONE;
582298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
583298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
584298e50adSKalle Valo 		return RNDIS_WLAN_ALG_WEP;
585298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
586298e50adSKalle Valo 		return RNDIS_WLAN_ALG_TKIP;
587298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
588298e50adSKalle Valo 		return RNDIS_WLAN_ALG_CCMP;
589298e50adSKalle Valo 	}
590298e50adSKalle Valo }
591298e50adSKalle Valo 
rndis_akm_suite_to_key_mgmt(u32 akm_suite)592298e50adSKalle Valo static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
593298e50adSKalle Valo {
594298e50adSKalle Valo 	switch (akm_suite) {
595298e50adSKalle Valo 	default:
596298e50adSKalle Valo 		return RNDIS_WLAN_KEY_MGMT_NONE;
597298e50adSKalle Valo 	case WLAN_AKM_SUITE_8021X:
598298e50adSKalle Valo 		return RNDIS_WLAN_KEY_MGMT_802_1X;
599298e50adSKalle Valo 	case WLAN_AKM_SUITE_PSK:
600298e50adSKalle Valo 		return RNDIS_WLAN_KEY_MGMT_PSK;
601298e50adSKalle Valo 	}
602298e50adSKalle Valo }
603298e50adSKalle Valo 
604298e50adSKalle Valo #ifdef DEBUG
oid_to_string(u32 oid)605298e50adSKalle Valo static const char *oid_to_string(u32 oid)
606298e50adSKalle Valo {
607298e50adSKalle Valo 	switch (oid) {
608298e50adSKalle Valo #define OID_STR(oid) case oid: return(#oid)
609298e50adSKalle Valo 		/* from rndis_host.h */
610298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
611298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
612298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
613298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
614298e50adSKalle Valo 
615298e50adSKalle Valo 		/* from rndis_wlan.c */
616298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_LINK_SPEED);
617298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
618298e50adSKalle Valo 
619298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_XMIT_OK);
620298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_RCV_OK);
621298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
622298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_RCV_ERROR);
623298e50adSKalle Valo 		OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
624298e50adSKalle Valo 
625298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
626298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
627298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
628298e50adSKalle Valo 
629298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_BSSID);
630298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_SSID);
631298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
632298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_ADD_WEP);
633298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
634298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
635298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
636298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
637298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
638298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
639298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_ADD_KEY);
640298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
641298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
642298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_CAPABILITY);
643298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_PMKID);
644298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
645298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
646298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
647298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_RSSI);
648298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
649298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
650298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
651298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
652298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_CONFIGURATION);
653298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_POWER_MODE);
654298e50adSKalle Valo 		OID_STR(RNDIS_OID_802_11_BSSID_LIST);
655298e50adSKalle Valo #undef OID_STR
656298e50adSKalle Valo 	}
657298e50adSKalle Valo 
658298e50adSKalle Valo 	return "?";
659298e50adSKalle Valo }
660298e50adSKalle Valo #else
oid_to_string(u32 oid)661298e50adSKalle Valo static const char *oid_to_string(u32 oid)
662298e50adSKalle Valo {
663298e50adSKalle Valo 	return "?";
664298e50adSKalle Valo }
665298e50adSKalle Valo #endif
666298e50adSKalle Valo 
667298e50adSKalle Valo /* translate error code */
rndis_error_status(__le32 rndis_status)668298e50adSKalle Valo static int rndis_error_status(__le32 rndis_status)
669298e50adSKalle Valo {
670298e50adSKalle Valo 	int ret = -EINVAL;
671298e50adSKalle Valo 	switch (le32_to_cpu(rndis_status)) {
672298e50adSKalle Valo 	case RNDIS_STATUS_SUCCESS:
673298e50adSKalle Valo 		ret = 0;
674298e50adSKalle Valo 		break;
675298e50adSKalle Valo 	case RNDIS_STATUS_FAILURE:
676298e50adSKalle Valo 	case RNDIS_STATUS_INVALID_DATA:
677298e50adSKalle Valo 		ret = -EINVAL;
678298e50adSKalle Valo 		break;
679298e50adSKalle Valo 	case RNDIS_STATUS_NOT_SUPPORTED:
680298e50adSKalle Valo 		ret = -EOPNOTSUPP;
681298e50adSKalle Valo 		break;
682298e50adSKalle Valo 	case RNDIS_STATUS_ADAPTER_NOT_READY:
683298e50adSKalle Valo 	case RNDIS_STATUS_ADAPTER_NOT_OPEN:
684298e50adSKalle Valo 		ret = -EBUSY;
685298e50adSKalle Valo 		break;
686298e50adSKalle Valo 	}
687298e50adSKalle Valo 	return ret;
688298e50adSKalle Valo }
689298e50adSKalle Valo 
rndis_query_oid(struct usbnet * dev,u32 oid,void * data,int * len)690298e50adSKalle Valo static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
691298e50adSKalle Valo {
692298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
693298e50adSKalle Valo 	union {
694298e50adSKalle Valo 		void			*buf;
695298e50adSKalle Valo 		struct rndis_msg_hdr	*header;
696298e50adSKalle Valo 		struct rndis_query	*get;
697298e50adSKalle Valo 		struct rndis_query_c	*get_c;
698298e50adSKalle Valo 	} u;
699298e50adSKalle Valo 	int ret;
700298e50adSKalle Valo 	size_t buflen, resplen, respoffs, copylen;
701298e50adSKalle Valo 
702298e50adSKalle Valo 	buflen = *len + sizeof(*u.get);
703298e50adSKalle Valo 	if (buflen < CONTROL_BUFFER_SIZE)
704298e50adSKalle Valo 		buflen = CONTROL_BUFFER_SIZE;
705298e50adSKalle Valo 
706298e50adSKalle Valo 	if (buflen > COMMAND_BUFFER_SIZE) {
707298e50adSKalle Valo 		u.buf = kmalloc(buflen, GFP_KERNEL);
708298e50adSKalle Valo 		if (!u.buf)
709298e50adSKalle Valo 			return -ENOMEM;
710298e50adSKalle Valo 	} else {
711298e50adSKalle Valo 		u.buf = priv->command_buffer;
712298e50adSKalle Valo 	}
713298e50adSKalle Valo 
714298e50adSKalle Valo 	mutex_lock(&priv->command_lock);
715298e50adSKalle Valo 
716298e50adSKalle Valo 	memset(u.get, 0, sizeof *u.get);
717298e50adSKalle Valo 	u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
718298e50adSKalle Valo 	u.get->msg_len = cpu_to_le32(sizeof *u.get);
719298e50adSKalle Valo 	u.get->oid = cpu_to_le32(oid);
720298e50adSKalle Valo 
721298e50adSKalle Valo 	priv->current_command_oid = oid;
722298e50adSKalle Valo 	ret = rndis_command(dev, u.header, buflen);
723298e50adSKalle Valo 	priv->current_command_oid = 0;
724298e50adSKalle Valo 	if (ret < 0)
725298e50adSKalle Valo 		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
726298e50adSKalle Valo 			   __func__, oid_to_string(oid), ret,
727298e50adSKalle Valo 			   le32_to_cpu(u.get_c->status));
728298e50adSKalle Valo 
729298e50adSKalle Valo 	if (ret == 0) {
730298e50adSKalle Valo 		resplen = le32_to_cpu(u.get_c->len);
731298e50adSKalle Valo 		respoffs = le32_to_cpu(u.get_c->offset) + 8;
732298e50adSKalle Valo 
733298e50adSKalle Valo 		if (respoffs > buflen) {
734298e50adSKalle Valo 			/* Device returned data offset outside buffer, error. */
735298e50adSKalle Valo 			netdev_dbg(dev->net,
736298e50adSKalle Valo 				   "%s(%s): received invalid data offset: %zu > %zu\n",
737298e50adSKalle Valo 				   __func__, oid_to_string(oid), respoffs, buflen);
738298e50adSKalle Valo 
739298e50adSKalle Valo 			ret = -EINVAL;
740298e50adSKalle Valo 			goto exit_unlock;
741298e50adSKalle Valo 		}
742298e50adSKalle Valo 
743298e50adSKalle Valo 		copylen = min(resplen, buflen - respoffs);
744298e50adSKalle Valo 
745298e50adSKalle Valo 		if (copylen > *len)
746298e50adSKalle Valo 			copylen = *len;
747298e50adSKalle Valo 
748298e50adSKalle Valo 		memcpy(data, u.buf + respoffs, copylen);
749298e50adSKalle Valo 
750298e50adSKalle Valo 		*len = resplen;
751298e50adSKalle Valo 
752298e50adSKalle Valo 		ret = rndis_error_status(u.get_c->status);
753298e50adSKalle Valo 		if (ret < 0)
754298e50adSKalle Valo 			netdev_dbg(dev->net, "%s(%s): device returned error,  0x%08x (%d)\n",
755298e50adSKalle Valo 				   __func__, oid_to_string(oid),
756298e50adSKalle Valo 				   le32_to_cpu(u.get_c->status), ret);
757298e50adSKalle Valo 	}
758298e50adSKalle Valo 
759298e50adSKalle Valo exit_unlock:
760298e50adSKalle Valo 	mutex_unlock(&priv->command_lock);
761298e50adSKalle Valo 
762298e50adSKalle Valo 	if (u.buf != priv->command_buffer)
763298e50adSKalle Valo 		kfree(u.buf);
764298e50adSKalle Valo 	return ret;
765298e50adSKalle Valo }
766298e50adSKalle Valo 
rndis_set_oid(struct usbnet * dev,u32 oid,const void * data,int len)767298e50adSKalle Valo static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
768298e50adSKalle Valo 			 int len)
769298e50adSKalle Valo {
770298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
771298e50adSKalle Valo 	union {
772298e50adSKalle Valo 		void			*buf;
773298e50adSKalle Valo 		struct rndis_msg_hdr	*header;
774298e50adSKalle Valo 		struct rndis_set	*set;
775298e50adSKalle Valo 		struct rndis_set_c	*set_c;
776298e50adSKalle Valo 	} u;
777298e50adSKalle Valo 	int ret, buflen;
778298e50adSKalle Valo 
779298e50adSKalle Valo 	buflen = len + sizeof(*u.set);
780298e50adSKalle Valo 	if (buflen < CONTROL_BUFFER_SIZE)
781298e50adSKalle Valo 		buflen = CONTROL_BUFFER_SIZE;
782298e50adSKalle Valo 
783298e50adSKalle Valo 	if (buflen > COMMAND_BUFFER_SIZE) {
784298e50adSKalle Valo 		u.buf = kmalloc(buflen, GFP_KERNEL);
785298e50adSKalle Valo 		if (!u.buf)
786298e50adSKalle Valo 			return -ENOMEM;
787298e50adSKalle Valo 	} else {
788298e50adSKalle Valo 		u.buf = priv->command_buffer;
789298e50adSKalle Valo 	}
790298e50adSKalle Valo 
791298e50adSKalle Valo 	mutex_lock(&priv->command_lock);
792298e50adSKalle Valo 
793298e50adSKalle Valo 	memset(u.set, 0, sizeof *u.set);
794298e50adSKalle Valo 	u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
795298e50adSKalle Valo 	u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
796298e50adSKalle Valo 	u.set->oid = cpu_to_le32(oid);
797298e50adSKalle Valo 	u.set->len = cpu_to_le32(len);
798298e50adSKalle Valo 	u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
799298e50adSKalle Valo 	u.set->handle = cpu_to_le32(0);
800298e50adSKalle Valo 	memcpy(u.buf + sizeof(*u.set), data, len);
801298e50adSKalle Valo 
802298e50adSKalle Valo 	priv->current_command_oid = oid;
803298e50adSKalle Valo 	ret = rndis_command(dev, u.header, buflen);
804298e50adSKalle Valo 	priv->current_command_oid = 0;
805298e50adSKalle Valo 	if (ret < 0)
806298e50adSKalle Valo 		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
807298e50adSKalle Valo 			   __func__, oid_to_string(oid), ret,
808298e50adSKalle Valo 			   le32_to_cpu(u.set_c->status));
809298e50adSKalle Valo 
810298e50adSKalle Valo 	if (ret == 0) {
811298e50adSKalle Valo 		ret = rndis_error_status(u.set_c->status);
812298e50adSKalle Valo 
813298e50adSKalle Valo 		if (ret < 0)
814298e50adSKalle Valo 			netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
815298e50adSKalle Valo 				   __func__, oid_to_string(oid),
816298e50adSKalle Valo 				   le32_to_cpu(u.set_c->status), ret);
817298e50adSKalle Valo 	}
818298e50adSKalle Valo 
819298e50adSKalle Valo 	mutex_unlock(&priv->command_lock);
820298e50adSKalle Valo 
821298e50adSKalle Valo 	if (u.buf != priv->command_buffer)
822298e50adSKalle Valo 		kfree(u.buf);
823298e50adSKalle Valo 	return ret;
824298e50adSKalle Valo }
825298e50adSKalle Valo 
rndis_reset(struct usbnet * usbdev)826298e50adSKalle Valo static int rndis_reset(struct usbnet *usbdev)
827298e50adSKalle Valo {
828298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
829298e50adSKalle Valo 	struct rndis_reset *reset;
830298e50adSKalle Valo 	int ret;
831298e50adSKalle Valo 
832298e50adSKalle Valo 	mutex_lock(&priv->command_lock);
833298e50adSKalle Valo 
834298e50adSKalle Valo 	reset = (void *)priv->command_buffer;
835298e50adSKalle Valo 	memset(reset, 0, sizeof(*reset));
836298e50adSKalle Valo 	reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
837298e50adSKalle Valo 	reset->msg_len = cpu_to_le32(sizeof(*reset));
838298e50adSKalle Valo 	priv->current_command_oid = 0;
839298e50adSKalle Valo 	ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
840298e50adSKalle Valo 
841298e50adSKalle Valo 	mutex_unlock(&priv->command_lock);
842298e50adSKalle Valo 
843298e50adSKalle Valo 	if (ret < 0)
844298e50adSKalle Valo 		return ret;
845298e50adSKalle Valo 	return 0;
846298e50adSKalle Valo }
847298e50adSKalle Valo 
848298e50adSKalle Valo /*
849298e50adSKalle Valo  * Specs say that we can only set config parameters only soon after device
850298e50adSKalle Valo  * initialization.
851298e50adSKalle Valo  *   value_type: 0 = u32, 2 = unicode string
852298e50adSKalle Valo  */
rndis_set_config_parameter(struct usbnet * dev,char * param,int value_type,void * value)853298e50adSKalle Valo static int rndis_set_config_parameter(struct usbnet *dev, char *param,
854298e50adSKalle Valo 						int value_type, void *value)
855298e50adSKalle Valo {
856298e50adSKalle Valo 	struct ndis_config_param *infobuf;
857298e50adSKalle Valo 	int value_len, info_len, param_len, ret, i;
858298e50adSKalle Valo 	__le16 *unibuf;
859298e50adSKalle Valo 	__le32 *dst_value;
860298e50adSKalle Valo 
861298e50adSKalle Valo 	if (value_type == 0)
862298e50adSKalle Valo 		value_len = sizeof(__le32);
863298e50adSKalle Valo 	else if (value_type == 2)
864298e50adSKalle Valo 		value_len = strlen(value) * sizeof(__le16);
865298e50adSKalle Valo 	else
866298e50adSKalle Valo 		return -EINVAL;
867298e50adSKalle Valo 
868298e50adSKalle Valo 	param_len = strlen(param) * sizeof(__le16);
869298e50adSKalle Valo 	info_len = sizeof(*infobuf) + param_len + value_len;
870298e50adSKalle Valo 
871298e50adSKalle Valo #ifdef DEBUG
872298e50adSKalle Valo 	info_len += 12;
873298e50adSKalle Valo #endif
874298e50adSKalle Valo 	infobuf = kmalloc(info_len, GFP_KERNEL);
875298e50adSKalle Valo 	if (!infobuf)
876298e50adSKalle Valo 		return -ENOMEM;
877298e50adSKalle Valo 
878298e50adSKalle Valo #ifdef DEBUG
879298e50adSKalle Valo 	info_len -= 12;
880298e50adSKalle Valo 	/* extra 12 bytes are for padding (debug output) */
881298e50adSKalle Valo 	memset(infobuf, 0xCC, info_len + 12);
882298e50adSKalle Valo #endif
883298e50adSKalle Valo 
884298e50adSKalle Valo 	if (value_type == 2)
885298e50adSKalle Valo 		netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
886298e50adSKalle Valo 			   param, (u8 *)value);
887298e50adSKalle Valo 	else
888298e50adSKalle Valo 		netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
889298e50adSKalle Valo 			   param, *(u32 *)value);
890298e50adSKalle Valo 
891298e50adSKalle Valo 	infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
892298e50adSKalle Valo 	infobuf->name_length = cpu_to_le32(param_len);
893298e50adSKalle Valo 	infobuf->type = cpu_to_le32(value_type);
894298e50adSKalle Valo 	infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
895298e50adSKalle Valo 	infobuf->value_length = cpu_to_le32(value_len);
896298e50adSKalle Valo 
897298e50adSKalle Valo 	/* simple string to unicode string conversion */
898298e50adSKalle Valo 	unibuf = (void *)infobuf + sizeof(*infobuf);
899298e50adSKalle Valo 	for (i = 0; i < param_len / sizeof(__le16); i++)
900298e50adSKalle Valo 		unibuf[i] = cpu_to_le16(param[i]);
901298e50adSKalle Valo 
902298e50adSKalle Valo 	if (value_type == 2) {
903298e50adSKalle Valo 		unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
904298e50adSKalle Valo 		for (i = 0; i < value_len / sizeof(__le16); i++)
905298e50adSKalle Valo 			unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
906298e50adSKalle Valo 	} else {
907298e50adSKalle Valo 		dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
908298e50adSKalle Valo 		*dst_value = cpu_to_le32(*(u32 *)value);
909298e50adSKalle Valo 	}
910298e50adSKalle Valo 
911298e50adSKalle Valo #ifdef DEBUG
912298e50adSKalle Valo 	netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
913298e50adSKalle Valo 	for (i = 0; i < info_len; i += 12) {
914298e50adSKalle Valo 		u32 *tmp = (u32 *)((u8 *)infobuf + i);
915298e50adSKalle Valo 		netdev_dbg(dev->net, "%08X:%08X:%08X\n",
916298e50adSKalle Valo 			   cpu_to_be32(tmp[0]),
917298e50adSKalle Valo 			   cpu_to_be32(tmp[1]),
918298e50adSKalle Valo 			   cpu_to_be32(tmp[2]));
919298e50adSKalle Valo 	}
920298e50adSKalle Valo #endif
921298e50adSKalle Valo 
922298e50adSKalle Valo 	ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
923298e50adSKalle Valo 							infobuf, info_len);
924298e50adSKalle Valo 	if (ret != 0)
925298e50adSKalle Valo 		netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
926298e50adSKalle Valo 			   ret);
927298e50adSKalle Valo 
928298e50adSKalle Valo 	kfree(infobuf);
929298e50adSKalle Valo 	return ret;
930298e50adSKalle Valo }
931298e50adSKalle Valo 
rndis_set_config_parameter_str(struct usbnet * dev,char * param,char * value)932298e50adSKalle Valo static int rndis_set_config_parameter_str(struct usbnet *dev,
933298e50adSKalle Valo 						char *param, char *value)
934298e50adSKalle Valo {
935298e50adSKalle Valo 	return rndis_set_config_parameter(dev, param, 2, value);
936298e50adSKalle Valo }
937298e50adSKalle Valo 
938298e50adSKalle Valo /*
939298e50adSKalle Valo  * data conversion functions
940298e50adSKalle Valo  */
level_to_qual(int level)941298e50adSKalle Valo static int level_to_qual(int level)
942298e50adSKalle Valo {
943298e50adSKalle Valo 	int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
944298e50adSKalle Valo 	return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
945298e50adSKalle Valo }
946298e50adSKalle Valo 
947298e50adSKalle Valo /*
948298e50adSKalle Valo  * common functions
949298e50adSKalle Valo  */
950298e50adSKalle Valo static int set_infra_mode(struct usbnet *usbdev, int mode);
951298e50adSKalle Valo static void restore_keys(struct usbnet *usbdev);
952298e50adSKalle Valo static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
953298e50adSKalle Valo 					bool *matched);
954298e50adSKalle Valo 
rndis_start_bssid_list_scan(struct usbnet * usbdev)955298e50adSKalle Valo static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
956298e50adSKalle Valo {
957298e50adSKalle Valo 	__le32 tmp;
958298e50adSKalle Valo 
959298e50adSKalle Valo 	/* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
960298e50adSKalle Valo 	tmp = cpu_to_le32(1);
961298e50adSKalle Valo 	return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
962298e50adSKalle Valo 							sizeof(tmp));
963298e50adSKalle Valo }
964298e50adSKalle Valo 
set_essid(struct usbnet * usbdev,struct ndis_80211_ssid * ssid)965298e50adSKalle Valo static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
966298e50adSKalle Valo {
967298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
968298e50adSKalle Valo 	int ret;
969298e50adSKalle Valo 
970298e50adSKalle Valo 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
971298e50adSKalle Valo 			    ssid, sizeof(*ssid));
972298e50adSKalle Valo 	if (ret < 0) {
973298e50adSKalle Valo 		netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
974298e50adSKalle Valo 		return ret;
975298e50adSKalle Valo 	}
976298e50adSKalle Valo 	if (ret == 0) {
977298e50adSKalle Valo 		priv->radio_on = true;
978298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
979298e50adSKalle Valo 	}
980298e50adSKalle Valo 
981298e50adSKalle Valo 	return ret;
982298e50adSKalle Valo }
983298e50adSKalle Valo 
set_bssid(struct usbnet * usbdev,const u8 * bssid)984298e50adSKalle Valo static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
985298e50adSKalle Valo {
986298e50adSKalle Valo 	int ret;
987298e50adSKalle Valo 
988298e50adSKalle Valo 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
989298e50adSKalle Valo 			    bssid, ETH_ALEN);
990298e50adSKalle Valo 	if (ret < 0) {
991298e50adSKalle Valo 		netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
992298e50adSKalle Valo 			    bssid, ret);
993298e50adSKalle Valo 		return ret;
994298e50adSKalle Valo 	}
995298e50adSKalle Valo 
996298e50adSKalle Valo 	return ret;
997298e50adSKalle Valo }
998298e50adSKalle Valo 
clear_bssid(struct usbnet * usbdev)999298e50adSKalle Valo static int clear_bssid(struct usbnet *usbdev)
1000298e50adSKalle Valo {
1001298e50adSKalle Valo 	static const u8 broadcast_mac[ETH_ALEN] = {
1002298e50adSKalle Valo 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1003298e50adSKalle Valo 	};
1004298e50adSKalle Valo 
1005298e50adSKalle Valo 	return set_bssid(usbdev, broadcast_mac);
1006298e50adSKalle Valo }
1007298e50adSKalle Valo 
get_bssid(struct usbnet * usbdev,u8 bssid[ETH_ALEN])1008298e50adSKalle Valo static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1009298e50adSKalle Valo {
1010298e50adSKalle Valo 	int ret, len;
1011298e50adSKalle Valo 
1012298e50adSKalle Valo 	len = ETH_ALEN;
1013298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
1014298e50adSKalle Valo 			      bssid, &len);
1015298e50adSKalle Valo 
1016298e50adSKalle Valo 	if (ret != 0)
1017298e50adSKalle Valo 		eth_zero_addr(bssid);
1018298e50adSKalle Valo 
1019298e50adSKalle Valo 	return ret;
1020298e50adSKalle Valo }
1021298e50adSKalle Valo 
get_association_info(struct usbnet * usbdev,struct ndis_80211_assoc_info * info,int len)1022298e50adSKalle Valo static int get_association_info(struct usbnet *usbdev,
1023298e50adSKalle Valo 			struct ndis_80211_assoc_info *info, int len)
1024298e50adSKalle Valo {
1025298e50adSKalle Valo 	return rndis_query_oid(usbdev,
1026298e50adSKalle Valo 			RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
1027298e50adSKalle Valo 			info, &len);
1028298e50adSKalle Valo }
1029298e50adSKalle Valo 
is_associated(struct usbnet * usbdev)1030298e50adSKalle Valo static bool is_associated(struct usbnet *usbdev)
1031298e50adSKalle Valo {
1032298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1033298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
1034298e50adSKalle Valo 
1035298e50adSKalle Valo 	if (!priv->radio_on)
1036298e50adSKalle Valo 		return false;
1037298e50adSKalle Valo 
1038298e50adSKalle Valo 	return (get_bssid(usbdev, bssid) == 0 && !is_zero_ether_addr(bssid));
1039298e50adSKalle Valo }
1040298e50adSKalle Valo 
disassociate(struct usbnet * usbdev,bool reset_ssid)1041298e50adSKalle Valo static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1042298e50adSKalle Valo {
1043298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1044298e50adSKalle Valo 	struct ndis_80211_ssid ssid;
1045298e50adSKalle Valo 	int i, ret = 0;
1046298e50adSKalle Valo 
1047298e50adSKalle Valo 	if (priv->radio_on) {
1048298e50adSKalle Valo 		ret = rndis_set_oid(usbdev,
1049298e50adSKalle Valo 				RNDIS_OID_802_11_DISASSOCIATE,
1050298e50adSKalle Valo 				NULL, 0);
1051298e50adSKalle Valo 		if (ret == 0) {
1052298e50adSKalle Valo 			priv->radio_on = false;
1053298e50adSKalle Valo 			netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1054298e50adSKalle Valo 				   __func__);
1055298e50adSKalle Valo 
1056298e50adSKalle Valo 			if (reset_ssid)
1057298e50adSKalle Valo 				msleep(100);
1058298e50adSKalle Valo 		}
1059298e50adSKalle Valo 	}
1060298e50adSKalle Valo 
1061298e50adSKalle Valo 	/* disassociate causes radio to be turned off; if reset_ssid
1062298e50adSKalle Valo 	 * is given, set random ssid to enable radio */
1063298e50adSKalle Valo 	if (reset_ssid) {
1064298e50adSKalle Valo 		/* Set device to infrastructure mode so we don't get ad-hoc
1065298e50adSKalle Valo 		 * 'media connect' indications with the random ssid.
1066298e50adSKalle Valo 		 */
1067298e50adSKalle Valo 		set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1068298e50adSKalle Valo 
1069298e50adSKalle Valo 		ssid.length = cpu_to_le32(sizeof(ssid.essid));
1070298e50adSKalle Valo 		get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1071298e50adSKalle Valo 		ssid.essid[0] = 0x1;
1072298e50adSKalle Valo 		ssid.essid[1] = 0xff;
1073298e50adSKalle Valo 		for (i = 2; i < sizeof(ssid.essid); i++)
1074298e50adSKalle Valo 			ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1075298e50adSKalle Valo 		ret = set_essid(usbdev, &ssid);
1076298e50adSKalle Valo 	}
1077298e50adSKalle Valo 	return ret;
1078298e50adSKalle Valo }
1079298e50adSKalle Valo 
set_auth_mode(struct usbnet * usbdev,u32 wpa_version,enum nl80211_auth_type auth_type,int keymgmt)1080298e50adSKalle Valo static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1081298e50adSKalle Valo 				enum nl80211_auth_type auth_type, int keymgmt)
1082298e50adSKalle Valo {
1083298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1084298e50adSKalle Valo 	__le32 tmp;
1085298e50adSKalle Valo 	int auth_mode, ret;
1086298e50adSKalle Valo 
1087298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1088298e50adSKalle Valo 		   __func__, wpa_version, auth_type, keymgmt);
1089298e50adSKalle Valo 
1090298e50adSKalle Valo 	if (wpa_version & NL80211_WPA_VERSION_2) {
1091298e50adSKalle Valo 		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1092298e50adSKalle Valo 			auth_mode = NDIS_80211_AUTH_WPA2;
1093298e50adSKalle Valo 		else
1094298e50adSKalle Valo 			auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1095298e50adSKalle Valo 	} else if (wpa_version & NL80211_WPA_VERSION_1) {
1096298e50adSKalle Valo 		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1097298e50adSKalle Valo 			auth_mode = NDIS_80211_AUTH_WPA;
1098298e50adSKalle Valo 		else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1099298e50adSKalle Valo 			auth_mode = NDIS_80211_AUTH_WPA_PSK;
1100298e50adSKalle Valo 		else
1101298e50adSKalle Valo 			auth_mode = NDIS_80211_AUTH_WPA_NONE;
1102298e50adSKalle Valo 	} else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1103298e50adSKalle Valo 		auth_mode = NDIS_80211_AUTH_SHARED;
1104298e50adSKalle Valo 	else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1105298e50adSKalle Valo 		auth_mode = NDIS_80211_AUTH_OPEN;
1106298e50adSKalle Valo 	else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1107298e50adSKalle Valo 		auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1108298e50adSKalle Valo 	else
1109298e50adSKalle Valo 		return -ENOTSUPP;
1110298e50adSKalle Valo 
1111298e50adSKalle Valo 	tmp = cpu_to_le32(auth_mode);
1112298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1113298e50adSKalle Valo 			    RNDIS_OID_802_11_AUTHENTICATION_MODE,
1114298e50adSKalle Valo 			    &tmp, sizeof(tmp));
1115298e50adSKalle Valo 	if (ret != 0) {
1116298e50adSKalle Valo 		netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1117298e50adSKalle Valo 			    ret);
1118298e50adSKalle Valo 		return ret;
1119298e50adSKalle Valo 	}
1120298e50adSKalle Valo 
1121298e50adSKalle Valo 	priv->wpa_version = wpa_version;
1122298e50adSKalle Valo 
1123298e50adSKalle Valo 	return 0;
1124298e50adSKalle Valo }
1125298e50adSKalle Valo 
set_priv_filter(struct usbnet * usbdev)1126298e50adSKalle Valo static int set_priv_filter(struct usbnet *usbdev)
1127298e50adSKalle Valo {
1128298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1129298e50adSKalle Valo 	__le32 tmp;
1130298e50adSKalle Valo 
1131298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1132298e50adSKalle Valo 		   __func__, priv->wpa_version);
1133298e50adSKalle Valo 
1134298e50adSKalle Valo 	if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1135298e50adSKalle Valo 	    priv->wpa_version & NL80211_WPA_VERSION_1)
1136298e50adSKalle Valo 		tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1137298e50adSKalle Valo 	else
1138298e50adSKalle Valo 		tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1139298e50adSKalle Valo 
1140298e50adSKalle Valo 	return rndis_set_oid(usbdev,
1141298e50adSKalle Valo 			     RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
1142298e50adSKalle Valo 			     sizeof(tmp));
1143298e50adSKalle Valo }
1144298e50adSKalle Valo 
set_encr_mode(struct usbnet * usbdev,int pairwise,int groupwise)1145298e50adSKalle Valo static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1146298e50adSKalle Valo {
1147298e50adSKalle Valo 	__le32 tmp;
1148298e50adSKalle Valo 	int encr_mode, ret;
1149298e50adSKalle Valo 
1150298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1151298e50adSKalle Valo 		   __func__, pairwise, groupwise);
1152298e50adSKalle Valo 
1153298e50adSKalle Valo 	if (pairwise & RNDIS_WLAN_ALG_CCMP)
1154298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1155298e50adSKalle Valo 	else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1156298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1157298e50adSKalle Valo 	else if (pairwise & RNDIS_WLAN_ALG_WEP)
1158298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1159298e50adSKalle Valo 	else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1160298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1161298e50adSKalle Valo 	else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1162298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1163298e50adSKalle Valo 	else
1164298e50adSKalle Valo 		encr_mode = NDIS_80211_ENCR_DISABLED;
1165298e50adSKalle Valo 
1166298e50adSKalle Valo 	tmp = cpu_to_le32(encr_mode);
1167298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1168298e50adSKalle Valo 			RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
1169298e50adSKalle Valo 			sizeof(tmp));
1170298e50adSKalle Valo 	if (ret != 0) {
1171298e50adSKalle Valo 		netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1172298e50adSKalle Valo 			    ret);
1173298e50adSKalle Valo 		return ret;
1174298e50adSKalle Valo 	}
1175298e50adSKalle Valo 
1176298e50adSKalle Valo 	return 0;
1177298e50adSKalle Valo }
1178298e50adSKalle Valo 
set_infra_mode(struct usbnet * usbdev,int mode)1179298e50adSKalle Valo static int set_infra_mode(struct usbnet *usbdev, int mode)
1180298e50adSKalle Valo {
1181298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1182298e50adSKalle Valo 	__le32 tmp;
1183298e50adSKalle Valo 	int ret;
1184298e50adSKalle Valo 
1185298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1186298e50adSKalle Valo 		   __func__, priv->infra_mode);
1187298e50adSKalle Valo 
1188298e50adSKalle Valo 	tmp = cpu_to_le32(mode);
1189298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1190298e50adSKalle Valo 			    RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
1191298e50adSKalle Valo 			    &tmp, sizeof(tmp));
1192298e50adSKalle Valo 	if (ret != 0) {
1193298e50adSKalle Valo 		netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1194298e50adSKalle Valo 			    ret);
1195298e50adSKalle Valo 		return ret;
1196298e50adSKalle Valo 	}
1197298e50adSKalle Valo 
1198298e50adSKalle Valo 	/* NDIS drivers clear keys when infrastructure mode is
1199298e50adSKalle Valo 	 * changed. But Linux tools assume otherwise. So set the
1200298e50adSKalle Valo 	 * keys */
1201298e50adSKalle Valo 	restore_keys(usbdev);
1202298e50adSKalle Valo 
1203298e50adSKalle Valo 	priv->infra_mode = mode;
1204298e50adSKalle Valo 	return 0;
1205298e50adSKalle Valo }
1206298e50adSKalle Valo 
set_rts_threshold(struct usbnet * usbdev,u32 rts_threshold)1207298e50adSKalle Valo static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1208298e50adSKalle Valo {
1209298e50adSKalle Valo 	__le32 tmp;
1210298e50adSKalle Valo 
1211298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1212298e50adSKalle Valo 
1213298e50adSKalle Valo 	if (rts_threshold == -1 || rts_threshold > 2347)
1214298e50adSKalle Valo 		rts_threshold = 2347;
1215298e50adSKalle Valo 
1216298e50adSKalle Valo 	tmp = cpu_to_le32(rts_threshold);
1217298e50adSKalle Valo 	return rndis_set_oid(usbdev,
1218298e50adSKalle Valo 			     RNDIS_OID_802_11_RTS_THRESHOLD,
1219298e50adSKalle Valo 			     &tmp, sizeof(tmp));
1220298e50adSKalle Valo }
1221298e50adSKalle Valo 
set_frag_threshold(struct usbnet * usbdev,u32 frag_threshold)1222298e50adSKalle Valo static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1223298e50adSKalle Valo {
1224298e50adSKalle Valo 	__le32 tmp;
1225298e50adSKalle Valo 
1226298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1227298e50adSKalle Valo 
1228298e50adSKalle Valo 	if (frag_threshold < 256 || frag_threshold > 2346)
1229298e50adSKalle Valo 		frag_threshold = 2346;
1230298e50adSKalle Valo 
1231298e50adSKalle Valo 	tmp = cpu_to_le32(frag_threshold);
1232298e50adSKalle Valo 	return rndis_set_oid(usbdev,
1233298e50adSKalle Valo 			RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
1234298e50adSKalle Valo 			&tmp, sizeof(tmp));
1235298e50adSKalle Valo }
1236298e50adSKalle Valo 
set_default_iw_params(struct usbnet * usbdev)1237298e50adSKalle Valo static void set_default_iw_params(struct usbnet *usbdev)
1238298e50adSKalle Valo {
1239298e50adSKalle Valo 	set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1240298e50adSKalle Valo 	set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1241298e50adSKalle Valo 						RNDIS_WLAN_KEY_MGMT_NONE);
1242298e50adSKalle Valo 	set_priv_filter(usbdev);
1243298e50adSKalle Valo 	set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1244298e50adSKalle Valo }
1245298e50adSKalle Valo 
deauthenticate(struct usbnet * usbdev)1246298e50adSKalle Valo static int deauthenticate(struct usbnet *usbdev)
1247298e50adSKalle Valo {
1248298e50adSKalle Valo 	int ret;
1249298e50adSKalle Valo 
1250298e50adSKalle Valo 	ret = disassociate(usbdev, true);
1251298e50adSKalle Valo 	set_default_iw_params(usbdev);
1252298e50adSKalle Valo 	return ret;
1253298e50adSKalle Valo }
1254298e50adSKalle Valo 
set_channel(struct usbnet * usbdev,int channel)1255298e50adSKalle Valo static int set_channel(struct usbnet *usbdev, int channel)
1256298e50adSKalle Valo {
1257298e50adSKalle Valo 	struct ndis_80211_conf config;
1258298e50adSKalle Valo 	unsigned int dsconfig;
1259298e50adSKalle Valo 	int len, ret;
1260298e50adSKalle Valo 
1261298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1262298e50adSKalle Valo 
1263298e50adSKalle Valo 	/* this OID is valid only when not associated */
1264298e50adSKalle Valo 	if (is_associated(usbdev))
1265298e50adSKalle Valo 		return 0;
1266298e50adSKalle Valo 
1267298e50adSKalle Valo 	dsconfig = 1000 *
1268298e50adSKalle Valo 		ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1269298e50adSKalle Valo 
1270298e50adSKalle Valo 	len = sizeof(config);
1271298e50adSKalle Valo 	ret = rndis_query_oid(usbdev,
1272298e50adSKalle Valo 			RNDIS_OID_802_11_CONFIGURATION,
1273298e50adSKalle Valo 			&config, &len);
1274298e50adSKalle Valo 	if (ret < 0) {
1275298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1276298e50adSKalle Valo 			   __func__);
1277298e50adSKalle Valo 		return ret;
1278298e50adSKalle Valo 	}
1279298e50adSKalle Valo 
1280298e50adSKalle Valo 	config.ds_config = cpu_to_le32(dsconfig);
1281298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1282298e50adSKalle Valo 			RNDIS_OID_802_11_CONFIGURATION,
1283298e50adSKalle Valo 			&config, sizeof(config));
1284298e50adSKalle Valo 
1285298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1286298e50adSKalle Valo 
1287298e50adSKalle Valo 	return ret;
1288298e50adSKalle Valo }
1289298e50adSKalle Valo 
get_current_channel(struct usbnet * usbdev,u32 * beacon_period)1290298e50adSKalle Valo static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
1291298e50adSKalle Valo 						     u32 *beacon_period)
1292298e50adSKalle Valo {
1293298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1294298e50adSKalle Valo 	struct ieee80211_channel *channel;
1295298e50adSKalle Valo 	struct ndis_80211_conf config;
1296298e50adSKalle Valo 	int len, ret;
1297298e50adSKalle Valo 
1298298e50adSKalle Valo 	/* Get channel and beacon interval */
1299298e50adSKalle Valo 	len = sizeof(config);
1300298e50adSKalle Valo 	ret = rndis_query_oid(usbdev,
1301298e50adSKalle Valo 			RNDIS_OID_802_11_CONFIGURATION,
1302298e50adSKalle Valo 			&config, &len);
1303298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
1304298e50adSKalle Valo 				__func__, ret);
1305298e50adSKalle Valo 	if (ret < 0)
1306298e50adSKalle Valo 		return NULL;
1307298e50adSKalle Valo 
1308298e50adSKalle Valo 	channel = ieee80211_get_channel(priv->wdev.wiphy,
1309298e50adSKalle Valo 				KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
1310298e50adSKalle Valo 	if (!channel)
1311298e50adSKalle Valo 		return NULL;
1312298e50adSKalle Valo 
1313298e50adSKalle Valo 	if (beacon_period)
1314298e50adSKalle Valo 		*beacon_period = le32_to_cpu(config.beacon_period);
1315298e50adSKalle Valo 	return channel;
1316298e50adSKalle Valo }
1317298e50adSKalle Valo 
1318298e50adSKalle Valo /* index must be 0 - N, as per NDIS  */
add_wep_key(struct usbnet * usbdev,const u8 * key,int key_len,u8 index)1319298e50adSKalle Valo static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1320298e50adSKalle Valo 								u8 index)
1321298e50adSKalle Valo {
1322298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1323298e50adSKalle Valo 	struct ndis_80211_wep_key ndis_key;
1324298e50adSKalle Valo 	u32 cipher;
1325298e50adSKalle Valo 	int ret;
1326298e50adSKalle Valo 
1327298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1328298e50adSKalle Valo 		   __func__, index, key_len);
1329298e50adSKalle Valo 
1330298e50adSKalle Valo 	if (index >= RNDIS_WLAN_NUM_KEYS)
1331298e50adSKalle Valo 		return -EINVAL;
1332298e50adSKalle Valo 
1333298e50adSKalle Valo 	if (key_len == 5)
1334298e50adSKalle Valo 		cipher = WLAN_CIPHER_SUITE_WEP40;
1335298e50adSKalle Valo 	else if (key_len == 13)
1336298e50adSKalle Valo 		cipher = WLAN_CIPHER_SUITE_WEP104;
1337298e50adSKalle Valo 	else
1338298e50adSKalle Valo 		return -EINVAL;
1339298e50adSKalle Valo 
1340298e50adSKalle Valo 	memset(&ndis_key, 0, sizeof(ndis_key));
1341298e50adSKalle Valo 
1342298e50adSKalle Valo 	ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1343298e50adSKalle Valo 	ndis_key.length = cpu_to_le32(key_len);
1344298e50adSKalle Valo 	ndis_key.index = cpu_to_le32(index);
1345298e50adSKalle Valo 	memcpy(&ndis_key.material, key, key_len);
1346298e50adSKalle Valo 
1347298e50adSKalle Valo 	if (index == priv->encr_tx_key_index) {
1348298e50adSKalle Valo 		ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1349298e50adSKalle Valo 		ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1350298e50adSKalle Valo 							RNDIS_WLAN_ALG_NONE);
1351298e50adSKalle Valo 		if (ret)
1352298e50adSKalle Valo 			netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1353298e50adSKalle Valo 				    ret);
1354298e50adSKalle Valo 	}
1355298e50adSKalle Valo 
1356298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1357298e50adSKalle Valo 			RNDIS_OID_802_11_ADD_WEP, &ndis_key,
1358298e50adSKalle Valo 			sizeof(ndis_key));
1359298e50adSKalle Valo 	if (ret != 0) {
1360298e50adSKalle Valo 		netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1361298e50adSKalle Valo 			    index + 1, ret);
1362298e50adSKalle Valo 		return ret;
1363298e50adSKalle Valo 	}
1364298e50adSKalle Valo 
1365298e50adSKalle Valo 	priv->encr_keys[index].len = key_len;
1366298e50adSKalle Valo 	priv->encr_keys[index].cipher = cipher;
1367298e50adSKalle Valo 	memcpy(&priv->encr_keys[index].material, key, key_len);
1368298e50adSKalle Valo 	eth_broadcast_addr(priv->encr_keys[index].bssid);
1369298e50adSKalle Valo 
1370298e50adSKalle Valo 	return 0;
1371298e50adSKalle Valo }
1372298e50adSKalle Valo 
add_wpa_key(struct usbnet * usbdev,const u8 * key,int key_len,u8 index,const u8 * addr,const u8 * rx_seq,int seq_len,u32 cipher,__le32 flags)1373298e50adSKalle Valo static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1374298e50adSKalle Valo 			u8 index, const u8 *addr, const u8 *rx_seq,
1375298e50adSKalle Valo 			int seq_len, u32 cipher, __le32 flags)
1376298e50adSKalle Valo {
1377298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1378298e50adSKalle Valo 	struct ndis_80211_key ndis_key;
1379298e50adSKalle Valo 	bool is_addr_ok;
1380298e50adSKalle Valo 	int ret;
1381298e50adSKalle Valo 
1382298e50adSKalle Valo 	if (index >= RNDIS_WLAN_NUM_KEYS) {
1383298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1384298e50adSKalle Valo 			   __func__, index);
1385298e50adSKalle Valo 		return -EINVAL;
1386298e50adSKalle Valo 	}
1387298e50adSKalle Valo 	if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1388298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1389298e50adSKalle Valo 			   __func__, key_len);
1390298e50adSKalle Valo 		return -EINVAL;
1391298e50adSKalle Valo 	}
1392298e50adSKalle Valo 	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1393298e50adSKalle Valo 		if (!rx_seq || seq_len <= 0) {
1394298e50adSKalle Valo 			netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1395298e50adSKalle Valo 				   __func__);
1396298e50adSKalle Valo 			return -EINVAL;
1397298e50adSKalle Valo 		}
1398298e50adSKalle Valo 		if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1399298e50adSKalle Valo 			netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1400298e50adSKalle Valo 			return -EINVAL;
1401298e50adSKalle Valo 		}
1402298e50adSKalle Valo 	}
1403298e50adSKalle Valo 
1404298e50adSKalle Valo 	is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1405298e50adSKalle Valo 					!is_broadcast_ether_addr(addr);
1406298e50adSKalle Valo 	if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1407298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1408298e50adSKalle Valo 			   __func__, addr);
1409298e50adSKalle Valo 		return -EINVAL;
1410298e50adSKalle Valo 	}
1411298e50adSKalle Valo 
1412298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1413298e50adSKalle Valo 		   __func__, index,
1414298e50adSKalle Valo 		   !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1415298e50adSKalle Valo 		   !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1416298e50adSKalle Valo 		   !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1417298e50adSKalle Valo 
1418298e50adSKalle Valo 	memset(&ndis_key, 0, sizeof(ndis_key));
1419298e50adSKalle Valo 
1420298e50adSKalle Valo 	ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1421298e50adSKalle Valo 				sizeof(ndis_key.material) + key_len);
1422298e50adSKalle Valo 	ndis_key.length = cpu_to_le32(key_len);
1423298e50adSKalle Valo 	ndis_key.index = cpu_to_le32(index) | flags;
1424298e50adSKalle Valo 
1425298e50adSKalle Valo 	if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1426298e50adSKalle Valo 		/* wpa_supplicant gives us the Michael MIC RX/TX keys in
1427298e50adSKalle Valo 		 * different order than NDIS spec, so swap the order here. */
1428298e50adSKalle Valo 		memcpy(ndis_key.material, key, 16);
1429298e50adSKalle Valo 		memcpy(ndis_key.material + 16, key + 24, 8);
1430298e50adSKalle Valo 		memcpy(ndis_key.material + 24, key + 16, 8);
1431298e50adSKalle Valo 	} else
1432298e50adSKalle Valo 		memcpy(ndis_key.material, key, key_len);
1433298e50adSKalle Valo 
1434298e50adSKalle Valo 	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1435298e50adSKalle Valo 		memcpy(ndis_key.rsc, rx_seq, seq_len);
1436298e50adSKalle Valo 
1437298e50adSKalle Valo 	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1438298e50adSKalle Valo 		/* pairwise key */
1439298e50adSKalle Valo 		memcpy(ndis_key.bssid, addr, ETH_ALEN);
1440298e50adSKalle Valo 	} else {
1441298e50adSKalle Valo 		/* group key */
1442298e50adSKalle Valo 		if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1443298e50adSKalle Valo 			eth_broadcast_addr(ndis_key.bssid);
1444298e50adSKalle Valo 		else
1445298e50adSKalle Valo 			get_bssid(usbdev, ndis_key.bssid);
1446298e50adSKalle Valo 	}
1447298e50adSKalle Valo 
1448298e50adSKalle Valo 	ret = rndis_set_oid(usbdev,
1449298e50adSKalle Valo 			RNDIS_OID_802_11_ADD_KEY, &ndis_key,
1450298e50adSKalle Valo 			le32_to_cpu(ndis_key.size));
1451298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
1452298e50adSKalle Valo 		   __func__, ret);
1453298e50adSKalle Valo 	if (ret != 0)
1454298e50adSKalle Valo 		return ret;
1455298e50adSKalle Valo 
1456298e50adSKalle Valo 	memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1457298e50adSKalle Valo 	priv->encr_keys[index].len = key_len;
1458298e50adSKalle Valo 	priv->encr_keys[index].cipher = cipher;
1459298e50adSKalle Valo 	memcpy(&priv->encr_keys[index].material, key, key_len);
1460298e50adSKalle Valo 	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1461298e50adSKalle Valo 		memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1462298e50adSKalle Valo 	else
1463298e50adSKalle Valo 		eth_broadcast_addr(priv->encr_keys[index].bssid);
1464298e50adSKalle Valo 
1465298e50adSKalle Valo 	if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1466298e50adSKalle Valo 		priv->encr_tx_key_index = index;
1467298e50adSKalle Valo 
1468298e50adSKalle Valo 	return 0;
1469298e50adSKalle Valo }
1470298e50adSKalle Valo 
restore_key(struct usbnet * usbdev,u8 key_idx)1471298e50adSKalle Valo static int restore_key(struct usbnet *usbdev, u8 key_idx)
1472298e50adSKalle Valo {
1473298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1474298e50adSKalle Valo 	struct rndis_wlan_encr_key key;
1475298e50adSKalle Valo 
1476298e50adSKalle Valo 	if (is_wpa_key(priv, key_idx))
1477298e50adSKalle Valo 		return 0;
1478298e50adSKalle Valo 
1479298e50adSKalle Valo 	key = priv->encr_keys[key_idx];
1480298e50adSKalle Valo 
1481298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1482298e50adSKalle Valo 
1483298e50adSKalle Valo 	if (key.len == 0)
1484298e50adSKalle Valo 		return 0;
1485298e50adSKalle Valo 
1486298e50adSKalle Valo 	return add_wep_key(usbdev, key.material, key.len, key_idx);
1487298e50adSKalle Valo }
1488298e50adSKalle Valo 
restore_keys(struct usbnet * usbdev)1489298e50adSKalle Valo static void restore_keys(struct usbnet *usbdev)
1490298e50adSKalle Valo {
1491298e50adSKalle Valo 	int i;
1492298e50adSKalle Valo 
1493298e50adSKalle Valo 	for (i = 0; i < 4; i++)
1494298e50adSKalle Valo 		restore_key(usbdev, i);
1495298e50adSKalle Valo }
1496298e50adSKalle Valo 
clear_key(struct rndis_wlan_private * priv,u8 idx)1497298e50adSKalle Valo static void clear_key(struct rndis_wlan_private *priv, u8 idx)
1498298e50adSKalle Valo {
1499298e50adSKalle Valo 	memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1500298e50adSKalle Valo }
1501298e50adSKalle Valo 
1502298e50adSKalle Valo /* remove_key is for both wep and wpa */
remove_key(struct usbnet * usbdev,u8 index,const u8 * bssid)1503298e50adSKalle Valo static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
1504298e50adSKalle Valo {
1505298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1506298e50adSKalle Valo 	struct ndis_80211_remove_key remove_key;
1507298e50adSKalle Valo 	__le32 keyindex;
1508298e50adSKalle Valo 	bool is_wpa;
1509298e50adSKalle Valo 	int ret;
1510298e50adSKalle Valo 
1511298e50adSKalle Valo 	if (index >= RNDIS_WLAN_NUM_KEYS)
1512298e50adSKalle Valo 		return -ENOENT;
1513298e50adSKalle Valo 
1514298e50adSKalle Valo 	if (priv->encr_keys[index].len == 0)
1515298e50adSKalle Valo 		return 0;
1516298e50adSKalle Valo 
1517298e50adSKalle Valo 	is_wpa = is_wpa_key(priv, index);
1518298e50adSKalle Valo 
1519298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1520298e50adSKalle Valo 		   __func__, index, is_wpa ? "wpa" : "wep",
1521298e50adSKalle Valo 		   priv->encr_keys[index].len);
1522298e50adSKalle Valo 
1523298e50adSKalle Valo 	clear_key(priv, index);
1524298e50adSKalle Valo 
1525298e50adSKalle Valo 	if (is_wpa) {
1526298e50adSKalle Valo 		remove_key.size = cpu_to_le32(sizeof(remove_key));
1527298e50adSKalle Valo 		remove_key.index = cpu_to_le32(index);
1528298e50adSKalle Valo 		if (bssid) {
1529298e50adSKalle Valo 			/* pairwise key */
1530298e50adSKalle Valo 			if (!is_broadcast_ether_addr(bssid))
1531298e50adSKalle Valo 				remove_key.index |=
1532298e50adSKalle Valo 					NDIS_80211_ADDKEY_PAIRWISE_KEY;
1533298e50adSKalle Valo 			memcpy(remove_key.bssid, bssid,
1534298e50adSKalle Valo 					sizeof(remove_key.bssid));
1535298e50adSKalle Valo 		} else
1536298e50adSKalle Valo 			memset(remove_key.bssid, 0xff,
1537298e50adSKalle Valo 						sizeof(remove_key.bssid));
1538298e50adSKalle Valo 
1539298e50adSKalle Valo 		ret = rndis_set_oid(usbdev,
1540298e50adSKalle Valo 				RNDIS_OID_802_11_REMOVE_KEY,
1541298e50adSKalle Valo 				&remove_key, sizeof(remove_key));
1542298e50adSKalle Valo 		if (ret != 0)
1543298e50adSKalle Valo 			return ret;
1544298e50adSKalle Valo 	} else {
1545298e50adSKalle Valo 		keyindex = cpu_to_le32(index);
1546298e50adSKalle Valo 		ret = rndis_set_oid(usbdev,
1547298e50adSKalle Valo 				RNDIS_OID_802_11_REMOVE_WEP,
1548298e50adSKalle Valo 				&keyindex, sizeof(keyindex));
1549298e50adSKalle Valo 		if (ret != 0) {
1550298e50adSKalle Valo 			netdev_warn(usbdev->net,
1551298e50adSKalle Valo 				    "removing encryption key %d failed (%08X)\n",
1552298e50adSKalle Valo 				    index, ret);
1553298e50adSKalle Valo 			return ret;
1554298e50adSKalle Valo 		}
1555298e50adSKalle Valo 	}
1556298e50adSKalle Valo 
1557298e50adSKalle Valo 	/* if it is transmit key, disable encryption */
1558298e50adSKalle Valo 	if (index == priv->encr_tx_key_index)
1559298e50adSKalle Valo 		set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1560298e50adSKalle Valo 
1561298e50adSKalle Valo 	return 0;
1562298e50adSKalle Valo }
1563298e50adSKalle Valo 
set_multicast_list(struct usbnet * usbdev)1564298e50adSKalle Valo static void set_multicast_list(struct usbnet *usbdev)
1565298e50adSKalle Valo {
1566298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1567298e50adSKalle Valo 	struct netdev_hw_addr *ha;
1568298e50adSKalle Valo 	__le32 filter, basefilter;
1569298e50adSKalle Valo 	int ret;
1570298e50adSKalle Valo 	char *mc_addrs = NULL;
1571298e50adSKalle Valo 	int mc_count;
1572298e50adSKalle Valo 
1573298e50adSKalle Valo 	basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
1574298e50adSKalle Valo 					  RNDIS_PACKET_TYPE_BROADCAST);
1575298e50adSKalle Valo 
1576298e50adSKalle Valo 	if (usbdev->net->flags & IFF_PROMISC) {
1577298e50adSKalle Valo 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
1578298e50adSKalle Valo 				      RNDIS_PACKET_TYPE_ALL_LOCAL);
1579298e50adSKalle Valo 	} else if (usbdev->net->flags & IFF_ALLMULTI) {
1580298e50adSKalle Valo 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1581298e50adSKalle Valo 	}
1582298e50adSKalle Valo 
1583298e50adSKalle Valo 	if (filter != basefilter)
1584298e50adSKalle Valo 		goto set_filter;
1585298e50adSKalle Valo 
1586298e50adSKalle Valo 	/*
1587298e50adSKalle Valo 	 * mc_list should be accessed holding the lock, so copy addresses to
1588298e50adSKalle Valo 	 * local buffer first.
1589298e50adSKalle Valo 	 */
1590298e50adSKalle Valo 	netif_addr_lock_bh(usbdev->net);
1591298e50adSKalle Valo 	mc_count = netdev_mc_count(usbdev->net);
1592298e50adSKalle Valo 	if (mc_count > priv->multicast_size) {
1593298e50adSKalle Valo 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1594298e50adSKalle Valo 	} else if (mc_count) {
1595298e50adSKalle Valo 		int i = 0;
1596298e50adSKalle Valo 
1597298e50adSKalle Valo 		mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
1598298e50adSKalle Valo 		if (!mc_addrs) {
1599298e50adSKalle Valo 			netif_addr_unlock_bh(usbdev->net);
1600298e50adSKalle Valo 			return;
1601298e50adSKalle Valo 		}
1602298e50adSKalle Valo 
1603298e50adSKalle Valo 		netdev_for_each_mc_addr(ha, usbdev->net)
1604298e50adSKalle Valo 			memcpy(mc_addrs + i++ * ETH_ALEN,
1605298e50adSKalle Valo 			       ha->addr, ETH_ALEN);
1606298e50adSKalle Valo 	}
1607298e50adSKalle Valo 	netif_addr_unlock_bh(usbdev->net);
1608298e50adSKalle Valo 
1609298e50adSKalle Valo 	if (filter != basefilter)
1610298e50adSKalle Valo 		goto set_filter;
1611298e50adSKalle Valo 
1612298e50adSKalle Valo 	if (mc_count) {
1613298e50adSKalle Valo 		ret = rndis_set_oid(usbdev,
1614298e50adSKalle Valo 				RNDIS_OID_802_3_MULTICAST_LIST,
1615298e50adSKalle Valo 				mc_addrs, mc_count * ETH_ALEN);
1616298e50adSKalle Valo 		kfree(mc_addrs);
1617298e50adSKalle Valo 		if (ret == 0)
1618298e50adSKalle Valo 			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
1619298e50adSKalle Valo 		else
1620298e50adSKalle Valo 			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1621298e50adSKalle Valo 
1622298e50adSKalle Valo 		netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1623298e50adSKalle Valo 			   mc_count, priv->multicast_size, ret);
1624298e50adSKalle Valo 	}
1625298e50adSKalle Valo 
1626298e50adSKalle Valo set_filter:
1627298e50adSKalle Valo 	ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
1628298e50adSKalle Valo 							sizeof(filter));
1629298e50adSKalle Valo 	if (ret < 0) {
1630298e50adSKalle Valo 		netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1631298e50adSKalle Valo 			    le32_to_cpu(filter));
1632298e50adSKalle Valo 	}
1633298e50adSKalle Valo 
1634298e50adSKalle Valo 	netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1635298e50adSKalle Valo 		   le32_to_cpu(filter), ret);
1636298e50adSKalle Valo }
1637298e50adSKalle Valo 
1638298e50adSKalle Valo #ifdef DEBUG
debug_print_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,const char * func_str)1639298e50adSKalle Valo static void debug_print_pmkids(struct usbnet *usbdev,
1640298e50adSKalle Valo 				struct ndis_80211_pmkid *pmkids,
1641298e50adSKalle Valo 				const char *func_str)
1642298e50adSKalle Valo {
1643298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1644298e50adSKalle Valo 	int i, len, count, max_pmkids, entry_len;
1645298e50adSKalle Valo 
1646298e50adSKalle Valo 	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1647298e50adSKalle Valo 	len = le32_to_cpu(pmkids->length);
1648298e50adSKalle Valo 	count = le32_to_cpu(pmkids->bssid_info_count);
1649298e50adSKalle Valo 
1650298e50adSKalle Valo 	entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1651298e50adSKalle Valo 
1652298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1653298e50adSKalle Valo 				"%d)\n", func_str, count, len, entry_len);
1654298e50adSKalle Valo 
1655298e50adSKalle Valo 	if (count > max_pmkids)
1656298e50adSKalle Valo 		count = max_pmkids;
1657298e50adSKalle Valo 
1658298e50adSKalle Valo 	for (i = 0; i < count; i++) {
1659298e50adSKalle Valo 		u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1660298e50adSKalle Valo 
1661298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s():  bssid: %pM, "
1662298e50adSKalle Valo 				"pmkid: %08X:%08X:%08X:%08X\n",
1663298e50adSKalle Valo 				func_str, pmkids->bssid_info[i].bssid,
1664298e50adSKalle Valo 				cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1665298e50adSKalle Valo 				cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1666298e50adSKalle Valo 	}
1667298e50adSKalle Valo }
1668298e50adSKalle Valo #else
debug_print_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,const char * func_str)1669298e50adSKalle Valo static void debug_print_pmkids(struct usbnet *usbdev,
1670298e50adSKalle Valo 				struct ndis_80211_pmkid *pmkids,
1671298e50adSKalle Valo 				const char *func_str)
1672298e50adSKalle Valo {
1673298e50adSKalle Valo 	return;
1674298e50adSKalle Valo }
1675298e50adSKalle Valo #endif
1676298e50adSKalle Valo 
get_device_pmkids(struct usbnet * usbdev)1677298e50adSKalle Valo static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1678298e50adSKalle Valo {
1679298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1680298e50adSKalle Valo 	struct ndis_80211_pmkid *pmkids;
1681298e50adSKalle Valo 	int len, ret, max_pmkids;
1682298e50adSKalle Valo 
1683298e50adSKalle Valo 	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1684298e50adSKalle Valo 	len = struct_size(pmkids, bssid_info, max_pmkids);
1685298e50adSKalle Valo 
1686298e50adSKalle Valo 	pmkids = kzalloc(len, GFP_KERNEL);
1687298e50adSKalle Valo 	if (!pmkids)
1688298e50adSKalle Valo 		return ERR_PTR(-ENOMEM);
1689298e50adSKalle Valo 
1690298e50adSKalle Valo 	pmkids->length = cpu_to_le32(len);
1691298e50adSKalle Valo 	pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1692298e50adSKalle Valo 
1693298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
1694298e50adSKalle Valo 			pmkids, &len);
1695298e50adSKalle Valo 	if (ret < 0) {
1696298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
1697298e50adSKalle Valo 				" -> %d\n", __func__, len, max_pmkids, ret);
1698298e50adSKalle Valo 
1699298e50adSKalle Valo 		kfree(pmkids);
1700298e50adSKalle Valo 		return ERR_PTR(ret);
1701298e50adSKalle Valo 	}
1702298e50adSKalle Valo 
1703298e50adSKalle Valo 	if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1704298e50adSKalle Valo 		pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1705298e50adSKalle Valo 
1706298e50adSKalle Valo 	debug_print_pmkids(usbdev, pmkids, __func__);
1707298e50adSKalle Valo 
1708298e50adSKalle Valo 	return pmkids;
1709298e50adSKalle Valo }
1710298e50adSKalle Valo 
set_device_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids)1711298e50adSKalle Valo static int set_device_pmkids(struct usbnet *usbdev,
1712298e50adSKalle Valo 				struct ndis_80211_pmkid *pmkids)
1713298e50adSKalle Valo {
1714298e50adSKalle Valo 	int ret, len, num_pmkids;
1715298e50adSKalle Valo 
1716298e50adSKalle Valo 	num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1717298e50adSKalle Valo 	len = struct_size(pmkids, bssid_info, num_pmkids);
1718298e50adSKalle Valo 	pmkids->length = cpu_to_le32(len);
1719298e50adSKalle Valo 
1720298e50adSKalle Valo 	debug_print_pmkids(usbdev, pmkids, __func__);
1721298e50adSKalle Valo 
1722298e50adSKalle Valo 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
1723298e50adSKalle Valo 			    le32_to_cpu(pmkids->length));
1724298e50adSKalle Valo 	if (ret < 0) {
1725298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
1726298e50adSKalle Valo 				"\n", __func__, len, num_pmkids, ret);
1727298e50adSKalle Valo 	}
1728298e50adSKalle Valo 
1729298e50adSKalle Valo 	kfree(pmkids);
1730298e50adSKalle Valo 	return ret;
1731298e50adSKalle Valo }
1732298e50adSKalle Valo 
remove_pmkid(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,struct cfg80211_pmksa * pmksa,int max_pmkids)1733298e50adSKalle Valo static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1734298e50adSKalle Valo 						struct ndis_80211_pmkid *pmkids,
1735298e50adSKalle Valo 						struct cfg80211_pmksa *pmksa,
1736298e50adSKalle Valo 						int max_pmkids)
1737298e50adSKalle Valo {
1738298e50adSKalle Valo 	int i, err;
1739298e50adSKalle Valo 	unsigned int count;
1740298e50adSKalle Valo 
1741298e50adSKalle Valo 	count = le32_to_cpu(pmkids->bssid_info_count);
1742298e50adSKalle Valo 
1743298e50adSKalle Valo 	if (count > max_pmkids)
1744298e50adSKalle Valo 		count = max_pmkids;
1745298e50adSKalle Valo 
1746298e50adSKalle Valo 	for (i = 0; i < count; i++)
1747298e50adSKalle Valo 		if (ether_addr_equal(pmkids->bssid_info[i].bssid,
1748298e50adSKalle Valo 				     pmksa->bssid))
1749298e50adSKalle Valo 			break;
1750298e50adSKalle Valo 
1751298e50adSKalle Valo 	/* pmkid not found */
1752298e50adSKalle Valo 	if (i == count) {
1753298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1754298e50adSKalle Valo 					__func__, pmksa->bssid);
1755298e50adSKalle Valo 		err = -ENOENT;
1756298e50adSKalle Valo 		goto error;
1757298e50adSKalle Valo 	}
1758298e50adSKalle Valo 
1759298e50adSKalle Valo 	for (; i + 1 < count; i++)
1760298e50adSKalle Valo 		pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1761298e50adSKalle Valo 
1762298e50adSKalle Valo 	count--;
1763298e50adSKalle Valo 	pmkids->length = cpu_to_le32(struct_size(pmkids, bssid_info, count));
1764298e50adSKalle Valo 	pmkids->bssid_info_count = cpu_to_le32(count);
1765298e50adSKalle Valo 
1766298e50adSKalle Valo 	return pmkids;
1767298e50adSKalle Valo error:
1768298e50adSKalle Valo 	kfree(pmkids);
1769298e50adSKalle Valo 	return ERR_PTR(err);
1770298e50adSKalle Valo }
1771298e50adSKalle Valo 
update_pmkid(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,struct cfg80211_pmksa * pmksa,int max_pmkids)1772298e50adSKalle Valo static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1773298e50adSKalle Valo 						struct ndis_80211_pmkid *pmkids,
1774298e50adSKalle Valo 						struct cfg80211_pmksa *pmksa,
1775298e50adSKalle Valo 						int max_pmkids)
1776298e50adSKalle Valo {
1777298e50adSKalle Valo 	struct ndis_80211_pmkid *new_pmkids;
1778298e50adSKalle Valo 	int i, err, newlen;
1779298e50adSKalle Valo 	unsigned int count;
1780298e50adSKalle Valo 
1781298e50adSKalle Valo 	count = le32_to_cpu(pmkids->bssid_info_count);
1782298e50adSKalle Valo 
1783298e50adSKalle Valo 	if (count > max_pmkids)
1784298e50adSKalle Valo 		count = max_pmkids;
1785298e50adSKalle Valo 
1786298e50adSKalle Valo 	/* update with new pmkid */
1787298e50adSKalle Valo 	for (i = 0; i < count; i++) {
1788298e50adSKalle Valo 		if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
1789298e50adSKalle Valo 				      pmksa->bssid))
1790298e50adSKalle Valo 			continue;
1791298e50adSKalle Valo 
1792298e50adSKalle Valo 		memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1793298e50adSKalle Valo 								WLAN_PMKID_LEN);
1794298e50adSKalle Valo 
1795298e50adSKalle Valo 		return pmkids;
1796298e50adSKalle Valo 	}
1797298e50adSKalle Valo 
1798298e50adSKalle Valo 	/* out of space, return error */
1799298e50adSKalle Valo 	if (i == max_pmkids) {
1800298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1801298e50adSKalle Valo 		err = -ENOSPC;
1802298e50adSKalle Valo 		goto error;
1803298e50adSKalle Valo 	}
1804298e50adSKalle Valo 
1805298e50adSKalle Valo 	/* add new pmkid */
1806298e50adSKalle Valo 	newlen = struct_size(pmkids, bssid_info, count + 1);
1807298e50adSKalle Valo 
1808298e50adSKalle Valo 	new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1809298e50adSKalle Valo 	if (!new_pmkids) {
1810298e50adSKalle Valo 		err = -ENOMEM;
1811298e50adSKalle Valo 		goto error;
1812298e50adSKalle Valo 	}
1813298e50adSKalle Valo 	pmkids = new_pmkids;
1814298e50adSKalle Valo 
1815298e50adSKalle Valo 	pmkids->length = cpu_to_le32(newlen);
1816298e50adSKalle Valo 	pmkids->bssid_info_count = cpu_to_le32(count + 1);
1817298e50adSKalle Valo 
1818298e50adSKalle Valo 	memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1819298e50adSKalle Valo 	memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1820298e50adSKalle Valo 
1821298e50adSKalle Valo 	return pmkids;
1822298e50adSKalle Valo error:
1823298e50adSKalle Valo 	kfree(pmkids);
1824298e50adSKalle Valo 	return ERR_PTR(err);
1825298e50adSKalle Valo }
1826298e50adSKalle Valo 
1827298e50adSKalle Valo /*
1828298e50adSKalle Valo  * cfg80211 ops
1829298e50adSKalle Valo  */
rndis_change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1830298e50adSKalle Valo static int rndis_change_virtual_intf(struct wiphy *wiphy,
1831298e50adSKalle Valo 					struct net_device *dev,
1832298e50adSKalle Valo 					enum nl80211_iftype type,
1833298e50adSKalle Valo 					struct vif_params *params)
1834298e50adSKalle Valo {
1835298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1836298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
1837298e50adSKalle Valo 	int mode;
1838298e50adSKalle Valo 
1839298e50adSKalle Valo 	switch (type) {
1840298e50adSKalle Valo 	case NL80211_IFTYPE_ADHOC:
1841298e50adSKalle Valo 		mode = NDIS_80211_INFRA_ADHOC;
1842298e50adSKalle Valo 		break;
1843298e50adSKalle Valo 	case NL80211_IFTYPE_STATION:
1844298e50adSKalle Valo 		mode = NDIS_80211_INFRA_INFRA;
1845298e50adSKalle Valo 		break;
1846298e50adSKalle Valo 	default:
1847298e50adSKalle Valo 		return -EINVAL;
1848298e50adSKalle Valo 	}
1849298e50adSKalle Valo 
1850298e50adSKalle Valo 	priv->wdev.iftype = type;
1851298e50adSKalle Valo 
1852298e50adSKalle Valo 	return set_infra_mode(usbdev, mode);
1853298e50adSKalle Valo }
1854298e50adSKalle Valo 
rndis_set_wiphy_params(struct wiphy * wiphy,u32 changed)1855298e50adSKalle Valo static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1856298e50adSKalle Valo {
1857298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1858298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
1859298e50adSKalle Valo 	int err;
1860298e50adSKalle Valo 
1861298e50adSKalle Valo 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1862298e50adSKalle Valo 		err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1863298e50adSKalle Valo 		if (err < 0)
1864298e50adSKalle Valo 			return err;
1865298e50adSKalle Valo 	}
1866298e50adSKalle Valo 
1867298e50adSKalle Valo 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1868298e50adSKalle Valo 		err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1869298e50adSKalle Valo 		if (err < 0)
1870298e50adSKalle Valo 			return err;
1871298e50adSKalle Valo 	}
1872298e50adSKalle Valo 
1873298e50adSKalle Valo 	return 0;
1874298e50adSKalle Valo }
1875298e50adSKalle Valo 
rndis_set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)1876298e50adSKalle Valo static int rndis_set_tx_power(struct wiphy *wiphy,
1877298e50adSKalle Valo 			      struct wireless_dev *wdev,
1878298e50adSKalle Valo 			      enum nl80211_tx_power_setting type,
1879298e50adSKalle Valo 			      int mbm)
1880298e50adSKalle Valo {
1881298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1882298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
1883298e50adSKalle Valo 
1884298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1885298e50adSKalle Valo 		   __func__, type, mbm);
1886298e50adSKalle Valo 
1887298e50adSKalle Valo 	if (mbm < 0 || (mbm % 100))
1888298e50adSKalle Valo 		return -ENOTSUPP;
1889298e50adSKalle Valo 
1890298e50adSKalle Valo 	/* Device doesn't support changing txpower after initialization, only
1891298e50adSKalle Valo 	 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1892298e50adSKalle Valo 	 * currently used.
1893298e50adSKalle Valo 	 */
1894298e50adSKalle Valo 	if (type == NL80211_TX_POWER_AUTOMATIC ||
1895298e50adSKalle Valo 	    MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1896298e50adSKalle Valo 		if (!priv->radio_on)
1897298e50adSKalle Valo 			disassociate(usbdev, true); /* turn on radio */
1898298e50adSKalle Valo 
1899298e50adSKalle Valo 		return 0;
1900298e50adSKalle Valo 	}
1901298e50adSKalle Valo 
1902298e50adSKalle Valo 	return -ENOTSUPP;
1903298e50adSKalle Valo }
1904298e50adSKalle Valo 
rndis_get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int * dbm)1905298e50adSKalle Valo static int rndis_get_tx_power(struct wiphy *wiphy,
1906298e50adSKalle Valo 			      struct wireless_dev *wdev,
1907298e50adSKalle Valo 			      int *dbm)
1908298e50adSKalle Valo {
1909298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1910298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
1911298e50adSKalle Valo 
1912298e50adSKalle Valo 	*dbm = get_bcm4320_power_dbm(priv);
1913298e50adSKalle Valo 
1914298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1915298e50adSKalle Valo 
1916298e50adSKalle Valo 	return 0;
1917298e50adSKalle Valo }
1918298e50adSKalle Valo 
1919298e50adSKalle Valo #define SCAN_DELAY_JIFFIES (6 * HZ)
rndis_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)1920298e50adSKalle Valo static int rndis_scan(struct wiphy *wiphy,
1921298e50adSKalle Valo 			struct cfg80211_scan_request *request)
1922298e50adSKalle Valo {
1923298e50adSKalle Valo 	struct net_device *dev = request->wdev->netdev;
1924298e50adSKalle Valo 	struct usbnet *usbdev = netdev_priv(dev);
1925298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1926298e50adSKalle Valo 	int ret;
1927298e50adSKalle Valo 	int delay = SCAN_DELAY_JIFFIES;
1928298e50adSKalle Valo 
1929298e50adSKalle Valo 	netdev_dbg(usbdev->net, "cfg80211.scan\n");
1930298e50adSKalle Valo 
1931298e50adSKalle Valo 	/* Get current bssid list from device before new scan, as new scan
1932298e50adSKalle Valo 	 * clears internal bssid list.
1933298e50adSKalle Valo 	 */
1934298e50adSKalle Valo 	rndis_check_bssid_list(usbdev, NULL, NULL);
1935298e50adSKalle Valo 
1936298e50adSKalle Valo 	if (priv->scan_request && priv->scan_request != request)
1937298e50adSKalle Valo 		return -EBUSY;
1938298e50adSKalle Valo 
1939298e50adSKalle Valo 	priv->scan_request = request;
1940298e50adSKalle Valo 
1941298e50adSKalle Valo 	ret = rndis_start_bssid_list_scan(usbdev);
1942298e50adSKalle Valo 	if (ret == 0) {
1943298e50adSKalle Valo 		if (priv->device_type == RNDIS_BCM4320A)
1944298e50adSKalle Valo 			delay = HZ;
1945298e50adSKalle Valo 
1946298e50adSKalle Valo 		/* Wait before retrieving scan results from device */
1947298e50adSKalle Valo 		queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
1948298e50adSKalle Valo 	}
1949298e50adSKalle Valo 
1950298e50adSKalle Valo 	return ret;
1951298e50adSKalle Valo }
1952298e50adSKalle Valo 
rndis_bss_info_update(struct usbnet * usbdev,struct ndis_80211_bssid_ex * bssid)1953298e50adSKalle Valo static bool rndis_bss_info_update(struct usbnet *usbdev,
1954298e50adSKalle Valo 				  struct ndis_80211_bssid_ex *bssid)
1955298e50adSKalle Valo {
1956298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1957298e50adSKalle Valo 	struct ieee80211_channel *channel;
1958298e50adSKalle Valo 	struct cfg80211_bss *bss;
1959298e50adSKalle Valo 	s32 signal;
1960298e50adSKalle Valo 	u64 timestamp;
1961298e50adSKalle Valo 	u16 capability;
1962298e50adSKalle Valo 	u16 beacon_interval;
1963298e50adSKalle Valo 	struct ndis_80211_fixed_ies *fixed;
1964298e50adSKalle Valo 	int ie_len, bssid_len;
1965298e50adSKalle Valo 	u8 *ie;
1966298e50adSKalle Valo 
1967298e50adSKalle Valo 	netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
1968298e50adSKalle Valo 		   bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
1969298e50adSKalle Valo 
1970298e50adSKalle Valo 	/* parse bssid structure */
1971298e50adSKalle Valo 	bssid_len = le32_to_cpu(bssid->length);
1972298e50adSKalle Valo 
1973298e50adSKalle Valo 	if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1974298e50adSKalle Valo 			sizeof(struct ndis_80211_fixed_ies))
1975703f15b6SDan Carpenter 		return false;
1976298e50adSKalle Valo 
1977298e50adSKalle Valo 	fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1978298e50adSKalle Valo 
1979298e50adSKalle Valo 	ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1980298e50adSKalle Valo 	ie_len = min(bssid_len - (int)sizeof(*bssid),
1981298e50adSKalle Valo 					(int)le32_to_cpu(bssid->ie_length));
1982298e50adSKalle Valo 	ie_len -= sizeof(struct ndis_80211_fixed_ies);
1983298e50adSKalle Valo 	if (ie_len < 0)
1984703f15b6SDan Carpenter 		return false;
1985298e50adSKalle Valo 
1986298e50adSKalle Valo 	/* extract data for cfg80211_inform_bss */
1987298e50adSKalle Valo 	channel = ieee80211_get_channel(priv->wdev.wiphy,
1988298e50adSKalle Valo 			KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1989298e50adSKalle Valo 	if (!channel)
1990703f15b6SDan Carpenter 		return false;
1991298e50adSKalle Valo 
1992298e50adSKalle Valo 	signal = level_to_qual(le32_to_cpu(bssid->rssi));
1993298e50adSKalle Valo 	timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
1994298e50adSKalle Valo 	capability = le16_to_cpu(fixed->capabilities);
1995298e50adSKalle Valo 	beacon_interval = le16_to_cpu(fixed->beacon_interval);
1996298e50adSKalle Valo 
1997298e50adSKalle Valo 	bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
1998298e50adSKalle Valo 				  CFG80211_BSS_FTYPE_UNKNOWN, bssid->mac,
1999298e50adSKalle Valo 				  timestamp, capability, beacon_interval,
2000298e50adSKalle Valo 				  ie, ie_len, signal, GFP_KERNEL);
2001298e50adSKalle Valo 	cfg80211_put_bss(priv->wdev.wiphy, bss);
2002298e50adSKalle Valo 
2003298e50adSKalle Valo 	return (bss != NULL);
2004298e50adSKalle Valo }
2005298e50adSKalle Valo 
next_bssid_list_item(struct ndis_80211_bssid_ex * bssid,int * bssid_len,void * buf,int len)2006298e50adSKalle Valo static struct ndis_80211_bssid_ex *next_bssid_list_item(
2007298e50adSKalle Valo 					struct ndis_80211_bssid_ex *bssid,
2008298e50adSKalle Valo 					int *bssid_len, void *buf, int len)
2009298e50adSKalle Valo {
2010298e50adSKalle Valo 	void *buf_end, *bssid_end;
2011298e50adSKalle Valo 
2012298e50adSKalle Valo 	buf_end = (char *)buf + len;
2013298e50adSKalle Valo 	bssid_end = (char *)bssid + *bssid_len;
2014298e50adSKalle Valo 
2015298e50adSKalle Valo 	if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
2016298e50adSKalle Valo 		*bssid_len = 0;
2017298e50adSKalle Valo 		return NULL;
2018298e50adSKalle Valo 	} else {
2019298e50adSKalle Valo 		bssid = (void *)((char *)bssid + *bssid_len);
2020298e50adSKalle Valo 		*bssid_len = le32_to_cpu(bssid->length);
2021298e50adSKalle Valo 		return bssid;
2022298e50adSKalle Valo 	}
2023298e50adSKalle Valo }
2024298e50adSKalle Valo 
check_bssid_list_item(struct ndis_80211_bssid_ex * bssid,int bssid_len,void * buf,int len)2025298e50adSKalle Valo static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
2026298e50adSKalle Valo 				  int bssid_len, void *buf, int len)
2027298e50adSKalle Valo {
2028298e50adSKalle Valo 	void *buf_end, *bssid_end;
2029298e50adSKalle Valo 
2030298e50adSKalle Valo 	if (!bssid || bssid_len <= 0 || bssid_len > len)
2031298e50adSKalle Valo 		return false;
2032298e50adSKalle Valo 
2033298e50adSKalle Valo 	buf_end = (char *)buf + len;
2034298e50adSKalle Valo 	bssid_end = (char *)bssid + bssid_len;
2035298e50adSKalle Valo 
2036298e50adSKalle Valo 	return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
2037298e50adSKalle Valo }
2038298e50adSKalle Valo 
rndis_check_bssid_list(struct usbnet * usbdev,u8 * match_bssid,bool * matched)2039298e50adSKalle Valo static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
2040298e50adSKalle Valo 					bool *matched)
2041298e50adSKalle Valo {
2042298e50adSKalle Valo 	void *buf = NULL;
2043298e50adSKalle Valo 	struct ndis_80211_bssid_list_ex *bssid_list;
2044298e50adSKalle Valo 	struct ndis_80211_bssid_ex *bssid;
2045298e50adSKalle Valo 	int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
2046298e50adSKalle Valo 
2047298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s()\n", __func__);
2048298e50adSKalle Valo 
2049298e50adSKalle Valo 	len = CONTROL_BUFFER_SIZE;
2050298e50adSKalle Valo resize_buf:
2051298e50adSKalle Valo 	buf = kzalloc(len, GFP_KERNEL);
2052298e50adSKalle Valo 	if (!buf) {
2053298e50adSKalle Valo 		ret = -ENOMEM;
2054298e50adSKalle Valo 		goto out;
2055298e50adSKalle Valo 	}
2056298e50adSKalle Valo 
2057298e50adSKalle Valo 	/* BSSID-list might have got bigger last time we checked, keep
2058298e50adSKalle Valo 	 * resizing until it won't get any bigger.
2059298e50adSKalle Valo 	 */
2060298e50adSKalle Valo 	new_len = len;
2061298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST,
2062298e50adSKalle Valo 			      buf, &new_len);
2063298e50adSKalle Valo 	if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
2064298e50adSKalle Valo 		goto out;
2065298e50adSKalle Valo 
2066298e50adSKalle Valo 	if (new_len > len) {
2067298e50adSKalle Valo 		len = new_len;
2068298e50adSKalle Valo 		kfree(buf);
2069298e50adSKalle Valo 		goto resize_buf;
2070298e50adSKalle Valo 	}
2071298e50adSKalle Valo 
2072298e50adSKalle Valo 	len = new_len;
2073298e50adSKalle Valo 
2074298e50adSKalle Valo 	bssid_list = buf;
2075298e50adSKalle Valo 	count = le32_to_cpu(bssid_list->num_items);
2076298e50adSKalle Valo 	real_count = 0;
2077298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
2078298e50adSKalle Valo 
2079298e50adSKalle Valo 	bssid_len = 0;
2080298e50adSKalle Valo 	bssid = next_bssid_list_item((void *)bssid_list->bssid_data,
2081298e50adSKalle Valo 				     &bssid_len, buf, len);
2082298e50adSKalle Valo 
2083298e50adSKalle Valo 	/* Device returns incorrect 'num_items'. Workaround by ignoring the
2084298e50adSKalle Valo 	 * received 'num_items' and walking through full bssid buffer instead.
2085298e50adSKalle Valo 	 */
2086298e50adSKalle Valo 	while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
2087298e50adSKalle Valo 		if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
2088298e50adSKalle Valo 		    matched) {
2089298e50adSKalle Valo 			if (ether_addr_equal(bssid->mac, match_bssid))
2090298e50adSKalle Valo 				*matched = true;
2091298e50adSKalle Valo 		}
2092298e50adSKalle Valo 
2093298e50adSKalle Valo 		real_count++;
2094298e50adSKalle Valo 		bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
2095298e50adSKalle Valo 	}
2096298e50adSKalle Valo 
2097298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
2098298e50adSKalle Valo 				" %d\n", __func__, count, real_count);
2099298e50adSKalle Valo 
2100298e50adSKalle Valo out:
2101298e50adSKalle Valo 	kfree(buf);
2102298e50adSKalle Valo 	return ret;
2103298e50adSKalle Valo }
2104298e50adSKalle Valo 
rndis_get_scan_results(struct work_struct * work)2105298e50adSKalle Valo static void rndis_get_scan_results(struct work_struct *work)
2106298e50adSKalle Valo {
2107298e50adSKalle Valo 	struct rndis_wlan_private *priv =
2108298e50adSKalle Valo 		container_of(work, struct rndis_wlan_private, scan_work.work);
2109298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2110298e50adSKalle Valo 	struct cfg80211_scan_info info = {};
2111298e50adSKalle Valo 	int ret;
2112298e50adSKalle Valo 
2113298e50adSKalle Valo 	netdev_dbg(usbdev->net, "get_scan_results\n");
2114298e50adSKalle Valo 
2115298e50adSKalle Valo 	if (!priv->scan_request)
2116298e50adSKalle Valo 		return;
2117298e50adSKalle Valo 
2118298e50adSKalle Valo 	ret = rndis_check_bssid_list(usbdev, NULL, NULL);
2119298e50adSKalle Valo 
2120298e50adSKalle Valo 	info.aborted = ret < 0;
2121298e50adSKalle Valo 	cfg80211_scan_done(priv->scan_request, &info);
2122298e50adSKalle Valo 
2123298e50adSKalle Valo 	priv->scan_request = NULL;
2124298e50adSKalle Valo }
2125298e50adSKalle Valo 
rndis_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)2126298e50adSKalle Valo static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2127298e50adSKalle Valo 					struct cfg80211_connect_params *sme)
2128298e50adSKalle Valo {
2129298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2130298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2131298e50adSKalle Valo 	struct ieee80211_channel *channel = sme->channel;
2132298e50adSKalle Valo 	struct ndis_80211_ssid ssid;
2133298e50adSKalle Valo 	int pairwise = RNDIS_WLAN_ALG_NONE;
2134298e50adSKalle Valo 	int groupwise = RNDIS_WLAN_ALG_NONE;
2135298e50adSKalle Valo 	int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2136298e50adSKalle Valo 	int length, i, ret, chan = -1;
2137298e50adSKalle Valo 
2138298e50adSKalle Valo 	if (channel)
2139298e50adSKalle Valo 		chan = ieee80211_frequency_to_channel(channel->center_freq);
2140298e50adSKalle Valo 
2141298e50adSKalle Valo 	groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2142298e50adSKalle Valo 	for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2143298e50adSKalle Valo 		pairwise |=
2144298e50adSKalle Valo 			rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2145298e50adSKalle Valo 
2146298e50adSKalle Valo 	if (sme->crypto.n_ciphers_pairwise > 0 &&
2147298e50adSKalle Valo 			pairwise == RNDIS_WLAN_ALG_NONE) {
2148298e50adSKalle Valo 		netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2149298e50adSKalle Valo 		return -ENOTSUPP;
2150298e50adSKalle Valo 	}
2151298e50adSKalle Valo 
2152298e50adSKalle Valo 	for (i = 0; i < sme->crypto.n_akm_suites; i++)
2153298e50adSKalle Valo 		keymgmt |=
2154298e50adSKalle Valo 			rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2155298e50adSKalle Valo 
2156298e50adSKalle Valo 	if (sme->crypto.n_akm_suites > 0 &&
2157298e50adSKalle Valo 			keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2158298e50adSKalle Valo 		netdev_err(usbdev->net, "Invalid keymgmt\n");
2159298e50adSKalle Valo 		return -ENOTSUPP;
2160298e50adSKalle Valo 	}
2161298e50adSKalle Valo 
2162298e50adSKalle Valo 	netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2163298e50adSKalle Valo 		   sme->ssid, sme->bssid, chan,
2164298e50adSKalle Valo 		   sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2165298e50adSKalle Valo 		   groupwise, pairwise, keymgmt);
2166298e50adSKalle Valo 
2167298e50adSKalle Valo 	if (is_associated(usbdev))
2168298e50adSKalle Valo 		disassociate(usbdev, false);
2169298e50adSKalle Valo 
2170298e50adSKalle Valo 	ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2171298e50adSKalle Valo 	if (ret < 0) {
2172298e50adSKalle Valo 		netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2173298e50adSKalle Valo 			   ret);
2174298e50adSKalle Valo 		goto err_turn_radio_on;
2175298e50adSKalle Valo 	}
2176298e50adSKalle Valo 
2177298e50adSKalle Valo 	ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2178298e50adSKalle Valo 								keymgmt);
2179298e50adSKalle Valo 	if (ret < 0) {
2180298e50adSKalle Valo 		netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2181298e50adSKalle Valo 			   ret);
2182298e50adSKalle Valo 		goto err_turn_radio_on;
2183298e50adSKalle Valo 	}
2184298e50adSKalle Valo 
2185298e50adSKalle Valo 	set_priv_filter(usbdev);
2186298e50adSKalle Valo 
2187298e50adSKalle Valo 	ret = set_encr_mode(usbdev, pairwise, groupwise);
2188298e50adSKalle Valo 	if (ret < 0) {
2189298e50adSKalle Valo 		netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2190298e50adSKalle Valo 			   ret);
2191298e50adSKalle Valo 		goto err_turn_radio_on;
2192298e50adSKalle Valo 	}
2193298e50adSKalle Valo 
2194298e50adSKalle Valo 	if (channel) {
2195298e50adSKalle Valo 		ret = set_channel(usbdev, chan);
2196298e50adSKalle Valo 		if (ret < 0) {
2197298e50adSKalle Valo 			netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2198298e50adSKalle Valo 				   ret);
2199298e50adSKalle Valo 			goto err_turn_radio_on;
2200298e50adSKalle Valo 		}
2201298e50adSKalle Valo 	}
2202298e50adSKalle Valo 
2203298e50adSKalle Valo 	if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2204298e50adSKalle Valo 		priv->encr_tx_key_index = sme->key_idx;
2205298e50adSKalle Valo 		ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2206298e50adSKalle Valo 		if (ret < 0) {
2207298e50adSKalle Valo 			netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2208298e50adSKalle Valo 				   ret, sme->key_len, sme->key_idx);
2209298e50adSKalle Valo 			goto err_turn_radio_on;
2210298e50adSKalle Valo 		}
2211298e50adSKalle Valo 	}
2212298e50adSKalle Valo 
2213298e50adSKalle Valo 	if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2214298e50adSKalle Valo 				!is_broadcast_ether_addr(sme->bssid)) {
2215298e50adSKalle Valo 		ret = set_bssid(usbdev, sme->bssid);
2216298e50adSKalle Valo 		if (ret < 0) {
2217298e50adSKalle Valo 			netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2218298e50adSKalle Valo 				   ret);
2219298e50adSKalle Valo 			goto err_turn_radio_on;
2220298e50adSKalle Valo 		}
2221298e50adSKalle Valo 	} else
2222298e50adSKalle Valo 		clear_bssid(usbdev);
2223298e50adSKalle Valo 
2224298e50adSKalle Valo 	length = sme->ssid_len;
2225298e50adSKalle Valo 	if (length > NDIS_802_11_LENGTH_SSID)
2226298e50adSKalle Valo 		length = NDIS_802_11_LENGTH_SSID;
2227298e50adSKalle Valo 
2228298e50adSKalle Valo 	memset(&ssid, 0, sizeof(ssid));
2229298e50adSKalle Valo 	ssid.length = cpu_to_le32(length);
2230298e50adSKalle Valo 	memcpy(ssid.essid, sme->ssid, length);
2231298e50adSKalle Valo 
2232298e50adSKalle Valo 	/* Pause and purge rx queue, so we don't pass packets before
2233298e50adSKalle Valo 	 * 'media connect'-indication.
2234298e50adSKalle Valo 	 */
2235298e50adSKalle Valo 	usbnet_pause_rx(usbdev);
2236298e50adSKalle Valo 	usbnet_purge_paused_rxq(usbdev);
2237298e50adSKalle Valo 
2238298e50adSKalle Valo 	ret = set_essid(usbdev, &ssid);
2239298e50adSKalle Valo 	if (ret < 0)
2240298e50adSKalle Valo 		netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2241298e50adSKalle Valo 	return ret;
2242298e50adSKalle Valo 
2243298e50adSKalle Valo err_turn_radio_on:
2244298e50adSKalle Valo 	disassociate(usbdev, true);
2245298e50adSKalle Valo 
2246298e50adSKalle Valo 	return ret;
2247298e50adSKalle Valo }
2248298e50adSKalle Valo 
rndis_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)2249298e50adSKalle Valo static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2250298e50adSKalle Valo 								u16 reason_code)
2251298e50adSKalle Valo {
2252298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2253298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2254298e50adSKalle Valo 
2255298e50adSKalle Valo 	netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2256298e50adSKalle Valo 
2257298e50adSKalle Valo 	priv->connected = false;
2258298e50adSKalle Valo 	eth_zero_addr(priv->bssid);
2259298e50adSKalle Valo 
2260298e50adSKalle Valo 	return deauthenticate(usbdev);
2261298e50adSKalle Valo }
2262298e50adSKalle Valo 
rndis_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)2263298e50adSKalle Valo static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2264298e50adSKalle Valo 					struct cfg80211_ibss_params *params)
2265298e50adSKalle Valo {
2266298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2267298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2268298e50adSKalle Valo 	struct ieee80211_channel *channel = params->chandef.chan;
2269298e50adSKalle Valo 	struct ndis_80211_ssid ssid;
2270298e50adSKalle Valo 	enum nl80211_auth_type auth_type;
2271298e50adSKalle Valo 	int ret, alg, length, chan = -1;
2272298e50adSKalle Valo 
2273298e50adSKalle Valo 	if (channel)
2274298e50adSKalle Valo 		chan = ieee80211_frequency_to_channel(channel->center_freq);
2275298e50adSKalle Valo 
2276298e50adSKalle Valo 	/* TODO: How to handle ad-hoc encryption?
2277298e50adSKalle Valo 	 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2278298e50adSKalle Valo 	 * pre-shared for encryption (open/shared/wpa), is key set before
2279298e50adSKalle Valo 	 * join_ibss? Which auth_type to use (not in params)? What about WPA?
2280298e50adSKalle Valo 	 */
2281298e50adSKalle Valo 	if (params->privacy) {
2282298e50adSKalle Valo 		auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2283298e50adSKalle Valo 		alg = RNDIS_WLAN_ALG_WEP;
2284298e50adSKalle Valo 	} else {
2285298e50adSKalle Valo 		auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2286298e50adSKalle Valo 		alg = RNDIS_WLAN_ALG_NONE;
2287298e50adSKalle Valo 	}
2288298e50adSKalle Valo 
2289298e50adSKalle Valo 	netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2290298e50adSKalle Valo 		   params->ssid, params->bssid, chan, params->privacy);
2291298e50adSKalle Valo 
2292298e50adSKalle Valo 	if (is_associated(usbdev))
2293298e50adSKalle Valo 		disassociate(usbdev, false);
2294298e50adSKalle Valo 
2295298e50adSKalle Valo 	ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2296298e50adSKalle Valo 	if (ret < 0) {
2297298e50adSKalle Valo 		netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2298298e50adSKalle Valo 			   ret);
2299298e50adSKalle Valo 		goto err_turn_radio_on;
2300298e50adSKalle Valo 	}
2301298e50adSKalle Valo 
2302298e50adSKalle Valo 	ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2303298e50adSKalle Valo 	if (ret < 0) {
2304298e50adSKalle Valo 		netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2305298e50adSKalle Valo 			   ret);
2306298e50adSKalle Valo 		goto err_turn_radio_on;
2307298e50adSKalle Valo 	}
2308298e50adSKalle Valo 
2309298e50adSKalle Valo 	set_priv_filter(usbdev);
2310298e50adSKalle Valo 
2311298e50adSKalle Valo 	ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2312298e50adSKalle Valo 	if (ret < 0) {
2313298e50adSKalle Valo 		netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2314298e50adSKalle Valo 			   ret);
2315298e50adSKalle Valo 		goto err_turn_radio_on;
2316298e50adSKalle Valo 	}
2317298e50adSKalle Valo 
2318298e50adSKalle Valo 	if (channel) {
2319298e50adSKalle Valo 		ret = set_channel(usbdev, chan);
2320298e50adSKalle Valo 		if (ret < 0) {
2321298e50adSKalle Valo 			netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2322298e50adSKalle Valo 				   ret);
2323298e50adSKalle Valo 			goto err_turn_radio_on;
2324298e50adSKalle Valo 		}
2325298e50adSKalle Valo 	}
2326298e50adSKalle Valo 
2327298e50adSKalle Valo 	if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2328298e50adSKalle Valo 				!is_broadcast_ether_addr(params->bssid)) {
2329298e50adSKalle Valo 		ret = set_bssid(usbdev, params->bssid);
2330298e50adSKalle Valo 		if (ret < 0) {
2331298e50adSKalle Valo 			netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2332298e50adSKalle Valo 				   ret);
2333298e50adSKalle Valo 			goto err_turn_radio_on;
2334298e50adSKalle Valo 		}
2335298e50adSKalle Valo 	} else
2336298e50adSKalle Valo 		clear_bssid(usbdev);
2337298e50adSKalle Valo 
2338298e50adSKalle Valo 	length = params->ssid_len;
2339298e50adSKalle Valo 	if (length > NDIS_802_11_LENGTH_SSID)
2340298e50adSKalle Valo 		length = NDIS_802_11_LENGTH_SSID;
2341298e50adSKalle Valo 
2342298e50adSKalle Valo 	memset(&ssid, 0, sizeof(ssid));
2343298e50adSKalle Valo 	ssid.length = cpu_to_le32(length);
2344298e50adSKalle Valo 	memcpy(ssid.essid, params->ssid, length);
2345298e50adSKalle Valo 
2346298e50adSKalle Valo 	/* Don't need to pause rx queue for ad-hoc. */
2347298e50adSKalle Valo 	usbnet_purge_paused_rxq(usbdev);
2348298e50adSKalle Valo 	usbnet_resume_rx(usbdev);
2349298e50adSKalle Valo 
2350298e50adSKalle Valo 	ret = set_essid(usbdev, &ssid);
2351298e50adSKalle Valo 	if (ret < 0)
2352298e50adSKalle Valo 		netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2353298e50adSKalle Valo 			   ret);
2354298e50adSKalle Valo 	return ret;
2355298e50adSKalle Valo 
2356298e50adSKalle Valo err_turn_radio_on:
2357298e50adSKalle Valo 	disassociate(usbdev, true);
2358298e50adSKalle Valo 
2359298e50adSKalle Valo 	return ret;
2360298e50adSKalle Valo }
2361298e50adSKalle Valo 
rndis_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2362298e50adSKalle Valo static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2363298e50adSKalle Valo {
2364298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2365298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2366298e50adSKalle Valo 
2367298e50adSKalle Valo 	netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2368298e50adSKalle Valo 
2369298e50adSKalle Valo 	priv->connected = false;
2370298e50adSKalle Valo 	eth_zero_addr(priv->bssid);
2371298e50adSKalle Valo 
2372298e50adSKalle Valo 	return deauthenticate(usbdev);
2373298e50adSKalle Valo }
2374298e50adSKalle Valo 
rndis_add_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)2375298e50adSKalle Valo static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2376298e50adSKalle Valo 			 int link_id,  u8 key_index, bool pairwise,
2377298e50adSKalle Valo 			 const u8 *mac_addr, struct key_params *params)
2378298e50adSKalle Valo {
2379298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2380298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2381298e50adSKalle Valo 	__le32 flags;
2382298e50adSKalle Valo 
2383298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2384298e50adSKalle Valo 		   __func__, key_index, mac_addr, params->cipher);
2385298e50adSKalle Valo 
2386298e50adSKalle Valo 	switch (params->cipher) {
2387298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2388298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2389298e50adSKalle Valo 		return add_wep_key(usbdev, params->key, params->key_len,
2390298e50adSKalle Valo 								key_index);
2391298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2392298e50adSKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2393298e50adSKalle Valo 		flags = 0;
2394298e50adSKalle Valo 
2395298e50adSKalle Valo 		if (params->seq && params->seq_len > 0)
2396298e50adSKalle Valo 			flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2397298e50adSKalle Valo 		if (mac_addr)
2398298e50adSKalle Valo 			flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2399298e50adSKalle Valo 					NDIS_80211_ADDKEY_TRANSMIT_KEY;
2400298e50adSKalle Valo 
2401298e50adSKalle Valo 		return add_wpa_key(usbdev, params->key, params->key_len,
2402298e50adSKalle Valo 				key_index, mac_addr, params->seq,
2403298e50adSKalle Valo 				params->seq_len, params->cipher, flags);
2404298e50adSKalle Valo 	default:
2405298e50adSKalle Valo 		netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2406298e50adSKalle Valo 			   __func__, params->cipher);
2407298e50adSKalle Valo 		return -ENOTSUPP;
2408298e50adSKalle Valo 	}
2409298e50adSKalle Valo }
2410298e50adSKalle Valo 
rndis_del_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)2411298e50adSKalle Valo static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2412298e50adSKalle Valo 			 int link_id, u8 key_index, bool pairwise,
2413298e50adSKalle Valo 			 const u8 *mac_addr)
2414298e50adSKalle Valo {
2415298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2416298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2417298e50adSKalle Valo 
2418298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2419298e50adSKalle Valo 
2420298e50adSKalle Valo 	return remove_key(usbdev, key_index, mac_addr);
2421298e50adSKalle Valo }
2422298e50adSKalle Valo 
rndis_set_default_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool unicast,bool multicast)2423298e50adSKalle Valo static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2424298e50adSKalle Valo 				 int link_id, u8 key_index, bool unicast,
2425298e50adSKalle Valo 				 bool multicast)
2426298e50adSKalle Valo {
2427298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2428298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2429298e50adSKalle Valo 	struct rndis_wlan_encr_key key;
2430298e50adSKalle Valo 
2431298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2432298e50adSKalle Valo 
2433298e50adSKalle Valo 	if (key_index >= RNDIS_WLAN_NUM_KEYS)
2434298e50adSKalle Valo 		return -ENOENT;
2435298e50adSKalle Valo 
2436298e50adSKalle Valo 	priv->encr_tx_key_index = key_index;
2437298e50adSKalle Valo 
2438298e50adSKalle Valo 	if (is_wpa_key(priv, key_index))
2439298e50adSKalle Valo 		return 0;
2440298e50adSKalle Valo 
2441298e50adSKalle Valo 	key = priv->encr_keys[key_index];
2442298e50adSKalle Valo 
2443298e50adSKalle Valo 	return add_wep_key(usbdev, key.material, key.len, key_index);
2444298e50adSKalle Valo }
2445298e50adSKalle Valo 
rndis_fill_station_info(struct usbnet * usbdev,struct station_info * sinfo)2446298e50adSKalle Valo static void rndis_fill_station_info(struct usbnet *usbdev,
2447298e50adSKalle Valo 						struct station_info *sinfo)
2448298e50adSKalle Valo {
2449298e50adSKalle Valo 	__le32 linkspeed, rssi;
2450298e50adSKalle Valo 	int ret, len;
2451298e50adSKalle Valo 
2452298e50adSKalle Valo 	memset(sinfo, 0, sizeof(*sinfo));
2453298e50adSKalle Valo 
2454298e50adSKalle Valo 	len = sizeof(linkspeed);
2455298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_GEN_LINK_SPEED, &linkspeed, &len);
2456298e50adSKalle Valo 	if (ret == 0) {
2457298e50adSKalle Valo 		sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2458298e50adSKalle Valo 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2459298e50adSKalle Valo 	}
2460298e50adSKalle Valo 
2461298e50adSKalle Valo 	len = sizeof(rssi);
2462298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
2463298e50adSKalle Valo 			      &rssi, &len);
2464298e50adSKalle Valo 	if (ret == 0) {
2465298e50adSKalle Valo 		sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2466298e50adSKalle Valo 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2467298e50adSKalle Valo 	}
2468298e50adSKalle Valo }
2469298e50adSKalle Valo 
rndis_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)2470298e50adSKalle Valo static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2471298e50adSKalle Valo 			     const u8 *mac, struct station_info *sinfo)
2472298e50adSKalle Valo {
2473298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2474298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2475298e50adSKalle Valo 
2476298e50adSKalle Valo 	if (!ether_addr_equal(priv->bssid, mac))
2477298e50adSKalle Valo 		return -ENOENT;
2478298e50adSKalle Valo 
2479298e50adSKalle Valo 	rndis_fill_station_info(usbdev, sinfo);
2480298e50adSKalle Valo 
2481298e50adSKalle Valo 	return 0;
2482298e50adSKalle Valo }
2483298e50adSKalle Valo 
rndis_dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)2484298e50adSKalle Valo static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2485298e50adSKalle Valo 			       int idx, u8 *mac, struct station_info *sinfo)
2486298e50adSKalle Valo {
2487298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2488298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2489298e50adSKalle Valo 
2490298e50adSKalle Valo 	if (idx != 0)
2491298e50adSKalle Valo 		return -ENOENT;
2492298e50adSKalle Valo 
2493298e50adSKalle Valo 	memcpy(mac, priv->bssid, ETH_ALEN);
2494298e50adSKalle Valo 
2495298e50adSKalle Valo 	rndis_fill_station_info(usbdev, sinfo);
2496298e50adSKalle Valo 
2497298e50adSKalle Valo 	return 0;
2498298e50adSKalle Valo }
2499298e50adSKalle Valo 
rndis_set_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)2500298e50adSKalle Valo static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2501298e50adSKalle Valo 				struct cfg80211_pmksa *pmksa)
2502298e50adSKalle Valo {
2503298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2504298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2505298e50adSKalle Valo 	struct ndis_80211_pmkid *pmkids;
2506298e50adSKalle Valo 	u32 *tmp = (u32 *)pmksa->pmkid;
2507298e50adSKalle Valo 
2508298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2509298e50adSKalle Valo 			pmksa->bssid,
2510298e50adSKalle Valo 			cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2511298e50adSKalle Valo 			cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2512298e50adSKalle Valo 
2513298e50adSKalle Valo 	pmkids = get_device_pmkids(usbdev);
2514298e50adSKalle Valo 	if (IS_ERR(pmkids)) {
2515298e50adSKalle Valo 		/* couldn't read PMKID cache from device */
2516298e50adSKalle Valo 		return PTR_ERR(pmkids);
2517298e50adSKalle Valo 	}
2518298e50adSKalle Valo 
2519298e50adSKalle Valo 	pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2520298e50adSKalle Valo 	if (IS_ERR(pmkids)) {
2521298e50adSKalle Valo 		/* not found, list full, etc */
2522298e50adSKalle Valo 		return PTR_ERR(pmkids);
2523298e50adSKalle Valo 	}
2524298e50adSKalle Valo 
2525298e50adSKalle Valo 	return set_device_pmkids(usbdev, pmkids);
2526298e50adSKalle Valo }
2527298e50adSKalle Valo 
rndis_del_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)2528298e50adSKalle Valo static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2529298e50adSKalle Valo 				struct cfg80211_pmksa *pmksa)
2530298e50adSKalle Valo {
2531298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2532298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2533298e50adSKalle Valo 	struct ndis_80211_pmkid *pmkids;
2534298e50adSKalle Valo 	u32 *tmp = (u32 *)pmksa->pmkid;
2535298e50adSKalle Valo 
2536298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2537298e50adSKalle Valo 			pmksa->bssid,
2538298e50adSKalle Valo 			cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2539298e50adSKalle Valo 			cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2540298e50adSKalle Valo 
2541298e50adSKalle Valo 	pmkids = get_device_pmkids(usbdev);
2542298e50adSKalle Valo 	if (IS_ERR(pmkids)) {
2543298e50adSKalle Valo 		/* Couldn't read PMKID cache from device */
2544298e50adSKalle Valo 		return PTR_ERR(pmkids);
2545298e50adSKalle Valo 	}
2546298e50adSKalle Valo 
2547298e50adSKalle Valo 	pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2548298e50adSKalle Valo 	if (IS_ERR(pmkids)) {
2549298e50adSKalle Valo 		/* not found, etc */
2550298e50adSKalle Valo 		return PTR_ERR(pmkids);
2551298e50adSKalle Valo 	}
2552298e50adSKalle Valo 
2553298e50adSKalle Valo 	return set_device_pmkids(usbdev, pmkids);
2554298e50adSKalle Valo }
2555298e50adSKalle Valo 
rndis_flush_pmksa(struct wiphy * wiphy,struct net_device * netdev)2556298e50adSKalle Valo static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2557298e50adSKalle Valo {
2558298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2559298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2560298e50adSKalle Valo 	struct ndis_80211_pmkid pmkid;
2561298e50adSKalle Valo 
2562298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s()\n", __func__);
2563298e50adSKalle Valo 
2564298e50adSKalle Valo 	memset(&pmkid, 0, sizeof(pmkid));
2565298e50adSKalle Valo 
2566298e50adSKalle Valo 	pmkid.length = cpu_to_le32(sizeof(pmkid));
2567298e50adSKalle Valo 	pmkid.bssid_info_count = cpu_to_le32(0);
2568298e50adSKalle Valo 
2569298e50adSKalle Valo 	return rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID,
2570298e50adSKalle Valo 			     &pmkid, sizeof(pmkid));
2571298e50adSKalle Valo }
2572298e50adSKalle Valo 
rndis_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)2573298e50adSKalle Valo static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2574298e50adSKalle Valo 				bool enabled, int timeout)
2575298e50adSKalle Valo {
2576298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2577298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2578298e50adSKalle Valo 	int power_mode;
2579298e50adSKalle Valo 	__le32 mode;
2580298e50adSKalle Valo 	int ret;
2581298e50adSKalle Valo 
2582298e50adSKalle Valo 	if (priv->device_type != RNDIS_BCM4320B)
2583298e50adSKalle Valo 		return -ENOTSUPP;
2584298e50adSKalle Valo 
2585298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
2586298e50adSKalle Valo 				enabled ? "enabled" : "disabled",
2587298e50adSKalle Valo 				timeout);
2588298e50adSKalle Valo 
2589298e50adSKalle Valo 	if (enabled)
2590298e50adSKalle Valo 		power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
2591298e50adSKalle Valo 	else
2592298e50adSKalle Valo 		power_mode = NDIS_80211_POWER_MODE_CAM;
2593298e50adSKalle Valo 
2594298e50adSKalle Valo 	if (power_mode == priv->power_mode)
2595298e50adSKalle Valo 		return 0;
2596298e50adSKalle Valo 
2597298e50adSKalle Valo 	priv->power_mode = power_mode;
2598298e50adSKalle Valo 
2599298e50adSKalle Valo 	mode = cpu_to_le32(power_mode);
2600298e50adSKalle Valo 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_POWER_MODE,
2601298e50adSKalle Valo 			    &mode, sizeof(mode));
2602298e50adSKalle Valo 
2603298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_POWER_MODE -> %d\n",
2604298e50adSKalle Valo 				__func__, ret);
2605298e50adSKalle Valo 
2606298e50adSKalle Valo 	return ret;
2607298e50adSKalle Valo }
2608298e50adSKalle Valo 
rndis_set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)2609298e50adSKalle Valo static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
2610298e50adSKalle Valo 					struct net_device *dev,
2611298e50adSKalle Valo 					s32 rssi_thold, u32 rssi_hyst)
2612298e50adSKalle Valo {
2613298e50adSKalle Valo 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2614298e50adSKalle Valo 
2615298e50adSKalle Valo 	priv->cqm_rssi_thold = rssi_thold;
2616298e50adSKalle Valo 	priv->cqm_rssi_hyst = rssi_hyst;
2617298e50adSKalle Valo 	priv->last_cqm_event_rssi = 0;
2618298e50adSKalle Valo 
2619298e50adSKalle Valo 	return 0;
2620298e50adSKalle Valo }
2621298e50adSKalle Valo 
rndis_wlan_craft_connected_bss(struct usbnet * usbdev,u8 * bssid,struct ndis_80211_assoc_info * info)2622298e50adSKalle Valo static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
2623298e50adSKalle Valo 					   struct ndis_80211_assoc_info *info)
2624298e50adSKalle Valo {
2625298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2626298e50adSKalle Valo 	struct ieee80211_channel *channel;
2627298e50adSKalle Valo 	struct ndis_80211_ssid ssid;
2628298e50adSKalle Valo 	struct cfg80211_bss *bss;
2629298e50adSKalle Valo 	s32 signal;
2630298e50adSKalle Valo 	u64 timestamp;
2631298e50adSKalle Valo 	u16 capability;
2632298e50adSKalle Valo 	u32 beacon_period = 0;
2633298e50adSKalle Valo 	__le32 rssi;
2634298e50adSKalle Valo 	u8 ie_buf[34];
2635298e50adSKalle Valo 	int len, ret, ie_len;
2636298e50adSKalle Valo 
2637298e50adSKalle Valo 	/* Get signal quality, in case of error use rssi=0 and ignore error. */
2638298e50adSKalle Valo 	len = sizeof(rssi);
2639298e50adSKalle Valo 	rssi = 0;
2640298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
2641298e50adSKalle Valo 			      &rssi, &len);
2642298e50adSKalle Valo 	signal = level_to_qual(le32_to_cpu(rssi));
2643298e50adSKalle Valo 
2644298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_RSSI -> %d, "
2645298e50adSKalle Valo 		   "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
2646298e50adSKalle Valo 		   level_to_qual(le32_to_cpu(rssi)));
2647298e50adSKalle Valo 
2648298e50adSKalle Valo 	/* Get AP capabilities */
2649298e50adSKalle Valo 	if (info) {
2650298e50adSKalle Valo 		capability = le16_to_cpu(info->resp_ie.capa);
2651298e50adSKalle Valo 	} else {
2652298e50adSKalle Valo 		/* Set atleast ESS/IBSS capability */
2653298e50adSKalle Valo 		capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
2654298e50adSKalle Valo 				WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
2655298e50adSKalle Valo 	}
2656298e50adSKalle Valo 
2657298e50adSKalle Valo 	/* Get channel and beacon interval */
2658298e50adSKalle Valo 	channel = get_current_channel(usbdev, &beacon_period);
2659298e50adSKalle Valo 	if (!channel) {
2660298e50adSKalle Valo 		netdev_warn(usbdev->net, "%s(): could not get channel.\n",
2661298e50adSKalle Valo 					__func__);
2662298e50adSKalle Valo 		return;
2663298e50adSKalle Valo 	}
2664298e50adSKalle Valo 
2665298e50adSKalle Valo 	/* Get SSID, in case of error, use zero length SSID and ignore error. */
2666298e50adSKalle Valo 	len = sizeof(ssid);
2667298e50adSKalle Valo 	memset(&ssid, 0, sizeof(ssid));
2668298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_SSID,
2669298e50adSKalle Valo 			      &ssid, &len);
2670298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_SSID -> %d, len: %d, ssid: "
2671298e50adSKalle Valo 				"'%.32s'\n", __func__, ret,
2672298e50adSKalle Valo 				le32_to_cpu(ssid.length), ssid.essid);
2673298e50adSKalle Valo 
2674298e50adSKalle Valo 	if (le32_to_cpu(ssid.length) > 32)
2675298e50adSKalle Valo 		ssid.length = cpu_to_le32(32);
2676298e50adSKalle Valo 
2677298e50adSKalle Valo 	ie_buf[0] = WLAN_EID_SSID;
2678298e50adSKalle Valo 	ie_buf[1] = le32_to_cpu(ssid.length);
2679298e50adSKalle Valo 	memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
2680298e50adSKalle Valo 
2681298e50adSKalle Valo 	ie_len = le32_to_cpu(ssid.length) + 2;
2682298e50adSKalle Valo 
2683298e50adSKalle Valo 	/* no tsf */
2684298e50adSKalle Valo 	timestamp = 0;
2685298e50adSKalle Valo 
2686298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
2687298e50adSKalle Valo 		"capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
2688298e50adSKalle Valo 		"signal:%d\n", __func__, (channel ? channel->center_freq : -1),
2689298e50adSKalle Valo 		bssid, (u32)timestamp, capability, beacon_period, ie_len,
2690298e50adSKalle Valo 		ssid.essid, signal);
2691298e50adSKalle Valo 
2692298e50adSKalle Valo 	bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
2693298e50adSKalle Valo 				  CFG80211_BSS_FTYPE_UNKNOWN, bssid,
2694298e50adSKalle Valo 				  timestamp, capability, beacon_period,
2695298e50adSKalle Valo 				  ie_buf, ie_len, signal, GFP_KERNEL);
2696298e50adSKalle Valo 	cfg80211_put_bss(priv->wdev.wiphy, bss);
2697298e50adSKalle Valo }
2698298e50adSKalle Valo 
2699298e50adSKalle Valo /*
2700298e50adSKalle Valo  * workers, indication handlers, device poller
2701298e50adSKalle Valo  */
rndis_wlan_do_link_up_work(struct usbnet * usbdev)2702298e50adSKalle Valo static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2703298e50adSKalle Valo {
2704298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2705298e50adSKalle Valo 	struct ndis_80211_assoc_info *info = NULL;
2706298e50adSKalle Valo 	u8 bssid[ETH_ALEN];
2707298e50adSKalle Valo 	unsigned int resp_ie_len, req_ie_len;
2708298e50adSKalle Valo 	unsigned int offset;
2709298e50adSKalle Valo 	u8 *req_ie, *resp_ie;
2710298e50adSKalle Valo 	int ret;
2711298e50adSKalle Valo 	bool roamed = false;
2712298e50adSKalle Valo 	bool match_bss;
2713298e50adSKalle Valo 
2714298e50adSKalle Valo 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2715298e50adSKalle Valo 		/* received media connect indication while connected, either
2716298e50adSKalle Valo 		 * device reassociated with same AP or roamed to new. */
2717298e50adSKalle Valo 		roamed = true;
2718298e50adSKalle Valo 	}
2719298e50adSKalle Valo 
2720298e50adSKalle Valo 	req_ie_len = 0;
2721298e50adSKalle Valo 	resp_ie_len = 0;
2722298e50adSKalle Valo 	req_ie = NULL;
2723298e50adSKalle Valo 	resp_ie = NULL;
2724298e50adSKalle Valo 
2725298e50adSKalle Valo 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2726298e50adSKalle Valo 		info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2727298e50adSKalle Valo 		if (!info) {
2728298e50adSKalle Valo 			/* No memory? Try resume work later */
2729298e50adSKalle Valo 			set_bit(WORK_LINK_UP, &priv->work_pending);
2730298e50adSKalle Valo 			queue_work(priv->workqueue, &priv->work);
2731298e50adSKalle Valo 			return;
2732298e50adSKalle Valo 		}
2733298e50adSKalle Valo 
2734298e50adSKalle Valo 		/* Get association info IEs from device. */
2735298e50adSKalle Valo 		ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2736298e50adSKalle Valo 		if (!ret) {
2737298e50adSKalle Valo 			req_ie_len = le32_to_cpu(info->req_ie_length);
2738298e50adSKalle Valo 			if (req_ie_len > CONTROL_BUFFER_SIZE)
2739298e50adSKalle Valo 				req_ie_len = CONTROL_BUFFER_SIZE;
2740298e50adSKalle Valo 			if (req_ie_len != 0) {
2741298e50adSKalle Valo 				offset = le32_to_cpu(info->offset_req_ies);
2742298e50adSKalle Valo 
2743298e50adSKalle Valo 				if (offset > CONTROL_BUFFER_SIZE)
2744298e50adSKalle Valo 					offset = CONTROL_BUFFER_SIZE;
2745298e50adSKalle Valo 
2746298e50adSKalle Valo 				req_ie = (u8 *)info + offset;
2747298e50adSKalle Valo 
2748298e50adSKalle Valo 				if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2749298e50adSKalle Valo 					req_ie_len =
2750298e50adSKalle Valo 						CONTROL_BUFFER_SIZE - offset;
2751298e50adSKalle Valo 			}
2752298e50adSKalle Valo 
2753298e50adSKalle Valo 			resp_ie_len = le32_to_cpu(info->resp_ie_length);
2754298e50adSKalle Valo 			if (resp_ie_len > CONTROL_BUFFER_SIZE)
2755298e50adSKalle Valo 				resp_ie_len = CONTROL_BUFFER_SIZE;
2756298e50adSKalle Valo 			if (resp_ie_len != 0) {
2757298e50adSKalle Valo 				offset = le32_to_cpu(info->offset_resp_ies);
2758298e50adSKalle Valo 
2759298e50adSKalle Valo 				if (offset > CONTROL_BUFFER_SIZE)
2760298e50adSKalle Valo 					offset = CONTROL_BUFFER_SIZE;
2761298e50adSKalle Valo 
2762298e50adSKalle Valo 				resp_ie = (u8 *)info + offset;
2763298e50adSKalle Valo 
2764298e50adSKalle Valo 				if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2765298e50adSKalle Valo 					resp_ie_len =
2766298e50adSKalle Valo 						CONTROL_BUFFER_SIZE - offset;
2767298e50adSKalle Valo 			}
2768298e50adSKalle Valo 		} else {
2769298e50adSKalle Valo 			/* Since rndis_wlan_craft_connected_bss() might use info
2770298e50adSKalle Valo 			 * later and expects info to contain valid data if
2771298e50adSKalle Valo 			 * non-null, free info and set NULL here.
2772298e50adSKalle Valo 			 */
2773298e50adSKalle Valo 			kfree(info);
2774298e50adSKalle Valo 			info = NULL;
2775298e50adSKalle Valo 		}
2776298e50adSKalle Valo 	} else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2777298e50adSKalle Valo 		return;
2778298e50adSKalle Valo 
2779298e50adSKalle Valo 	ret = get_bssid(usbdev, bssid);
2780298e50adSKalle Valo 	if (ret < 0)
2781298e50adSKalle Valo 		memset(bssid, 0, sizeof(bssid));
2782298e50adSKalle Valo 
2783298e50adSKalle Valo 	netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2784298e50adSKalle Valo 		   bssid, roamed ? " roamed" : "");
2785298e50adSKalle Valo 
2786298e50adSKalle Valo 	/* Internal bss list in device should contain at least the currently
2787298e50adSKalle Valo 	 * connected bss and we can get it to cfg80211 with
2788298e50adSKalle Valo 	 * rndis_check_bssid_list().
2789298e50adSKalle Valo 	 *
2790298e50adSKalle Valo 	 * NDIS spec says: "If the device is associated, but the associated
2791298e50adSKalle Valo 	 *  BSSID is not in its BSSID scan list, then the driver must add an
2792298e50adSKalle Valo 	 *  entry for the BSSID at the end of the data that it returns in
2793298e50adSKalle Valo 	 *  response to query of RNDIS_OID_802_11_BSSID_LIST."
2794298e50adSKalle Valo 	 *
2795298e50adSKalle Valo 	 * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
2796298e50adSKalle Valo 	 */
2797298e50adSKalle Valo 	match_bss = false;
2798298e50adSKalle Valo 	rndis_check_bssid_list(usbdev, bssid, &match_bss);
2799298e50adSKalle Valo 
2800298e50adSKalle Valo 	if (!is_zero_ether_addr(bssid) && !match_bss) {
2801298e50adSKalle Valo 		/* Couldn't get bss from device, we need to manually craft bss
2802298e50adSKalle Valo 		 * for cfg80211.
2803298e50adSKalle Valo 		 */
2804298e50adSKalle Valo 		rndis_wlan_craft_connected_bss(usbdev, bssid, info);
2805298e50adSKalle Valo 	}
2806298e50adSKalle Valo 
2807298e50adSKalle Valo 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2808298e50adSKalle Valo 		if (!roamed) {
2809298e50adSKalle Valo 			cfg80211_connect_result(usbdev->net, bssid, req_ie,
2810298e50adSKalle Valo 						req_ie_len, resp_ie,
2811298e50adSKalle Valo 						resp_ie_len, 0, GFP_KERNEL);
2812298e50adSKalle Valo 		} else {
2813298e50adSKalle Valo 			struct cfg80211_roam_info roam_info = {
2814298e50adSKalle Valo 				.links[0].channel =
2815298e50adSKalle Valo 					get_current_channel(usbdev, NULL),
2816298e50adSKalle Valo 				.links[0].bssid = bssid,
2817298e50adSKalle Valo 				.req_ie = req_ie,
2818298e50adSKalle Valo 				.req_ie_len = req_ie_len,
2819298e50adSKalle Valo 				.resp_ie = resp_ie,
2820298e50adSKalle Valo 				.resp_ie_len = resp_ie_len,
2821298e50adSKalle Valo 			};
2822298e50adSKalle Valo 
2823298e50adSKalle Valo 			cfg80211_roamed(usbdev->net, &roam_info, GFP_KERNEL);
2824298e50adSKalle Valo 		}
2825298e50adSKalle Valo 	} else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2826298e50adSKalle Valo 		cfg80211_ibss_joined(usbdev->net, bssid,
2827298e50adSKalle Valo 				     get_current_channel(usbdev, NULL),
2828298e50adSKalle Valo 				     GFP_KERNEL);
2829298e50adSKalle Valo 
2830298e50adSKalle Valo 	kfree(info);
2831298e50adSKalle Valo 
2832298e50adSKalle Valo 	priv->connected = true;
2833298e50adSKalle Valo 	memcpy(priv->bssid, bssid, ETH_ALEN);
2834298e50adSKalle Valo 
2835298e50adSKalle Valo 	usbnet_resume_rx(usbdev);
2836298e50adSKalle Valo 	netif_carrier_on(usbdev->net);
2837298e50adSKalle Valo }
2838298e50adSKalle Valo 
rndis_wlan_do_link_down_work(struct usbnet * usbdev)2839298e50adSKalle Valo static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2840298e50adSKalle Valo {
2841298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2842298e50adSKalle Valo 
2843298e50adSKalle Valo 	if (priv->connected) {
2844298e50adSKalle Valo 		priv->connected = false;
2845298e50adSKalle Valo 		eth_zero_addr(priv->bssid);
2846298e50adSKalle Valo 
2847298e50adSKalle Valo 		deauthenticate(usbdev);
2848298e50adSKalle Valo 
2849298e50adSKalle Valo 		cfg80211_disconnected(usbdev->net, 0, NULL, 0, true, GFP_KERNEL);
2850298e50adSKalle Valo 	}
2851298e50adSKalle Valo 
2852298e50adSKalle Valo 	netif_carrier_off(usbdev->net);
2853298e50adSKalle Valo }
2854298e50adSKalle Valo 
rndis_wlan_worker(struct work_struct * work)2855298e50adSKalle Valo static void rndis_wlan_worker(struct work_struct *work)
2856298e50adSKalle Valo {
2857298e50adSKalle Valo 	struct rndis_wlan_private *priv =
2858298e50adSKalle Valo 		container_of(work, struct rndis_wlan_private, work);
2859298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
2860298e50adSKalle Valo 
2861298e50adSKalle Valo 	if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2862298e50adSKalle Valo 		rndis_wlan_do_link_up_work(usbdev);
2863298e50adSKalle Valo 
2864298e50adSKalle Valo 	if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2865298e50adSKalle Valo 		rndis_wlan_do_link_down_work(usbdev);
2866298e50adSKalle Valo 
2867298e50adSKalle Valo 	if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2868298e50adSKalle Valo 		set_multicast_list(usbdev);
2869298e50adSKalle Valo }
2870298e50adSKalle Valo 
rndis_wlan_set_multicast_list(struct net_device * dev)2871298e50adSKalle Valo static void rndis_wlan_set_multicast_list(struct net_device *dev)
2872298e50adSKalle Valo {
2873298e50adSKalle Valo 	struct usbnet *usbdev = netdev_priv(dev);
2874298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2875298e50adSKalle Valo 
2876298e50adSKalle Valo 	if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2877298e50adSKalle Valo 		return;
2878298e50adSKalle Valo 
2879298e50adSKalle Valo 	set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2880298e50adSKalle Valo 	queue_work(priv->workqueue, &priv->work);
2881298e50adSKalle Valo }
2882298e50adSKalle Valo 
rndis_wlan_auth_indication(struct usbnet * usbdev,struct ndis_80211_status_indication * indication,int len)2883298e50adSKalle Valo static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2884298e50adSKalle Valo 				struct ndis_80211_status_indication *indication,
2885298e50adSKalle Valo 				int len)
2886298e50adSKalle Valo {
2887298e50adSKalle Valo 	u8 *buf;
2888298e50adSKalle Valo 	const char *type;
2889298e50adSKalle Valo 	int flags, buflen, key_id;
2890298e50adSKalle Valo 	bool pairwise_error, group_error;
2891298e50adSKalle Valo 	struct ndis_80211_auth_request *auth_req;
2892298e50adSKalle Valo 	enum nl80211_key_type key_type;
2893298e50adSKalle Valo 
2894298e50adSKalle Valo 	/* must have at least one array entry */
2895298e50adSKalle Valo 	if (len < offsetof(struct ndis_80211_status_indication, u) +
2896298e50adSKalle Valo 				sizeof(struct ndis_80211_auth_request)) {
2897298e50adSKalle Valo 		netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2898298e50adSKalle Valo 			    len);
2899298e50adSKalle Valo 		return;
2900298e50adSKalle Valo 	}
2901298e50adSKalle Valo 
2902298e50adSKalle Valo 	buf = (void *)&indication->u.auth_request[0];
2903298e50adSKalle Valo 	buflen = len - offsetof(struct ndis_80211_status_indication, u);
2904298e50adSKalle Valo 
2905298e50adSKalle Valo 	while (buflen >= sizeof(*auth_req)) {
2906298e50adSKalle Valo 		auth_req = (void *)buf;
2907298e50adSKalle Valo 		if (buflen < le32_to_cpu(auth_req->length))
2908298e50adSKalle Valo 			return;
2909298e50adSKalle Valo 		type = "unknown";
2910298e50adSKalle Valo 		flags = le32_to_cpu(auth_req->flags);
2911298e50adSKalle Valo 		pairwise_error = false;
2912298e50adSKalle Valo 		group_error = false;
2913298e50adSKalle Valo 
2914298e50adSKalle Valo 		if (flags & 0x1)
2915298e50adSKalle Valo 			type = "reauth request";
2916298e50adSKalle Valo 		if (flags & 0x2)
2917298e50adSKalle Valo 			type = "key update request";
2918298e50adSKalle Valo 		if (flags & 0x6) {
2919298e50adSKalle Valo 			pairwise_error = true;
2920298e50adSKalle Valo 			type = "pairwise_error";
2921298e50adSKalle Valo 		}
2922298e50adSKalle Valo 		if (flags & 0xe) {
2923298e50adSKalle Valo 			group_error = true;
2924298e50adSKalle Valo 			type = "group_error";
2925298e50adSKalle Valo 		}
2926298e50adSKalle Valo 
2927298e50adSKalle Valo 		netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2928298e50adSKalle Valo 			    type, le32_to_cpu(auth_req->flags));
2929298e50adSKalle Valo 
2930298e50adSKalle Valo 		if (pairwise_error) {
2931298e50adSKalle Valo 			key_type = NL80211_KEYTYPE_PAIRWISE;
2932298e50adSKalle Valo 			key_id = -1;
2933298e50adSKalle Valo 
2934298e50adSKalle Valo 			cfg80211_michael_mic_failure(usbdev->net,
2935298e50adSKalle Valo 							auth_req->bssid,
2936298e50adSKalle Valo 							key_type, key_id, NULL,
2937298e50adSKalle Valo 							GFP_KERNEL);
2938298e50adSKalle Valo 		}
2939298e50adSKalle Valo 
2940298e50adSKalle Valo 		if (group_error) {
2941298e50adSKalle Valo 			key_type = NL80211_KEYTYPE_GROUP;
2942298e50adSKalle Valo 			key_id = -1;
2943298e50adSKalle Valo 
2944298e50adSKalle Valo 			cfg80211_michael_mic_failure(usbdev->net,
2945298e50adSKalle Valo 							auth_req->bssid,
2946298e50adSKalle Valo 							key_type, key_id, NULL,
2947298e50adSKalle Valo 							GFP_KERNEL);
2948298e50adSKalle Valo 		}
2949298e50adSKalle Valo 
2950298e50adSKalle Valo 		buflen -= le32_to_cpu(auth_req->length);
2951298e50adSKalle Valo 		buf += le32_to_cpu(auth_req->length);
2952298e50adSKalle Valo 	}
2953298e50adSKalle Valo }
2954298e50adSKalle Valo 
rndis_wlan_pmkid_cand_list_indication(struct usbnet * usbdev,struct ndis_80211_status_indication * indication,int len)2955298e50adSKalle Valo static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2956298e50adSKalle Valo 				struct ndis_80211_status_indication *indication,
2957298e50adSKalle Valo 				int len)
2958298e50adSKalle Valo {
2959298e50adSKalle Valo 	struct ndis_80211_pmkid_cand_list *cand_list;
2960298e50adSKalle Valo 	int list_len, expected_len, i;
2961298e50adSKalle Valo 
2962298e50adSKalle Valo 	if (len < offsetof(struct ndis_80211_status_indication, u) +
2963298e50adSKalle Valo 				sizeof(struct ndis_80211_pmkid_cand_list)) {
2964298e50adSKalle Valo 		netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2965298e50adSKalle Valo 			    len);
2966298e50adSKalle Valo 		return;
2967298e50adSKalle Valo 	}
2968298e50adSKalle Valo 
2969298e50adSKalle Valo 	list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2970298e50adSKalle Valo 			sizeof(struct ndis_80211_pmkid_candidate);
2971298e50adSKalle Valo 	expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2972298e50adSKalle Valo 			offsetof(struct ndis_80211_status_indication, u);
2973298e50adSKalle Valo 
2974298e50adSKalle Valo 	if (len < expected_len) {
2975298e50adSKalle Valo 		netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2976298e50adSKalle Valo 			    len, expected_len);
2977298e50adSKalle Valo 		return;
2978298e50adSKalle Valo 	}
2979298e50adSKalle Valo 
2980298e50adSKalle Valo 	cand_list = &indication->u.cand_list;
2981298e50adSKalle Valo 
2982298e50adSKalle Valo 	netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2983298e50adSKalle Valo 		    le32_to_cpu(cand_list->version),
2984298e50adSKalle Valo 		    le32_to_cpu(cand_list->num_candidates));
2985298e50adSKalle Valo 
2986298e50adSKalle Valo 	if (le32_to_cpu(cand_list->version) != 1)
2987298e50adSKalle Valo 		return;
2988298e50adSKalle Valo 
2989298e50adSKalle Valo 	for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2990298e50adSKalle Valo 		struct ndis_80211_pmkid_candidate *cand =
2991298e50adSKalle Valo 						&cand_list->candidate_list[i];
2992298e50adSKalle Valo 		bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
2993298e50adSKalle Valo 
2994298e50adSKalle Valo 		netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
2995298e50adSKalle Valo 			   i, le32_to_cpu(cand->flags), preauth, cand->bssid);
2996298e50adSKalle Valo 
2997298e50adSKalle Valo 		cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
2998298e50adSKalle Valo 						preauth, GFP_ATOMIC);
2999298e50adSKalle Valo 	}
3000298e50adSKalle Valo }
3001298e50adSKalle Valo 
rndis_wlan_media_specific_indication(struct usbnet * usbdev,struct rndis_indicate * msg,int buflen)3002298e50adSKalle Valo static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
3003298e50adSKalle Valo 			struct rndis_indicate *msg, int buflen)
3004298e50adSKalle Valo {
3005298e50adSKalle Valo 	struct ndis_80211_status_indication *indication;
3006298e50adSKalle Valo 	unsigned int len, offset;
3007298e50adSKalle Valo 
3008298e50adSKalle Valo 	offset = offsetof(struct rndis_indicate, status) +
3009298e50adSKalle Valo 			le32_to_cpu(msg->offset);
3010298e50adSKalle Valo 	len = le32_to_cpu(msg->length);
3011298e50adSKalle Valo 
3012298e50adSKalle Valo 	if (len < 8) {
3013298e50adSKalle Valo 		netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
3014298e50adSKalle Valo 			    len);
3015298e50adSKalle Valo 		return;
3016298e50adSKalle Valo 	}
3017298e50adSKalle Valo 
3018298e50adSKalle Valo 	if (len > buflen || offset > buflen || offset + len > buflen) {
3019298e50adSKalle Valo 		netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
3020298e50adSKalle Valo 			    offset + len, buflen);
3021298e50adSKalle Valo 		return;
3022298e50adSKalle Valo 	}
3023298e50adSKalle Valo 
3024298e50adSKalle Valo 	indication = (void *)((u8 *)msg + offset);
3025298e50adSKalle Valo 
3026298e50adSKalle Valo 	switch (le32_to_cpu(indication->status_type)) {
3027298e50adSKalle Valo 	case NDIS_80211_STATUSTYPE_RADIOSTATE:
3028298e50adSKalle Valo 		netdev_info(usbdev->net, "radio state indication: %i\n",
3029298e50adSKalle Valo 			    le32_to_cpu(indication->u.radio_status));
3030298e50adSKalle Valo 		return;
3031298e50adSKalle Valo 
3032298e50adSKalle Valo 	case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
3033298e50adSKalle Valo 		netdev_info(usbdev->net, "media stream mode indication: %i\n",
3034298e50adSKalle Valo 			    le32_to_cpu(indication->u.media_stream_mode));
3035298e50adSKalle Valo 		return;
3036298e50adSKalle Valo 
3037298e50adSKalle Valo 	case NDIS_80211_STATUSTYPE_AUTHENTICATION:
3038298e50adSKalle Valo 		rndis_wlan_auth_indication(usbdev, indication, len);
3039298e50adSKalle Valo 		return;
3040298e50adSKalle Valo 
3041298e50adSKalle Valo 	case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
3042298e50adSKalle Valo 		rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
3043298e50adSKalle Valo 		return;
3044298e50adSKalle Valo 
3045298e50adSKalle Valo 	default:
3046298e50adSKalle Valo 		netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
3047298e50adSKalle Valo 			    le32_to_cpu(indication->status_type));
3048298e50adSKalle Valo 	}
3049298e50adSKalle Valo }
3050298e50adSKalle Valo 
rndis_wlan_indication(struct usbnet * usbdev,void * ind,int buflen)3051298e50adSKalle Valo static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
3052298e50adSKalle Valo {
3053298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3054298e50adSKalle Valo 	struct rndis_indicate *msg = ind;
3055298e50adSKalle Valo 
3056298e50adSKalle Valo 	switch (le32_to_cpu(msg->status)) {
3057298e50adSKalle Valo 	case RNDIS_STATUS_MEDIA_CONNECT:
3058298e50adSKalle Valo 		if (priv->current_command_oid == RNDIS_OID_802_11_ADD_KEY) {
3059298e50adSKalle Valo 			/* RNDIS_OID_802_11_ADD_KEY causes sometimes extra
3060298e50adSKalle Valo 			 * "media connect" indications which confuses driver
3061298e50adSKalle Valo 			 * and userspace to think that device is
3062298e50adSKalle Valo 			 * roaming/reassociating when it isn't.
3063298e50adSKalle Valo 			 */
3064298e50adSKalle Valo 			netdev_dbg(usbdev->net, "ignored RNDIS_OID_802_11_ADD_KEY triggered 'media connect'\n");
3065298e50adSKalle Valo 			return;
3066298e50adSKalle Valo 		}
3067298e50adSKalle Valo 
3068298e50adSKalle Valo 		usbnet_pause_rx(usbdev);
3069298e50adSKalle Valo 
3070298e50adSKalle Valo 		netdev_info(usbdev->net, "media connect\n");
3071298e50adSKalle Valo 
3072298e50adSKalle Valo 		/* queue work to avoid recursive calls into rndis_command */
3073298e50adSKalle Valo 		set_bit(WORK_LINK_UP, &priv->work_pending);
3074298e50adSKalle Valo 		queue_work(priv->workqueue, &priv->work);
3075298e50adSKalle Valo 		break;
3076298e50adSKalle Valo 
3077298e50adSKalle Valo 	case RNDIS_STATUS_MEDIA_DISCONNECT:
3078298e50adSKalle Valo 		netdev_info(usbdev->net, "media disconnect\n");
3079298e50adSKalle Valo 
3080298e50adSKalle Valo 		/* queue work to avoid recursive calls into rndis_command */
3081298e50adSKalle Valo 		set_bit(WORK_LINK_DOWN, &priv->work_pending);
3082298e50adSKalle Valo 		queue_work(priv->workqueue, &priv->work);
3083298e50adSKalle Valo 		break;
3084298e50adSKalle Valo 
3085298e50adSKalle Valo 	case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
3086298e50adSKalle Valo 		rndis_wlan_media_specific_indication(usbdev, msg, buflen);
3087298e50adSKalle Valo 		break;
3088298e50adSKalle Valo 
3089298e50adSKalle Valo 	default:
3090298e50adSKalle Valo 		netdev_info(usbdev->net, "indication: 0x%08x\n",
3091298e50adSKalle Valo 			    le32_to_cpu(msg->status));
3092298e50adSKalle Valo 		break;
3093298e50adSKalle Valo 	}
3094298e50adSKalle Valo }
3095298e50adSKalle Valo 
rndis_wlan_get_caps(struct usbnet * usbdev,struct wiphy * wiphy)3096298e50adSKalle Valo static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
3097298e50adSKalle Valo {
3098298e50adSKalle Valo 	struct {
3099298e50adSKalle Valo 		__le32	num_items;
3100298e50adSKalle Valo 		__le32	items[8];
3101298e50adSKalle Valo 	} networks_supported;
3102298e50adSKalle Valo 	struct ndis_80211_capability caps;
3103298e50adSKalle Valo 	int len, retval, i, n;
3104298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3105298e50adSKalle Valo 
3106298e50adSKalle Valo 	/* determine supported modes */
3107298e50adSKalle Valo 	len = sizeof(networks_supported);
3108298e50adSKalle Valo 	retval = rndis_query_oid(usbdev,
3109298e50adSKalle Valo 				 RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED,
3110298e50adSKalle Valo 				 &networks_supported, &len);
3111298e50adSKalle Valo 	if (!retval) {
3112298e50adSKalle Valo 		n = le32_to_cpu(networks_supported.num_items);
3113298e50adSKalle Valo 		if (n > 8)
3114298e50adSKalle Valo 			n = 8;
3115298e50adSKalle Valo 		for (i = 0; i < n; i++) {
3116298e50adSKalle Valo 			switch (le32_to_cpu(networks_supported.items[i])) {
3117298e50adSKalle Valo 			case NDIS_80211_TYPE_FREQ_HOP:
3118298e50adSKalle Valo 			case NDIS_80211_TYPE_DIRECT_SEQ:
3119298e50adSKalle Valo 				priv->caps |= CAP_MODE_80211B;
3120298e50adSKalle Valo 				break;
3121298e50adSKalle Valo 			case NDIS_80211_TYPE_OFDM_A:
3122298e50adSKalle Valo 				priv->caps |= CAP_MODE_80211A;
3123298e50adSKalle Valo 				break;
3124298e50adSKalle Valo 			case NDIS_80211_TYPE_OFDM_G:
3125298e50adSKalle Valo 				priv->caps |= CAP_MODE_80211G;
3126298e50adSKalle Valo 				break;
3127298e50adSKalle Valo 			}
3128298e50adSKalle Valo 		}
3129298e50adSKalle Valo 	}
3130298e50adSKalle Valo 
3131298e50adSKalle Valo 	/* get device 802.11 capabilities, number of PMKIDs */
3132298e50adSKalle Valo 	len = sizeof(caps);
3133298e50adSKalle Valo 	retval = rndis_query_oid(usbdev,
3134298e50adSKalle Valo 				 RNDIS_OID_802_11_CAPABILITY,
3135298e50adSKalle Valo 				 &caps, &len);
3136298e50adSKalle Valo 	if (!retval) {
3137298e50adSKalle Valo 		netdev_dbg(usbdev->net, "RNDIS_OID_802_11_CAPABILITY -> len %d, "
3138298e50adSKalle Valo 				"ver %d, pmkids %d, auth-encr-pairs %d\n",
3139298e50adSKalle Valo 				le32_to_cpu(caps.length),
3140298e50adSKalle Valo 				le32_to_cpu(caps.version),
3141298e50adSKalle Valo 				le32_to_cpu(caps.num_pmkids),
3142298e50adSKalle Valo 				le32_to_cpu(caps.num_auth_encr_pair));
3143298e50adSKalle Valo 		wiphy->max_num_pmkids = le32_to_cpu(caps.num_pmkids);
3144298e50adSKalle Valo 	} else
3145298e50adSKalle Valo 		wiphy->max_num_pmkids = 0;
3146298e50adSKalle Valo 
3147298e50adSKalle Valo 	return retval;
3148298e50adSKalle Valo }
3149298e50adSKalle Valo 
rndis_do_cqm(struct usbnet * usbdev,s32 rssi)3150298e50adSKalle Valo static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
3151298e50adSKalle Valo {
3152298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3153298e50adSKalle Valo 	enum nl80211_cqm_rssi_threshold_event event;
3154298e50adSKalle Valo 	int thold, hyst, last_event;
3155298e50adSKalle Valo 
3156298e50adSKalle Valo 	if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
3157298e50adSKalle Valo 		return;
3158298e50adSKalle Valo 	if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
3159298e50adSKalle Valo 		return;
3160298e50adSKalle Valo 
3161298e50adSKalle Valo 	last_event = priv->last_cqm_event_rssi;
3162298e50adSKalle Valo 	thold = priv->cqm_rssi_thold;
3163298e50adSKalle Valo 	hyst = priv->cqm_rssi_hyst;
3164298e50adSKalle Valo 
3165298e50adSKalle Valo 	if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
3166298e50adSKalle Valo 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
3167298e50adSKalle Valo 	else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
3168298e50adSKalle Valo 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
3169298e50adSKalle Valo 	else
3170298e50adSKalle Valo 		return;
3171298e50adSKalle Valo 
3172298e50adSKalle Valo 	priv->last_cqm_event_rssi = rssi;
3173298e50adSKalle Valo 	cfg80211_cqm_rssi_notify(usbdev->net, event, rssi, GFP_KERNEL);
3174298e50adSKalle Valo }
3175298e50adSKalle Valo 
3176298e50adSKalle Valo #define DEVICE_POLLER_JIFFIES (HZ)
rndis_device_poller(struct work_struct * work)3177298e50adSKalle Valo static void rndis_device_poller(struct work_struct *work)
3178298e50adSKalle Valo {
3179298e50adSKalle Valo 	struct rndis_wlan_private *priv =
3180298e50adSKalle Valo 		container_of(work, struct rndis_wlan_private,
3181298e50adSKalle Valo 							dev_poller_work.work);
3182298e50adSKalle Valo 	struct usbnet *usbdev = priv->usbdev;
3183298e50adSKalle Valo 	__le32 rssi, tmp;
3184298e50adSKalle Valo 	int len, ret, j;
3185298e50adSKalle Valo 	int update_jiffies = DEVICE_POLLER_JIFFIES;
3186298e50adSKalle Valo 	void *buf;
3187298e50adSKalle Valo 
3188298e50adSKalle Valo 	/* Only check/do workaround when connected. Calling is_associated()
3189298e50adSKalle Valo 	 * also polls device with rndis_command() and catches for media link
3190298e50adSKalle Valo 	 * indications.
3191298e50adSKalle Valo 	 */
3192298e50adSKalle Valo 	if (!is_associated(usbdev)) {
3193298e50adSKalle Valo 		/* Workaround bad scanning in BCM4320a devices with active
3194298e50adSKalle Valo 		 * background scanning when not associated.
3195298e50adSKalle Valo 		 */
3196298e50adSKalle Valo 		if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
3197298e50adSKalle Valo 		    !priv->scan_request) {
3198298e50adSKalle Valo 			/* Get previous scan results */
3199298e50adSKalle Valo 			rndis_check_bssid_list(usbdev, NULL, NULL);
3200298e50adSKalle Valo 
3201298e50adSKalle Valo 			/* Initiate new scan */
3202298e50adSKalle Valo 			rndis_start_bssid_list_scan(usbdev);
3203298e50adSKalle Valo 		}
3204298e50adSKalle Valo 
3205298e50adSKalle Valo 		goto end;
3206298e50adSKalle Valo 	}
3207298e50adSKalle Valo 
3208298e50adSKalle Valo 	len = sizeof(rssi);
3209298e50adSKalle Valo 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
3210298e50adSKalle Valo 			      &rssi, &len);
3211298e50adSKalle Valo 	if (ret == 0) {
3212298e50adSKalle Valo 		priv->last_qual = level_to_qual(le32_to_cpu(rssi));
3213298e50adSKalle Valo 		rndis_do_cqm(usbdev, le32_to_cpu(rssi));
3214298e50adSKalle Valo 	}
3215298e50adSKalle Valo 
3216298e50adSKalle Valo 	netdev_dbg(usbdev->net, "dev-poller: RNDIS_OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
3217298e50adSKalle Valo 		   ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
3218298e50adSKalle Valo 
3219298e50adSKalle Valo 	/* Workaround transfer stalls on poor quality links.
3220298e50adSKalle Valo 	 * TODO: find right way to fix these stalls (as stalls do not happen
3221298e50adSKalle Valo 	 * with ndiswrapper/windows driver). */
3222298e50adSKalle Valo 	if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
3223298e50adSKalle Valo 		/* Decrease stats worker interval to catch stalls.
3224298e50adSKalle Valo 		 * faster. Faster than 400-500ms causes packet loss,
3225298e50adSKalle Valo 		 * Slower doesn't catch stalls fast enough.
3226298e50adSKalle Valo 		 */
3227298e50adSKalle Valo 		j = msecs_to_jiffies(priv->param_workaround_interval);
3228298e50adSKalle Valo 		if (j > DEVICE_POLLER_JIFFIES)
3229298e50adSKalle Valo 			j = DEVICE_POLLER_JIFFIES;
3230298e50adSKalle Valo 		else if (j <= 0)
3231298e50adSKalle Valo 			j = 1;
3232298e50adSKalle Valo 		update_jiffies = j;
3233298e50adSKalle Valo 
3234298e50adSKalle Valo 		/* Send scan OID. Use of both OIDs is required to get device
3235298e50adSKalle Valo 		 * working.
3236298e50adSKalle Valo 		 */
3237298e50adSKalle Valo 		tmp = cpu_to_le32(1);
3238298e50adSKalle Valo 		rndis_set_oid(usbdev,
3239298e50adSKalle Valo 			      RNDIS_OID_802_11_BSSID_LIST_SCAN,
3240298e50adSKalle Valo 			      &tmp, sizeof(tmp));
3241298e50adSKalle Valo 
3242298e50adSKalle Valo 		len = CONTROL_BUFFER_SIZE;
3243298e50adSKalle Valo 		buf = kmalloc(len, GFP_KERNEL);
3244298e50adSKalle Valo 		if (!buf)
3245298e50adSKalle Valo 			goto end;
3246298e50adSKalle Valo 
3247298e50adSKalle Valo 		rndis_query_oid(usbdev,
3248298e50adSKalle Valo 				RNDIS_OID_802_11_BSSID_LIST,
3249298e50adSKalle Valo 				buf, &len);
3250298e50adSKalle Valo 		kfree(buf);
3251298e50adSKalle Valo 	}
3252298e50adSKalle Valo 
3253298e50adSKalle Valo end:
3254298e50adSKalle Valo 	if (update_jiffies >= HZ)
3255298e50adSKalle Valo 		update_jiffies = round_jiffies_relative(update_jiffies);
3256298e50adSKalle Valo 	else {
3257298e50adSKalle Valo 		j = round_jiffies_relative(update_jiffies);
3258298e50adSKalle Valo 		if (abs(j - update_jiffies) <= 10)
3259298e50adSKalle Valo 			update_jiffies = j;
3260298e50adSKalle Valo 	}
3261298e50adSKalle Valo 
3262298e50adSKalle Valo 	queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3263298e50adSKalle Valo 								update_jiffies);
3264298e50adSKalle Valo }
3265298e50adSKalle Valo 
3266298e50adSKalle Valo /*
3267298e50adSKalle Valo  * driver/device initialization
3268298e50adSKalle Valo  */
rndis_copy_module_params(struct usbnet * usbdev,int device_type)3269298e50adSKalle Valo static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
3270298e50adSKalle Valo {
3271298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3272298e50adSKalle Valo 
3273298e50adSKalle Valo 	priv->device_type = device_type;
3274298e50adSKalle Valo 
3275298e50adSKalle Valo 	priv->param_country[0] = modparam_country[0];
3276298e50adSKalle Valo 	priv->param_country[1] = modparam_country[1];
3277298e50adSKalle Valo 	priv->param_country[2] = 0;
3278298e50adSKalle Valo 	priv->param_frameburst   = modparam_frameburst;
3279298e50adSKalle Valo 	priv->param_afterburner  = modparam_afterburner;
3280298e50adSKalle Valo 	priv->param_power_save   = modparam_power_save;
3281298e50adSKalle Valo 	priv->param_power_output = modparam_power_output;
3282298e50adSKalle Valo 	priv->param_roamtrigger  = modparam_roamtrigger;
3283298e50adSKalle Valo 	priv->param_roamdelta    = modparam_roamdelta;
3284298e50adSKalle Valo 
3285298e50adSKalle Valo 	priv->param_country[0] = toupper(priv->param_country[0]);
3286298e50adSKalle Valo 	priv->param_country[1] = toupper(priv->param_country[1]);
3287298e50adSKalle Valo 	/* doesn't support EU as country code, use FI instead */
3288298e50adSKalle Valo 	if (!strcmp(priv->param_country, "EU"))
3289298e50adSKalle Valo 		strcpy(priv->param_country, "FI");
3290298e50adSKalle Valo 
3291298e50adSKalle Valo 	if (priv->param_power_save < 0)
3292298e50adSKalle Valo 		priv->param_power_save = 0;
3293298e50adSKalle Valo 	else if (priv->param_power_save > 2)
3294298e50adSKalle Valo 		priv->param_power_save = 2;
3295298e50adSKalle Valo 
3296298e50adSKalle Valo 	if (priv->param_power_output < 0)
3297298e50adSKalle Valo 		priv->param_power_output = 0;
3298298e50adSKalle Valo 	else if (priv->param_power_output > 3)
3299298e50adSKalle Valo 		priv->param_power_output = 3;
3300298e50adSKalle Valo 
3301298e50adSKalle Valo 	if (priv->param_roamtrigger < -80)
3302298e50adSKalle Valo 		priv->param_roamtrigger = -80;
3303298e50adSKalle Valo 	else if (priv->param_roamtrigger > -60)
3304298e50adSKalle Valo 		priv->param_roamtrigger = -60;
3305298e50adSKalle Valo 
3306298e50adSKalle Valo 	if (priv->param_roamdelta < 0)
3307298e50adSKalle Valo 		priv->param_roamdelta = 0;
3308298e50adSKalle Valo 	else if (priv->param_roamdelta > 2)
3309298e50adSKalle Valo 		priv->param_roamdelta = 2;
3310298e50adSKalle Valo 
3311298e50adSKalle Valo 	if (modparam_workaround_interval < 0)
3312298e50adSKalle Valo 		priv->param_workaround_interval = 500;
3313298e50adSKalle Valo 	else
3314298e50adSKalle Valo 		priv->param_workaround_interval = modparam_workaround_interval;
3315298e50adSKalle Valo }
3316298e50adSKalle Valo 
unknown_early_init(struct usbnet * usbdev)3317298e50adSKalle Valo static int unknown_early_init(struct usbnet *usbdev)
3318298e50adSKalle Valo {
3319298e50adSKalle Valo 	/* copy module parameters for unknown so that iwconfig reports txpower
3320298e50adSKalle Valo 	 * and workaround parameter is copied to private structure correctly.
3321298e50adSKalle Valo 	 */
3322298e50adSKalle Valo 	rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
3323298e50adSKalle Valo 
3324298e50adSKalle Valo 	/* This is unknown device, so do not try set configuration parameters.
3325298e50adSKalle Valo 	 */
3326298e50adSKalle Valo 
3327298e50adSKalle Valo 	return 0;
3328298e50adSKalle Valo }
3329298e50adSKalle Valo 
bcm4320a_early_init(struct usbnet * usbdev)3330298e50adSKalle Valo static int bcm4320a_early_init(struct usbnet *usbdev)
3331298e50adSKalle Valo {
3332298e50adSKalle Valo 	/* copy module parameters for bcm4320a so that iwconfig reports txpower
3333298e50adSKalle Valo 	 * and workaround parameter is copied to private structure correctly.
3334298e50adSKalle Valo 	 */
3335298e50adSKalle Valo 	rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
3336298e50adSKalle Valo 
3337298e50adSKalle Valo 	/* bcm4320a doesn't handle configuration parameters well. Try
3338298e50adSKalle Valo 	 * set any and you get partially zeroed mac and broken device.
3339298e50adSKalle Valo 	 */
3340298e50adSKalle Valo 
3341298e50adSKalle Valo 	return 0;
3342298e50adSKalle Valo }
3343298e50adSKalle Valo 
bcm4320b_early_init(struct usbnet * usbdev)3344298e50adSKalle Valo static int bcm4320b_early_init(struct usbnet *usbdev)
3345298e50adSKalle Valo {
3346298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3347298e50adSKalle Valo 	char buf[8];
3348298e50adSKalle Valo 
3349298e50adSKalle Valo 	rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
3350298e50adSKalle Valo 
3351298e50adSKalle Valo 	/* Early initialization settings, setting these won't have effect
3352298e50adSKalle Valo 	 * if called after generic_rndis_bind().
3353298e50adSKalle Valo 	 */
3354298e50adSKalle Valo 
3355298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3356298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "FrameBursting",
3357298e50adSKalle Valo 					priv->param_frameburst ? "1" : "0");
3358298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "Afterburner",
3359298e50adSKalle Valo 					priv->param_afterburner ? "1" : "0");
3360298e50adSKalle Valo 	sprintf(buf, "%d", priv->param_power_save);
3361298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3362298e50adSKalle Valo 	sprintf(buf, "%d", priv->param_power_output);
3363298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3364298e50adSKalle Valo 	sprintf(buf, "%d", priv->param_roamtrigger);
3365298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3366298e50adSKalle Valo 	sprintf(buf, "%d", priv->param_roamdelta);
3367298e50adSKalle Valo 	rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3368298e50adSKalle Valo 
3369298e50adSKalle Valo 	return 0;
3370298e50adSKalle Valo }
3371298e50adSKalle Valo 
3372298e50adSKalle Valo /* same as rndis_netdev_ops but with local multicast handler */
3373298e50adSKalle Valo static const struct net_device_ops rndis_wlan_netdev_ops = {
3374298e50adSKalle Valo 	.ndo_open		= usbnet_open,
3375298e50adSKalle Valo 	.ndo_stop		= usbnet_stop,
3376298e50adSKalle Valo 	.ndo_start_xmit		= usbnet_start_xmit,
3377298e50adSKalle Valo 	.ndo_tx_timeout		= usbnet_tx_timeout,
3378298e50adSKalle Valo 	.ndo_get_stats64	= dev_get_tstats64,
3379298e50adSKalle Valo 	.ndo_set_mac_address 	= eth_mac_addr,
3380298e50adSKalle Valo 	.ndo_validate_addr	= eth_validate_addr,
3381298e50adSKalle Valo 	.ndo_set_rx_mode	= rndis_wlan_set_multicast_list,
3382298e50adSKalle Valo };
3383298e50adSKalle Valo 
rndis_wlan_bind(struct usbnet * usbdev,struct usb_interface * intf)3384298e50adSKalle Valo static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3385298e50adSKalle Valo {
3386298e50adSKalle Valo 	struct wiphy *wiphy;
3387298e50adSKalle Valo 	struct rndis_wlan_private *priv;
3388298e50adSKalle Valo 	int retval, len;
3389298e50adSKalle Valo 	__le32 tmp;
3390298e50adSKalle Valo 
3391298e50adSKalle Valo 	/* allocate wiphy and rndis private data
3392298e50adSKalle Valo 	 * NOTE: We only support a single virtual interface, so wiphy
3393298e50adSKalle Valo 	 * and wireless_dev are somewhat synonymous for this device.
3394298e50adSKalle Valo 	 */
3395298e50adSKalle Valo 	wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3396298e50adSKalle Valo 	if (!wiphy)
3397298e50adSKalle Valo 		return -ENOMEM;
3398298e50adSKalle Valo 
3399298e50adSKalle Valo 	priv = wiphy_priv(wiphy);
3400298e50adSKalle Valo 	usbdev->net->ieee80211_ptr = &priv->wdev;
3401298e50adSKalle Valo 	priv->wdev.wiphy = wiphy;
3402298e50adSKalle Valo 	priv->wdev.iftype = NL80211_IFTYPE_STATION;
3403298e50adSKalle Valo 
3404298e50adSKalle Valo 	/* These have to be initialized before calling generic_rndis_bind().
3405298e50adSKalle Valo 	 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3406298e50adSKalle Valo 	 */
3407298e50adSKalle Valo 	usbdev->driver_priv = priv;
3408298e50adSKalle Valo 	priv->usbdev = usbdev;
3409298e50adSKalle Valo 
3410298e50adSKalle Valo 	mutex_init(&priv->command_lock);
3411298e50adSKalle Valo 
3412298e50adSKalle Valo 	/* because rndis_command() sleeps we need to use workqueue */
3413298e50adSKalle Valo 	priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3414298e50adSKalle Valo 	if (!priv->workqueue) {
3415298e50adSKalle Valo 		wiphy_free(wiphy);
3416298e50adSKalle Valo 		return -ENOMEM;
3417298e50adSKalle Valo 	}
3418298e50adSKalle Valo 	INIT_WORK(&priv->work, rndis_wlan_worker);
3419298e50adSKalle Valo 	INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3420298e50adSKalle Valo 	INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3421298e50adSKalle Valo 
3422298e50adSKalle Valo 	/* try bind rndis_host */
3423298e50adSKalle Valo 	retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3424298e50adSKalle Valo 	if (retval < 0)
3425298e50adSKalle Valo 		goto fail;
3426298e50adSKalle Valo 
3427298e50adSKalle Valo 	/* generic_rndis_bind set packet filter to multicast_all+
3428298e50adSKalle Valo 	 * promisc mode which doesn't work well for our devices (device
3429298e50adSKalle Valo 	 * picks up rssi to closest station instead of to access point).
3430298e50adSKalle Valo 	 *
3431298e50adSKalle Valo 	 * rndis_host wants to avoid all OID as much as possible
3432298e50adSKalle Valo 	 * so do promisc/multicast handling in rndis_wlan.
3433298e50adSKalle Valo 	 */
3434298e50adSKalle Valo 	usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3435298e50adSKalle Valo 
3436298e50adSKalle Valo 	tmp = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST);
3437298e50adSKalle Valo 	retval = rndis_set_oid(usbdev,
3438298e50adSKalle Valo 			       RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
3439298e50adSKalle Valo 			       &tmp, sizeof(tmp));
3440298e50adSKalle Valo 
3441298e50adSKalle Valo 	len = sizeof(tmp);
3442298e50adSKalle Valo 	retval = rndis_query_oid(usbdev,
3443298e50adSKalle Valo 				 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
3444298e50adSKalle Valo 				 &tmp, &len);
3445298e50adSKalle Valo 	priv->multicast_size = le32_to_cpu(tmp);
3446298e50adSKalle Valo 	if (retval < 0 || priv->multicast_size < 0)
3447298e50adSKalle Valo 		priv->multicast_size = 0;
3448298e50adSKalle Valo 	if (priv->multicast_size > 0)
3449298e50adSKalle Valo 		usbdev->net->flags |= IFF_MULTICAST;
3450298e50adSKalle Valo 	else
3451298e50adSKalle Valo 		usbdev->net->flags &= ~IFF_MULTICAST;
3452298e50adSKalle Valo 
3453298e50adSKalle Valo 	/* fill-out wiphy structure and register w/ cfg80211 */
3454298e50adSKalle Valo 	memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3455298e50adSKalle Valo 	wiphy->privid = rndis_wiphy_privid;
3456298e50adSKalle Valo 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3457298e50adSKalle Valo 					| BIT(NL80211_IFTYPE_ADHOC);
3458298e50adSKalle Valo 	wiphy->max_scan_ssids = 1;
3459298e50adSKalle Valo 
3460298e50adSKalle Valo 	/* TODO: fill-out band/encr information based on priv->caps */
3461298e50adSKalle Valo 	rndis_wlan_get_caps(usbdev, wiphy);
3462298e50adSKalle Valo 
3463298e50adSKalle Valo 	memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3464298e50adSKalle Valo 	memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3465298e50adSKalle Valo 	priv->band.channels = priv->channels;
3466298e50adSKalle Valo 	priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3467298e50adSKalle Valo 	priv->band.bitrates = priv->rates;
3468298e50adSKalle Valo 	priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3469298e50adSKalle Valo 	wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
3470298e50adSKalle Valo 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3471298e50adSKalle Valo 
3472298e50adSKalle Valo 	memcpy(priv->cipher_suites, rndis_cipher_suites,
3473298e50adSKalle Valo 						sizeof(rndis_cipher_suites));
3474298e50adSKalle Valo 	wiphy->cipher_suites = priv->cipher_suites;
3475298e50adSKalle Valo 	wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3476298e50adSKalle Valo 
3477298e50adSKalle Valo 	set_wiphy_dev(wiphy, &usbdev->udev->dev);
3478298e50adSKalle Valo 
3479298e50adSKalle Valo 	if (wiphy_register(wiphy)) {
3480298e50adSKalle Valo 		retval = -ENODEV;
3481298e50adSKalle Valo 		goto fail;
3482298e50adSKalle Valo 	}
3483298e50adSKalle Valo 
3484298e50adSKalle Valo 	set_default_iw_params(usbdev);
3485298e50adSKalle Valo 
3486298e50adSKalle Valo 	priv->power_mode = -1;
3487298e50adSKalle Valo 
3488298e50adSKalle Valo 	/* set default rts/frag */
3489298e50adSKalle Valo 	rndis_set_wiphy_params(wiphy,
3490298e50adSKalle Valo 			WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3491298e50adSKalle Valo 
3492298e50adSKalle Valo 	/* turn radio off on init */
3493298e50adSKalle Valo 	priv->radio_on = false;
3494298e50adSKalle Valo 	disassociate(usbdev, false);
3495298e50adSKalle Valo 	netif_carrier_off(usbdev->net);
3496298e50adSKalle Valo 
3497298e50adSKalle Valo 	return 0;
3498298e50adSKalle Valo 
3499298e50adSKalle Valo fail:
3500298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->dev_poller_work);
3501298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->scan_work);
3502298e50adSKalle Valo 	cancel_work_sync(&priv->work);
3503298e50adSKalle Valo 	destroy_workqueue(priv->workqueue);
3504298e50adSKalle Valo 
3505298e50adSKalle Valo 	wiphy_free(wiphy);
3506298e50adSKalle Valo 	return retval;
3507298e50adSKalle Valo }
3508298e50adSKalle Valo 
rndis_wlan_unbind(struct usbnet * usbdev,struct usb_interface * intf)3509298e50adSKalle Valo static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3510298e50adSKalle Valo {
3511298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3512298e50adSKalle Valo 
3513298e50adSKalle Valo 	/* turn radio off */
3514298e50adSKalle Valo 	disassociate(usbdev, false);
3515298e50adSKalle Valo 
3516298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->dev_poller_work);
3517298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->scan_work);
3518298e50adSKalle Valo 	cancel_work_sync(&priv->work);
3519298e50adSKalle Valo 	destroy_workqueue(priv->workqueue);
3520298e50adSKalle Valo 
3521298e50adSKalle Valo 	rndis_unbind(usbdev, intf);
3522298e50adSKalle Valo 
3523298e50adSKalle Valo 	wiphy_unregister(priv->wdev.wiphy);
3524298e50adSKalle Valo 	wiphy_free(priv->wdev.wiphy);
3525298e50adSKalle Valo }
3526298e50adSKalle Valo 
rndis_wlan_reset(struct usbnet * usbdev)3527298e50adSKalle Valo static int rndis_wlan_reset(struct usbnet *usbdev)
3528298e50adSKalle Valo {
3529298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3530298e50adSKalle Valo 	int retval;
3531298e50adSKalle Valo 
3532298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s()\n", __func__);
3533298e50adSKalle Valo 
3534298e50adSKalle Valo 	retval = rndis_reset(usbdev);
3535298e50adSKalle Valo 	if (retval)
3536298e50adSKalle Valo 		netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3537298e50adSKalle Valo 
3538298e50adSKalle Valo 	/* rndis_reset cleared multicast list, so restore here.
3539298e50adSKalle Valo 	   (set_multicast_list() also turns on current packet filter) */
3540298e50adSKalle Valo 	set_multicast_list(usbdev);
3541298e50adSKalle Valo 
3542298e50adSKalle Valo 	queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3543298e50adSKalle Valo 		round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3544298e50adSKalle Valo 
3545298e50adSKalle Valo 	return deauthenticate(usbdev);
3546298e50adSKalle Valo }
3547298e50adSKalle Valo 
rndis_wlan_stop(struct usbnet * usbdev)3548298e50adSKalle Valo static int rndis_wlan_stop(struct usbnet *usbdev)
3549298e50adSKalle Valo {
3550298e50adSKalle Valo 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3551298e50adSKalle Valo 	int retval;
3552298e50adSKalle Valo 	__le32 filter;
3553298e50adSKalle Valo 
3554298e50adSKalle Valo 	netdev_dbg(usbdev->net, "%s()\n", __func__);
3555298e50adSKalle Valo 
3556298e50adSKalle Valo 	retval = disassociate(usbdev, false);
3557298e50adSKalle Valo 
3558298e50adSKalle Valo 	priv->work_pending = 0;
3559298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->dev_poller_work);
3560298e50adSKalle Valo 	cancel_delayed_work_sync(&priv->scan_work);
3561298e50adSKalle Valo 	cancel_work_sync(&priv->work);
3562298e50adSKalle Valo 	flush_workqueue(priv->workqueue);
3563298e50adSKalle Valo 
3564298e50adSKalle Valo 	if (priv->scan_request) {
3565298e50adSKalle Valo 		struct cfg80211_scan_info info = {
3566298e50adSKalle Valo 			.aborted = true,
3567298e50adSKalle Valo 		};
3568298e50adSKalle Valo 
3569298e50adSKalle Valo 		cfg80211_scan_done(priv->scan_request, &info);
3570298e50adSKalle Valo 		priv->scan_request = NULL;
3571298e50adSKalle Valo 	}
3572298e50adSKalle Valo 
3573298e50adSKalle Valo 	/* Set current packet filter zero to block receiving data packets from
3574298e50adSKalle Valo 	   device. */
3575298e50adSKalle Valo 	filter = 0;
3576298e50adSKalle Valo 	rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
3577298e50adSKalle Valo 								sizeof(filter));
3578298e50adSKalle Valo 
3579298e50adSKalle Valo 	return retval;
3580298e50adSKalle Valo }
3581298e50adSKalle Valo 
3582298e50adSKalle Valo static const struct driver_info	bcm4320b_info = {
3583298e50adSKalle Valo 	.description =	"Wireless RNDIS device, BCM4320b based",
3584298e50adSKalle Valo 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3585298e50adSKalle Valo 				FLAG_AVOID_UNLINK_URBS,
3586298e50adSKalle Valo 	.bind =		rndis_wlan_bind,
3587298e50adSKalle Valo 	.unbind =	rndis_wlan_unbind,
3588298e50adSKalle Valo 	.status =	rndis_status,
3589298e50adSKalle Valo 	.rx_fixup =	rndis_rx_fixup,
3590298e50adSKalle Valo 	.tx_fixup =	rndis_tx_fixup,
3591298e50adSKalle Valo 	.reset =	rndis_wlan_reset,
3592298e50adSKalle Valo 	.stop =		rndis_wlan_stop,
3593298e50adSKalle Valo 	.early_init =	bcm4320b_early_init,
3594298e50adSKalle Valo 	.indication =	rndis_wlan_indication,
3595298e50adSKalle Valo };
3596298e50adSKalle Valo 
3597298e50adSKalle Valo static const struct driver_info	bcm4320a_info = {
3598298e50adSKalle Valo 	.description =	"Wireless RNDIS device, BCM4320a based",
3599298e50adSKalle Valo 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3600298e50adSKalle Valo 				FLAG_AVOID_UNLINK_URBS,
3601298e50adSKalle Valo 	.bind =		rndis_wlan_bind,
3602298e50adSKalle Valo 	.unbind =	rndis_wlan_unbind,
3603298e50adSKalle Valo 	.status =	rndis_status,
3604298e50adSKalle Valo 	.rx_fixup =	rndis_rx_fixup,
3605298e50adSKalle Valo 	.tx_fixup =	rndis_tx_fixup,
3606298e50adSKalle Valo 	.reset =	rndis_wlan_reset,
3607298e50adSKalle Valo 	.stop =		rndis_wlan_stop,
3608298e50adSKalle Valo 	.early_init =	bcm4320a_early_init,
3609298e50adSKalle Valo 	.indication =	rndis_wlan_indication,
3610298e50adSKalle Valo };
3611298e50adSKalle Valo 
3612298e50adSKalle Valo static const struct driver_info rndis_wlan_info = {
3613298e50adSKalle Valo 	.description =	"Wireless RNDIS device",
3614298e50adSKalle Valo 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3615298e50adSKalle Valo 				FLAG_AVOID_UNLINK_URBS,
3616298e50adSKalle Valo 	.bind =		rndis_wlan_bind,
3617298e50adSKalle Valo 	.unbind =	rndis_wlan_unbind,
3618298e50adSKalle Valo 	.status =	rndis_status,
3619298e50adSKalle Valo 	.rx_fixup =	rndis_rx_fixup,
3620298e50adSKalle Valo 	.tx_fixup =	rndis_tx_fixup,
3621298e50adSKalle Valo 	.reset =	rndis_wlan_reset,
3622298e50adSKalle Valo 	.stop =		rndis_wlan_stop,
3623298e50adSKalle Valo 	.early_init =	unknown_early_init,
3624298e50adSKalle Valo 	.indication =	rndis_wlan_indication,
3625298e50adSKalle Valo };
3626298e50adSKalle Valo 
3627298e50adSKalle Valo /*-------------------------------------------------------------------------*/
3628298e50adSKalle Valo 
3629298e50adSKalle Valo static const struct usb_device_id products [] = {
3630298e50adSKalle Valo #define	RNDIS_MASTER_INTERFACE \
3631298e50adSKalle Valo 	.bInterfaceClass	= USB_CLASS_COMM, \
3632298e50adSKalle Valo 	.bInterfaceSubClass	= 2 /* ACM */, \
3633298e50adSKalle Valo 	.bInterfaceProtocol	= 0x0ff
3634298e50adSKalle Valo 
3635298e50adSKalle Valo /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3636298e50adSKalle Valo  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3637298e50adSKalle Valo  */
3638298e50adSKalle Valo {
3639298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3640298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3641298e50adSKalle Valo 	.idVendor		= 0x0411,
3642298e50adSKalle Valo 	.idProduct		= 0x00bc,	/* Buffalo WLI-U2-KG125S */
3643298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3644298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3645298e50adSKalle Valo }, {
3646298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3647298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3648298e50adSKalle Valo 	.idVendor		= 0x0baf,
3649298e50adSKalle Valo 	.idProduct		= 0x011b,	/* U.S. Robotics USR5421 */
3650298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3651298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3652298e50adSKalle Valo }, {
3653298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3654298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3655298e50adSKalle Valo 	.idVendor		= 0x050d,
3656298e50adSKalle Valo 	.idProduct		= 0x011b,	/* Belkin F5D7051 */
3657298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3658298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3659298e50adSKalle Valo }, {
3660298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3661298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3662298e50adSKalle Valo 	.idVendor		= 0x1799,	/* Belkin has two vendor ids */
3663298e50adSKalle Valo 	.idProduct		= 0x011b,	/* Belkin F5D7051 */
3664298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3665298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3666298e50adSKalle Valo }, {
3667298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3668298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3669298e50adSKalle Valo 	.idVendor		= 0x13b1,
3670298e50adSKalle Valo 	.idProduct		= 0x0014,	/* Linksys WUSB54GSv2 */
3671298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3672298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3673298e50adSKalle Valo }, {
3674298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3675298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3676298e50adSKalle Valo 	.idVendor		= 0x13b1,
3677298e50adSKalle Valo 	.idProduct		= 0x0026,	/* Linksys WUSB54GSC */
3678298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3679298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3680298e50adSKalle Valo }, {
3681298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3682298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3683298e50adSKalle Valo 	.idVendor		= 0x0b05,
3684298e50adSKalle Valo 	.idProduct		= 0x1717,	/* Asus WL169gE */
3685298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3686298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3687298e50adSKalle Valo }, {
3688298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3689298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3690298e50adSKalle Valo 	.idVendor		= 0x0a5c,
3691298e50adSKalle Valo 	.idProduct		= 0xd11b,	/* Eminent EM4045 */
3692298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3693298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3694298e50adSKalle Valo }, {
3695298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3696298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3697298e50adSKalle Valo 	.idVendor		= 0x1690,
3698298e50adSKalle Valo 	.idProduct		= 0x0715,	/* BT Voyager 1055 */
3699298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3700298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320b_info,
3701298e50adSKalle Valo },
3702298e50adSKalle Valo /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3703298e50adSKalle Valo  * parameters available, hardware probably contain older firmware version with
3704298e50adSKalle Valo  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3705298e50adSKalle Valo  */
3706298e50adSKalle Valo {
3707298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3708298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3709298e50adSKalle Valo 	.idVendor		= 0x13b1,
3710298e50adSKalle Valo 	.idProduct		= 0x000e,	/* Linksys WUSB54GSv1 */
3711298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3712298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320a_info,
3713298e50adSKalle Valo }, {
3714298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3715298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3716298e50adSKalle Valo 	.idVendor		= 0x0baf,
3717298e50adSKalle Valo 	.idProduct		= 0x0111,	/* U.S. Robotics USR5420 */
3718298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3719298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320a_info,
3720298e50adSKalle Valo }, {
3721298e50adSKalle Valo 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3722298e50adSKalle Valo 			  | USB_DEVICE_ID_MATCH_DEVICE,
3723298e50adSKalle Valo 	.idVendor		= 0x0411,
3724298e50adSKalle Valo 	.idProduct		= 0x004b,	/* BUFFALO WLI-USB-G54 */
3725298e50adSKalle Valo 	RNDIS_MASTER_INTERFACE,
3726298e50adSKalle Valo 	.driver_info		= (unsigned long) &bcm4320a_info,
3727298e50adSKalle Valo },
3728298e50adSKalle Valo /* Generic Wireless RNDIS devices that we don't have exact
3729298e50adSKalle Valo  * idVendor/idProduct/chip yet.
3730298e50adSKalle Valo  */
3731298e50adSKalle Valo {
3732298e50adSKalle Valo 	/* RNDIS is MSFT's un-official variant of CDC ACM */
3733298e50adSKalle Valo 	USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3734298e50adSKalle Valo 	.driver_info = (unsigned long) &rndis_wlan_info,
3735298e50adSKalle Valo }, {
3736298e50adSKalle Valo 	/* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3737298e50adSKalle Valo 	USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3738298e50adSKalle Valo 	.driver_info = (unsigned long) &rndis_wlan_info,
3739298e50adSKalle Valo },
3740298e50adSKalle Valo 	{ },		// END
3741298e50adSKalle Valo };
3742298e50adSKalle Valo MODULE_DEVICE_TABLE(usb, products);
3743298e50adSKalle Valo 
3744298e50adSKalle Valo static struct usb_driver rndis_wlan_driver = {
3745298e50adSKalle Valo 	.name =		"rndis_wlan",
3746298e50adSKalle Valo 	.id_table =	products,
3747298e50adSKalle Valo 	.probe =	usbnet_probe,
3748298e50adSKalle Valo 	.disconnect =	usbnet_disconnect,
3749298e50adSKalle Valo 	.suspend =	usbnet_suspend,
3750298e50adSKalle Valo 	.resume =	usbnet_resume,
3751298e50adSKalle Valo 	.disable_hub_initiated_lpm = 1,
3752298e50adSKalle Valo };
3753298e50adSKalle Valo 
3754298e50adSKalle Valo module_usb_driver(rndis_wlan_driver);
3755298e50adSKalle Valo 
3756298e50adSKalle Valo MODULE_AUTHOR("Bjorge Dijkstra");
3757298e50adSKalle Valo MODULE_AUTHOR("Jussi Kivilinna");
3758298e50adSKalle Valo MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3759298e50adSKalle Valo MODULE_LICENSE("GPL");
3760298e50adSKalle Valo 
3761