1 /*
2  * NXP Wireless LAN device driver: CFG80211
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include "cfg80211.h"
21 #include "main.h"
22 #include "11n.h"
23 #include "wmm.h"
24 
25 static char *reg_alpha2;
26 module_param(reg_alpha2, charp, 0);
27 
28 static const struct ieee80211_iface_limit mwifiex_ap_sta_limits[] = {
29 	{
30 		.max = MWIFIEX_MAX_BSS_NUM,
31 		.types = BIT(NL80211_IFTYPE_STATION) |
32 				   BIT(NL80211_IFTYPE_P2P_GO) |
33 				   BIT(NL80211_IFTYPE_P2P_CLIENT) |
34 				   BIT(NL80211_IFTYPE_AP),
35 	},
36 };
37 
38 static const struct ieee80211_iface_combination
39 mwifiex_iface_comb_ap_sta = {
40 	.limits = mwifiex_ap_sta_limits,
41 	.num_different_channels = 1,
42 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
43 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
44 	.beacon_int_infra_match = true,
45 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
46 				BIT(NL80211_CHAN_WIDTH_20) |
47 				BIT(NL80211_CHAN_WIDTH_40),
48 };
49 
50 static const struct ieee80211_iface_combination
51 mwifiex_iface_comb_ap_sta_vht = {
52 	.limits = mwifiex_ap_sta_limits,
53 	.num_different_channels = 1,
54 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
55 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
56 	.beacon_int_infra_match = true,
57 	.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
58 				BIT(NL80211_CHAN_WIDTH_20) |
59 				BIT(NL80211_CHAN_WIDTH_40) |
60 				BIT(NL80211_CHAN_WIDTH_80),
61 };
62 
63 static const struct
64 ieee80211_iface_combination mwifiex_iface_comb_ap_sta_drcs = {
65 	.limits = mwifiex_ap_sta_limits,
66 	.num_different_channels = 2,
67 	.n_limits = ARRAY_SIZE(mwifiex_ap_sta_limits),
68 	.max_interfaces = MWIFIEX_MAX_BSS_NUM,
69 	.beacon_int_infra_match = true,
70 };
71 
72 /*
73  * This function maps the nl802.11 channel type into driver channel type.
74  *
75  * The mapping is as follows -
76  *      NL80211_CHAN_NO_HT     -> IEEE80211_HT_PARAM_CHA_SEC_NONE
77  *      NL80211_CHAN_HT20      -> IEEE80211_HT_PARAM_CHA_SEC_NONE
78  *      NL80211_CHAN_HT40PLUS  -> IEEE80211_HT_PARAM_CHA_SEC_ABOVE
79  *      NL80211_CHAN_HT40MINUS -> IEEE80211_HT_PARAM_CHA_SEC_BELOW
80  *      Others                 -> IEEE80211_HT_PARAM_CHA_SEC_NONE
81  */
mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)82 u8 mwifiex_chan_type_to_sec_chan_offset(enum nl80211_channel_type chan_type)
83 {
84 	switch (chan_type) {
85 	case NL80211_CHAN_NO_HT:
86 	case NL80211_CHAN_HT20:
87 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
88 	case NL80211_CHAN_HT40PLUS:
89 		return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
90 	case NL80211_CHAN_HT40MINUS:
91 		return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
92 	default:
93 		return IEEE80211_HT_PARAM_CHA_SEC_NONE;
94 	}
95 }
96 
97 /* This function maps IEEE HT secondary channel type to NL80211 channel type
98  */
mwifiex_get_chan_type(struct mwifiex_private * priv)99 u8 mwifiex_get_chan_type(struct mwifiex_private *priv)
100 {
101 	struct mwifiex_channel_band channel_band;
102 	int ret;
103 
104 	ret = mwifiex_get_chan_info(priv, &channel_band);
105 
106 	if (!ret) {
107 		switch (channel_band.band_config.chan_width) {
108 		case CHAN_BW_20MHZ:
109 			if (IS_11N_ENABLED(priv))
110 				return NL80211_CHAN_HT20;
111 			else
112 				return NL80211_CHAN_NO_HT;
113 		case CHAN_BW_40MHZ:
114 			if (channel_band.band_config.chan2_offset ==
115 			    SEC_CHAN_ABOVE)
116 				return NL80211_CHAN_HT40PLUS;
117 			else
118 				return NL80211_CHAN_HT40MINUS;
119 		default:
120 			return NL80211_CHAN_HT20;
121 		}
122 	}
123 
124 	return NL80211_CHAN_HT20;
125 }
126 
127 /*
128  * This function checks whether WEP is set.
129  */
130 static int
mwifiex_is_alg_wep(u32 cipher)131 mwifiex_is_alg_wep(u32 cipher)
132 {
133 	switch (cipher) {
134 	case WLAN_CIPHER_SUITE_WEP40:
135 	case WLAN_CIPHER_SUITE_WEP104:
136 		return 1;
137 	default:
138 		break;
139 	}
140 
141 	return 0;
142 }
143 
144 /*
145  * This function retrieves the private structure from kernel wiphy structure.
146  */
mwifiex_cfg80211_get_adapter(struct wiphy * wiphy)147 static void *mwifiex_cfg80211_get_adapter(struct wiphy *wiphy)
148 {
149 	return (void *) (*(unsigned long *) wiphy_priv(wiphy));
150 }
151 
152 /*
153  * CFG802.11 operation handler to delete a network key.
154  */
155 static int
mwifiex_cfg80211_del_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool pairwise,const u8 * mac_addr)156 mwifiex_cfg80211_del_key(struct wiphy *wiphy, struct net_device *netdev,
157 			 u8 key_index, bool pairwise, const u8 *mac_addr)
158 {
159 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
160 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
161 	const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
162 
163 	if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index, peer_mac, 1)) {
164 		mwifiex_dbg(priv->adapter, ERROR, "deleting the crypto keys\n");
165 		return -EFAULT;
166 	}
167 
168 	mwifiex_dbg(priv->adapter, INFO, "info: crypto keys deleted\n");
169 	return 0;
170 }
171 
172 /*
173  * This function forms an skb for management frame.
174  */
175 static int
mwifiex_form_mgmt_frame(struct sk_buff * skb,const u8 * buf,size_t len)176 mwifiex_form_mgmt_frame(struct sk_buff *skb, const u8 *buf, size_t len)
177 {
178 	u8 addr[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
179 	u16 pkt_len;
180 	u32 tx_control = 0, pkt_type = PKT_TYPE_MGMT;
181 
182 	pkt_len = len + ETH_ALEN;
183 
184 	skb_reserve(skb, MWIFIEX_MIN_DATA_HEADER_LEN +
185 		    MWIFIEX_MGMT_FRAME_HEADER_SIZE + sizeof(pkt_len));
186 	memcpy(skb_push(skb, sizeof(pkt_len)), &pkt_len, sizeof(pkt_len));
187 
188 	memcpy(skb_push(skb, sizeof(tx_control)),
189 	       &tx_control, sizeof(tx_control));
190 
191 	memcpy(skb_push(skb, sizeof(pkt_type)), &pkt_type, sizeof(pkt_type));
192 
193 	/* Add packet data and address4 */
194 	skb_put_data(skb, buf, sizeof(struct ieee80211_hdr_3addr));
195 	skb_put_data(skb, addr, ETH_ALEN);
196 	skb_put_data(skb, buf + sizeof(struct ieee80211_hdr_3addr),
197 		     len - sizeof(struct ieee80211_hdr_3addr));
198 
199 	skb->priority = LOW_PRIO_TID;
200 	__net_timestamp(skb);
201 
202 	return 0;
203 }
204 
205 /*
206  * CFG802.11 operation handler to transmit a management frame.
207  */
208 static int
mwifiex_cfg80211_mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)209 mwifiex_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
210 			 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
211 {
212 	const u8 *buf = params->buf;
213 	size_t len = params->len;
214 	struct sk_buff *skb;
215 	u16 pkt_len;
216 	const struct ieee80211_mgmt *mgmt;
217 	struct mwifiex_txinfo *tx_info;
218 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
219 
220 	if (!buf || !len) {
221 		mwifiex_dbg(priv->adapter, ERROR, "invalid buffer and length\n");
222 		return -EFAULT;
223 	}
224 
225 	mgmt = (const struct ieee80211_mgmt *)buf;
226 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA &&
227 	    ieee80211_is_probe_resp(mgmt->frame_control)) {
228 		/* Since we support offload probe resp, we need to skip probe
229 		 * resp in AP or GO mode */
230 		mwifiex_dbg(priv->adapter, INFO,
231 			    "info: skip to send probe resp in AP or GO mode\n");
232 		return 0;
233 	}
234 
235 	pkt_len = len + ETH_ALEN;
236 	skb = dev_alloc_skb(MWIFIEX_MIN_DATA_HEADER_LEN +
237 			    MWIFIEX_MGMT_FRAME_HEADER_SIZE +
238 			    pkt_len + sizeof(pkt_len));
239 
240 	if (!skb) {
241 		mwifiex_dbg(priv->adapter, ERROR,
242 			    "allocate skb failed for management frame\n");
243 		return -ENOMEM;
244 	}
245 
246 	tx_info = MWIFIEX_SKB_TXCB(skb);
247 	memset(tx_info, 0, sizeof(*tx_info));
248 	tx_info->bss_num = priv->bss_num;
249 	tx_info->bss_type = priv->bss_type;
250 	tx_info->pkt_len = pkt_len;
251 
252 	mwifiex_form_mgmt_frame(skb, buf, len);
253 	*cookie = prandom_u32() | 1;
254 
255 	if (ieee80211_is_action(mgmt->frame_control))
256 		skb = mwifiex_clone_skb_for_tx_status(priv,
257 						      skb,
258 				MWIFIEX_BUF_FLAG_ACTION_TX_STATUS, cookie);
259 	else
260 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
261 					GFP_ATOMIC);
262 
263 	mwifiex_queue_tx_pkt(priv, skb);
264 
265 	mwifiex_dbg(priv->adapter, INFO, "info: management frame transmitted\n");
266 	return 0;
267 }
268 
269 /*
270  * CFG802.11 operation handler to register a mgmt frame.
271  */
272 static void
mwifiex_cfg80211_update_mgmt_frame_registrations(struct wiphy * wiphy,struct wireless_dev * wdev,struct mgmt_frame_regs * upd)273 mwifiex_cfg80211_update_mgmt_frame_registrations(struct wiphy *wiphy,
274 						 struct wireless_dev *wdev,
275 						 struct mgmt_frame_regs *upd)
276 {
277 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
278 	u32 mask = upd->interface_stypes;
279 
280 	if (mask != priv->mgmt_frame_mask) {
281 		priv->mgmt_frame_mask = mask;
282 		mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
283 				 HostCmd_ACT_GEN_SET, 0,
284 				 &priv->mgmt_frame_mask, false);
285 		mwifiex_dbg(priv->adapter, INFO, "info: mgmt frame registered\n");
286 	}
287 }
288 
289 /*
290  * CFG802.11 operation handler to remain on channel.
291  */
292 static int
mwifiex_cfg80211_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * chan,unsigned int duration,u64 * cookie)293 mwifiex_cfg80211_remain_on_channel(struct wiphy *wiphy,
294 				   struct wireless_dev *wdev,
295 				   struct ieee80211_channel *chan,
296 				   unsigned int duration, u64 *cookie)
297 {
298 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
299 	int ret;
300 
301 	if (!chan || !cookie) {
302 		mwifiex_dbg(priv->adapter, ERROR, "Invalid parameter for ROC\n");
303 		return -EINVAL;
304 	}
305 
306 	if (priv->roc_cfg.cookie) {
307 		mwifiex_dbg(priv->adapter, INFO,
308 			    "info: ongoing ROC, cookie = 0x%llx\n",
309 			    priv->roc_cfg.cookie);
310 		return -EBUSY;
311 	}
312 
313 	ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_SET, chan,
314 					 duration);
315 
316 	if (!ret) {
317 		*cookie = prandom_u32() | 1;
318 		priv->roc_cfg.cookie = *cookie;
319 		priv->roc_cfg.chan = *chan;
320 
321 		cfg80211_ready_on_channel(wdev, *cookie, chan,
322 					  duration, GFP_ATOMIC);
323 
324 		mwifiex_dbg(priv->adapter, INFO,
325 			    "info: ROC, cookie = 0x%llx\n", *cookie);
326 	}
327 
328 	return ret;
329 }
330 
331 /*
332  * CFG802.11 operation handler to cancel remain on channel.
333  */
334 static int
mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)335 mwifiex_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
336 					  struct wireless_dev *wdev, u64 cookie)
337 {
338 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
339 	int ret;
340 
341 	if (cookie != priv->roc_cfg.cookie)
342 		return -ENOENT;
343 
344 	ret = mwifiex_remain_on_chan_cfg(priv, HostCmd_ACT_GEN_REMOVE,
345 					 &priv->roc_cfg.chan, 0);
346 
347 	if (!ret) {
348 		cfg80211_remain_on_channel_expired(wdev, cookie,
349 						   &priv->roc_cfg.chan,
350 						   GFP_ATOMIC);
351 
352 		memset(&priv->roc_cfg, 0, sizeof(struct mwifiex_roc_cfg));
353 
354 		mwifiex_dbg(priv->adapter, INFO,
355 			    "info: cancel ROC, cookie = 0x%llx\n", cookie);
356 	}
357 
358 	return ret;
359 }
360 
361 /*
362  * CFG802.11 operation handler to set Tx power.
363  */
364 static int
mwifiex_cfg80211_set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)365 mwifiex_cfg80211_set_tx_power(struct wiphy *wiphy,
366 			      struct wireless_dev *wdev,
367 			      enum nl80211_tx_power_setting type,
368 			      int mbm)
369 {
370 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
371 	struct mwifiex_private *priv;
372 	struct mwifiex_power_cfg power_cfg;
373 	int dbm = MBM_TO_DBM(mbm);
374 
375 	switch (type) {
376 	case NL80211_TX_POWER_FIXED:
377 		power_cfg.is_power_auto = 0;
378 		power_cfg.is_power_fixed = 1;
379 		power_cfg.power_level = dbm;
380 		break;
381 	case NL80211_TX_POWER_LIMITED:
382 		power_cfg.is_power_auto = 0;
383 		power_cfg.is_power_fixed = 0;
384 		power_cfg.power_level = dbm;
385 		break;
386 	case NL80211_TX_POWER_AUTOMATIC:
387 		power_cfg.is_power_auto = 1;
388 		break;
389 	}
390 
391 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
392 
393 	return mwifiex_set_tx_power(priv, &power_cfg);
394 }
395 
396 /*
397  * CFG802.11 operation handler to get Tx power.
398  */
399 static int
mwifiex_cfg80211_get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int * dbm)400 mwifiex_cfg80211_get_tx_power(struct wiphy *wiphy,
401 			      struct wireless_dev *wdev,
402 			      int *dbm)
403 {
404 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
405 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
406 							MWIFIEX_BSS_ROLE_ANY);
407 	int ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
408 				   HostCmd_ACT_GEN_GET, 0, NULL, true);
409 
410 	if (ret < 0)
411 		return ret;
412 
413 	/* tx_power_level is set in HostCmd_CMD_RF_TX_PWR command handler */
414 	*dbm = priv->tx_power_level;
415 
416 	return 0;
417 }
418 
419 /*
420  * CFG802.11 operation handler to set Power Save option.
421  *
422  * The timeout value, if provided, is currently ignored.
423  */
424 static int
mwifiex_cfg80211_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)425 mwifiex_cfg80211_set_power_mgmt(struct wiphy *wiphy,
426 				struct net_device *dev,
427 				bool enabled, int timeout)
428 {
429 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
430 	u32 ps_mode;
431 
432 	if (timeout)
433 		mwifiex_dbg(priv->adapter, INFO,
434 			    "info: ignore timeout value for IEEE Power Save\n");
435 
436 	ps_mode = enabled;
437 
438 	return mwifiex_drv_set_power(priv, &ps_mode);
439 }
440 
441 /*
442  * CFG802.11 operation handler to set the default network key.
443  */
444 static int
mwifiex_cfg80211_set_default_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool unicast,bool multicast)445 mwifiex_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
446 				 u8 key_index, bool unicast,
447 				 bool multicast)
448 {
449 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
450 
451 	/* Return if WEP key not configured */
452 	if (!priv->sec_info.wep_enabled)
453 		return 0;
454 
455 	if (priv->bss_type == MWIFIEX_BSS_TYPE_UAP) {
456 		priv->wep_key_curr_index = key_index;
457 	} else if (mwifiex_set_encode(priv, NULL, NULL, 0, key_index,
458 				      NULL, 0)) {
459 		mwifiex_dbg(priv->adapter, ERROR, "set default Tx key index\n");
460 		return -EFAULT;
461 	}
462 
463 	return 0;
464 }
465 
466 /*
467  * CFG802.11 operation handler to add a network key.
468  */
469 static int
mwifiex_cfg80211_add_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)470 mwifiex_cfg80211_add_key(struct wiphy *wiphy, struct net_device *netdev,
471 			 u8 key_index, bool pairwise, const u8 *mac_addr,
472 			 struct key_params *params)
473 {
474 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
475 	struct mwifiex_wep_key *wep_key;
476 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
477 	const u8 *peer_mac = pairwise ? mac_addr : bc_mac;
478 
479 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
480 	    (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
481 	     params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
482 		if (params->key && params->key_len) {
483 			wep_key = &priv->wep_key[key_index];
484 			memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
485 			memcpy(wep_key->key_material, params->key,
486 			       params->key_len);
487 			wep_key->key_index = key_index;
488 			wep_key->key_length = params->key_len;
489 			priv->sec_info.wep_enabled = 1;
490 		}
491 		return 0;
492 	}
493 
494 	if (mwifiex_set_encode(priv, params, params->key, params->key_len,
495 			       key_index, peer_mac, 0)) {
496 		mwifiex_dbg(priv->adapter, ERROR, "crypto keys added\n");
497 		return -EFAULT;
498 	}
499 
500 	return 0;
501 }
502 
503 /*
504  * CFG802.11 operation handler to set default mgmt key.
505  */
506 static int
mwifiex_cfg80211_set_default_mgmt_key(struct wiphy * wiphy,struct net_device * netdev,u8 key_index)507 mwifiex_cfg80211_set_default_mgmt_key(struct wiphy *wiphy,
508 				      struct net_device *netdev,
509 				      u8 key_index)
510 {
511 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(netdev);
512 	struct mwifiex_ds_encrypt_key encrypt_key;
513 
514 	wiphy_dbg(wiphy, "set default mgmt key, key index=%d\n", key_index);
515 
516 	memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
517 	encrypt_key.key_len = WLAN_KEY_LEN_CCMP;
518 	encrypt_key.key_index = key_index;
519 	encrypt_key.is_igtk_def_key = true;
520 	eth_broadcast_addr(encrypt_key.mac_addr);
521 
522 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
523 			     HostCmd_ACT_GEN_SET, true, &encrypt_key, true)) {
524 		mwifiex_dbg(priv->adapter, ERROR,
525 			    "Sending KEY_MATERIAL command failed\n");
526 		return -1;
527 	}
528 
529 	return 0;
530 }
531 
532 /*
533  * This function sends domain information to the firmware.
534  *
535  * The following information are passed to the firmware -
536  *      - Country codes
537  *      - Sub bands (first channel, number of channels, maximum Tx power)
538  */
mwifiex_send_domain_info_cmd_fw(struct wiphy * wiphy)539 int mwifiex_send_domain_info_cmd_fw(struct wiphy *wiphy)
540 {
541 	u8 no_of_triplet = 0;
542 	struct ieee80211_country_ie_triplet *t;
543 	u8 no_of_parsed_chan = 0;
544 	u8 first_chan = 0, next_chan = 0, max_pwr = 0;
545 	u8 i, flag = 0;
546 	enum nl80211_band band;
547 	struct ieee80211_supported_band *sband;
548 	struct ieee80211_channel *ch;
549 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
550 	struct mwifiex_private *priv;
551 	struct mwifiex_802_11d_domain_reg *domain_info = &adapter->domain_reg;
552 
553 	/* Set country code */
554 	domain_info->country_code[0] = adapter->country_code[0];
555 	domain_info->country_code[1] = adapter->country_code[1];
556 	domain_info->country_code[2] = ' ';
557 
558 	band = mwifiex_band_to_radio_type(adapter->config_bands);
559 	if (!wiphy->bands[band]) {
560 		mwifiex_dbg(adapter, ERROR,
561 			    "11D: setting domain info in FW\n");
562 		return -1;
563 	}
564 
565 	sband = wiphy->bands[band];
566 
567 	for (i = 0; i < sband->n_channels ; i++) {
568 		ch = &sband->channels[i];
569 		if (ch->flags & IEEE80211_CHAN_DISABLED)
570 			continue;
571 
572 		if (!flag) {
573 			flag = 1;
574 			first_chan = (u32) ch->hw_value;
575 			next_chan = first_chan;
576 			max_pwr = ch->max_power;
577 			no_of_parsed_chan = 1;
578 			continue;
579 		}
580 
581 		if (ch->hw_value == next_chan + 1 &&
582 		    ch->max_power == max_pwr) {
583 			next_chan++;
584 			no_of_parsed_chan++;
585 		} else {
586 			t = &domain_info->triplet[no_of_triplet];
587 			t->chans.first_channel = first_chan;
588 			t->chans.num_channels = no_of_parsed_chan;
589 			t->chans.max_power = max_pwr;
590 			no_of_triplet++;
591 			first_chan = (u32) ch->hw_value;
592 			next_chan = first_chan;
593 			max_pwr = ch->max_power;
594 			no_of_parsed_chan = 1;
595 		}
596 	}
597 
598 	if (flag) {
599 		t = &domain_info->triplet[no_of_triplet];
600 		t->chans.first_channel = first_chan;
601 		t->chans.num_channels = no_of_parsed_chan;
602 		t->chans.max_power = max_pwr;
603 		no_of_triplet++;
604 	}
605 
606 	domain_info->no_of_triplet = no_of_triplet;
607 
608 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
609 
610 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
611 			     HostCmd_ACT_GEN_SET, 0, NULL, false)) {
612 		mwifiex_dbg(adapter, INFO,
613 			    "11D: setting domain info in FW\n");
614 		return -1;
615 	}
616 
617 	return 0;
618 }
619 
mwifiex_reg_apply_radar_flags(struct wiphy * wiphy)620 static void mwifiex_reg_apply_radar_flags(struct wiphy *wiphy)
621 {
622 	struct ieee80211_supported_band *sband;
623 	struct ieee80211_channel *chan;
624 	unsigned int i;
625 
626 	if (!wiphy->bands[NL80211_BAND_5GHZ])
627 		return;
628 	sband = wiphy->bands[NL80211_BAND_5GHZ];
629 
630 	for (i = 0; i < sband->n_channels; i++) {
631 		chan = &sband->channels[i];
632 		if ((!(chan->flags & IEEE80211_CHAN_DISABLED)) &&
633 		    (chan->flags & IEEE80211_CHAN_RADAR))
634 			chan->flags |= IEEE80211_CHAN_NO_IR;
635 	}
636 }
637 
638 /*
639  * CFG802.11 regulatory domain callback function.
640  *
641  * This function is called when the regulatory domain is changed due to the
642  * following reasons -
643  *      - Set by driver
644  *      - Set by system core
645  *      - Set by user
646  *      - Set bt Country IE
647  */
mwifiex_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)648 static void mwifiex_reg_notifier(struct wiphy *wiphy,
649 				 struct regulatory_request *request)
650 {
651 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
652 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
653 							MWIFIEX_BSS_ROLE_ANY);
654 	mwifiex_dbg(adapter, INFO,
655 		    "info: cfg80211 regulatory domain callback for %c%c\n",
656 		    request->alpha2[0], request->alpha2[1]);
657 	mwifiex_reg_apply_radar_flags(wiphy);
658 
659 	switch (request->initiator) {
660 	case NL80211_REGDOM_SET_BY_DRIVER:
661 	case NL80211_REGDOM_SET_BY_CORE:
662 	case NL80211_REGDOM_SET_BY_USER:
663 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
664 		break;
665 	default:
666 		mwifiex_dbg(adapter, ERROR,
667 			    "unknown regdom initiator: %d\n",
668 			    request->initiator);
669 		return;
670 	}
671 
672 	/* Don't send world or same regdom info to firmware */
673 	if (strncmp(request->alpha2, "00", 2) &&
674 	    strncmp(request->alpha2, adapter->country_code,
675 		    sizeof(request->alpha2))) {
676 		memcpy(adapter->country_code, request->alpha2,
677 		       sizeof(request->alpha2));
678 		mwifiex_send_domain_info_cmd_fw(wiphy);
679 		mwifiex_dnld_txpwr_table(priv);
680 	}
681 }
682 
683 /*
684  * This function sets the fragmentation threshold.
685  *
686  * The fragmentation threshold value must lie between MWIFIEX_FRAG_MIN_VALUE
687  * and MWIFIEX_FRAG_MAX_VALUE.
688  */
689 static int
mwifiex_set_frag(struct mwifiex_private * priv,u32 frag_thr)690 mwifiex_set_frag(struct mwifiex_private *priv, u32 frag_thr)
691 {
692 	if (frag_thr < MWIFIEX_FRAG_MIN_VALUE ||
693 	    frag_thr > MWIFIEX_FRAG_MAX_VALUE)
694 		frag_thr = MWIFIEX_FRAG_MAX_VALUE;
695 
696 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
697 				HostCmd_ACT_GEN_SET, FRAG_THRESH_I,
698 				&frag_thr, true);
699 }
700 
701 /*
702  * This function sets the RTS threshold.
703 
704  * The rts value must lie between MWIFIEX_RTS_MIN_VALUE
705  * and MWIFIEX_RTS_MAX_VALUE.
706  */
707 static int
mwifiex_set_rts(struct mwifiex_private * priv,u32 rts_thr)708 mwifiex_set_rts(struct mwifiex_private *priv, u32 rts_thr)
709 {
710 	if (rts_thr < MWIFIEX_RTS_MIN_VALUE || rts_thr > MWIFIEX_RTS_MAX_VALUE)
711 		rts_thr = MWIFIEX_RTS_MAX_VALUE;
712 
713 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
714 				HostCmd_ACT_GEN_SET, RTS_THRESH_I,
715 				&rts_thr, true);
716 }
717 
718 /*
719  * CFG802.11 operation handler to set wiphy parameters.
720  *
721  * This function can be used to set the RTS threshold and the
722  * Fragmentation threshold of the driver.
723  */
724 static int
mwifiex_cfg80211_set_wiphy_params(struct wiphy * wiphy,u32 changed)725 mwifiex_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
726 {
727 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
728 	struct mwifiex_private *priv;
729 	struct mwifiex_uap_bss_param *bss_cfg;
730 	int ret;
731 
732 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
733 
734 	switch (priv->bss_role) {
735 	case MWIFIEX_BSS_ROLE_UAP:
736 		if (priv->bss_started) {
737 			mwifiex_dbg(adapter, ERROR,
738 				    "cannot change wiphy params when bss started");
739 			return -EINVAL;
740 		}
741 
742 		bss_cfg = kzalloc(sizeof(*bss_cfg), GFP_KERNEL);
743 		if (!bss_cfg)
744 			return -ENOMEM;
745 
746 		mwifiex_set_sys_config_invalid_data(bss_cfg);
747 
748 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
749 			bss_cfg->rts_threshold = wiphy->rts_threshold;
750 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
751 			bss_cfg->frag_threshold = wiphy->frag_threshold;
752 		if (changed & WIPHY_PARAM_RETRY_LONG)
753 			bss_cfg->retry_limit = wiphy->retry_long;
754 
755 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
756 				       HostCmd_ACT_GEN_SET,
757 				       UAP_BSS_PARAMS_I, bss_cfg,
758 				       false);
759 
760 		kfree(bss_cfg);
761 		if (ret) {
762 			mwifiex_dbg(adapter, ERROR,
763 				    "Failed to set wiphy phy params\n");
764 			return ret;
765 		}
766 		break;
767 
768 	case MWIFIEX_BSS_ROLE_STA:
769 		if (priv->media_connected) {
770 			mwifiex_dbg(adapter, ERROR,
771 				    "cannot change wiphy params when connected");
772 			return -EINVAL;
773 		}
774 		if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
775 			ret = mwifiex_set_rts(priv,
776 					      wiphy->rts_threshold);
777 			if (ret)
778 				return ret;
779 		}
780 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
781 			ret = mwifiex_set_frag(priv,
782 					       wiphy->frag_threshold);
783 			if (ret)
784 				return ret;
785 		}
786 		break;
787 	}
788 
789 	return 0;
790 }
791 
792 static int
mwifiex_cfg80211_deinit_p2p(struct mwifiex_private * priv)793 mwifiex_cfg80211_deinit_p2p(struct mwifiex_private *priv)
794 {
795 	u16 mode = P2P_MODE_DISABLE;
796 
797 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
798 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
799 		return -1;
800 
801 	return 0;
802 }
803 
804 /*
805  * This function initializes the functionalities for P2P client.
806  * The P2P client initialization sequence is:
807  * disable -> device -> client
808  */
809 static int
mwifiex_cfg80211_init_p2p_client(struct mwifiex_private * priv)810 mwifiex_cfg80211_init_p2p_client(struct mwifiex_private *priv)
811 {
812 	u16 mode;
813 
814 	if (mwifiex_cfg80211_deinit_p2p(priv))
815 		return -1;
816 
817 	mode = P2P_MODE_DEVICE;
818 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
819 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
820 		return -1;
821 
822 	mode = P2P_MODE_CLIENT;
823 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
824 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
825 		return -1;
826 
827 	return 0;
828 }
829 
830 /*
831  * This function initializes the functionalities for P2P GO.
832  * The P2P GO initialization sequence is:
833  * disable -> device -> GO
834  */
835 static int
mwifiex_cfg80211_init_p2p_go(struct mwifiex_private * priv)836 mwifiex_cfg80211_init_p2p_go(struct mwifiex_private *priv)
837 {
838 	u16 mode;
839 
840 	if (mwifiex_cfg80211_deinit_p2p(priv))
841 		return -1;
842 
843 	mode = P2P_MODE_DEVICE;
844 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
845 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
846 		return -1;
847 
848 	mode = P2P_MODE_GO;
849 	if (mwifiex_send_cmd(priv, HostCmd_CMD_P2P_MODE_CFG,
850 			     HostCmd_ACT_GEN_SET, 0, &mode, true))
851 		return -1;
852 
853 	return 0;
854 }
855 
mwifiex_deinit_priv_params(struct mwifiex_private * priv)856 static int mwifiex_deinit_priv_params(struct mwifiex_private *priv)
857 {
858 	struct mwifiex_adapter *adapter = priv->adapter;
859 	unsigned long flags;
860 
861 	priv->mgmt_frame_mask = 0;
862 	if (mwifiex_send_cmd(priv, HostCmd_CMD_MGMT_FRAME_REG,
863 			     HostCmd_ACT_GEN_SET, 0,
864 			     &priv->mgmt_frame_mask, false)) {
865 		mwifiex_dbg(adapter, ERROR,
866 			    "could not unregister mgmt frame rx\n");
867 		return -1;
868 	}
869 
870 	mwifiex_deauthenticate(priv, NULL);
871 
872 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
873 	adapter->main_locked = true;
874 	if (adapter->mwifiex_processing) {
875 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
876 		flush_workqueue(adapter->workqueue);
877 	} else {
878 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
879 	}
880 
881 	spin_lock_bh(&adapter->rx_proc_lock);
882 	adapter->rx_locked = true;
883 	if (adapter->rx_processing) {
884 		spin_unlock_bh(&adapter->rx_proc_lock);
885 		flush_workqueue(adapter->rx_workqueue);
886 	} else {
887 	spin_unlock_bh(&adapter->rx_proc_lock);
888 	}
889 
890 	mwifiex_free_priv(priv);
891 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
892 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
893 	priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
894 
895 	return 0;
896 }
897 
898 static int
mwifiex_init_new_priv_params(struct mwifiex_private * priv,struct net_device * dev,enum nl80211_iftype type)899 mwifiex_init_new_priv_params(struct mwifiex_private *priv,
900 			     struct net_device *dev,
901 			     enum nl80211_iftype type)
902 {
903 	struct mwifiex_adapter *adapter = priv->adapter;
904 	unsigned long flags;
905 
906 	mwifiex_init_priv(priv);
907 
908 	priv->bss_mode = type;
909 	priv->wdev.iftype = type;
910 
911 	mwifiex_init_priv_params(priv, priv->netdev);
912 	priv->bss_started = 0;
913 
914 	switch (type) {
915 	case NL80211_IFTYPE_STATION:
916 	case NL80211_IFTYPE_ADHOC:
917 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
918 		priv->bss_type = MWIFIEX_BSS_TYPE_STA;
919 		break;
920 	case NL80211_IFTYPE_P2P_CLIENT:
921 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
922 		priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
923 		break;
924 	case NL80211_IFTYPE_P2P_GO:
925 		priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
926 		priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
927 		break;
928 	case NL80211_IFTYPE_AP:
929 		priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
930 		priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
931 		break;
932 	default:
933 		mwifiex_dbg(adapter, ERROR,
934 			    "%s: changing to %d not supported\n",
935 			    dev->name, type);
936 		return -EOPNOTSUPP;
937 	}
938 
939 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
940 	adapter->main_locked = false;
941 	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
942 
943 	spin_lock_bh(&adapter->rx_proc_lock);
944 	adapter->rx_locked = false;
945 	spin_unlock_bh(&adapter->rx_proc_lock);
946 
947 	mwifiex_set_mac_address(priv, dev, false, NULL);
948 
949 	return 0;
950 }
951 
952 static bool
is_vif_type_change_allowed(struct mwifiex_adapter * adapter,enum nl80211_iftype old_iftype,enum nl80211_iftype new_iftype)953 is_vif_type_change_allowed(struct mwifiex_adapter *adapter,
954 			   enum nl80211_iftype old_iftype,
955 			   enum nl80211_iftype new_iftype)
956 {
957 	switch (old_iftype) {
958 	case NL80211_IFTYPE_ADHOC:
959 		switch (new_iftype) {
960 		case NL80211_IFTYPE_STATION:
961 			return true;
962 		case NL80211_IFTYPE_P2P_CLIENT:
963 		case NL80211_IFTYPE_P2P_GO:
964 			return adapter->curr_iface_comb.p2p_intf !=
965 			       adapter->iface_limit.p2p_intf;
966 		case NL80211_IFTYPE_AP:
967 			return adapter->curr_iface_comb.uap_intf !=
968 			       adapter->iface_limit.uap_intf;
969 		default:
970 			return false;
971 		}
972 
973 	case NL80211_IFTYPE_STATION:
974 		switch (new_iftype) {
975 		case NL80211_IFTYPE_ADHOC:
976 			return true;
977 		case NL80211_IFTYPE_P2P_CLIENT:
978 		case NL80211_IFTYPE_P2P_GO:
979 			return adapter->curr_iface_comb.p2p_intf !=
980 			       adapter->iface_limit.p2p_intf;
981 		case NL80211_IFTYPE_AP:
982 			return adapter->curr_iface_comb.uap_intf !=
983 			       adapter->iface_limit.uap_intf;
984 		default:
985 			return false;
986 		}
987 
988 	case NL80211_IFTYPE_AP:
989 		switch (new_iftype) {
990 		case NL80211_IFTYPE_ADHOC:
991 		case NL80211_IFTYPE_STATION:
992 			return adapter->curr_iface_comb.sta_intf !=
993 			       adapter->iface_limit.sta_intf;
994 		case NL80211_IFTYPE_P2P_CLIENT:
995 		case NL80211_IFTYPE_P2P_GO:
996 			return adapter->curr_iface_comb.p2p_intf !=
997 			       adapter->iface_limit.p2p_intf;
998 		default:
999 			return false;
1000 		}
1001 
1002 	case NL80211_IFTYPE_P2P_CLIENT:
1003 		switch (new_iftype) {
1004 		case NL80211_IFTYPE_ADHOC:
1005 		case NL80211_IFTYPE_STATION:
1006 			return true;
1007 		case NL80211_IFTYPE_P2P_GO:
1008 			return true;
1009 		case NL80211_IFTYPE_AP:
1010 			return adapter->curr_iface_comb.uap_intf !=
1011 			       adapter->iface_limit.uap_intf;
1012 		default:
1013 			return false;
1014 		}
1015 
1016 	case NL80211_IFTYPE_P2P_GO:
1017 		switch (new_iftype) {
1018 		case NL80211_IFTYPE_ADHOC:
1019 		case NL80211_IFTYPE_STATION:
1020 			return true;
1021 		case NL80211_IFTYPE_P2P_CLIENT:
1022 			return true;
1023 		case NL80211_IFTYPE_AP:
1024 			return adapter->curr_iface_comb.uap_intf !=
1025 			       adapter->iface_limit.uap_intf;
1026 		default:
1027 			return false;
1028 		}
1029 
1030 	default:
1031 		break;
1032 	}
1033 
1034 	return false;
1035 }
1036 
1037 static void
update_vif_type_counter(struct mwifiex_adapter * adapter,enum nl80211_iftype iftype,int change)1038 update_vif_type_counter(struct mwifiex_adapter *adapter,
1039 			enum nl80211_iftype iftype,
1040 			int change)
1041 {
1042 	switch (iftype) {
1043 	case NL80211_IFTYPE_UNSPECIFIED:
1044 	case NL80211_IFTYPE_ADHOC:
1045 	case NL80211_IFTYPE_STATION:
1046 		adapter->curr_iface_comb.sta_intf += change;
1047 		break;
1048 	case NL80211_IFTYPE_AP:
1049 		adapter->curr_iface_comb.uap_intf += change;
1050 		break;
1051 	case NL80211_IFTYPE_P2P_CLIENT:
1052 	case NL80211_IFTYPE_P2P_GO:
1053 		adapter->curr_iface_comb.p2p_intf += change;
1054 		break;
1055 	default:
1056 		mwifiex_dbg(adapter, ERROR,
1057 			    "%s: Unsupported iftype passed: %d\n",
1058 			    __func__, iftype);
1059 		break;
1060 	}
1061 }
1062 
1063 static int
mwifiex_change_vif_to_p2p(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)1064 mwifiex_change_vif_to_p2p(struct net_device *dev,
1065 			  enum nl80211_iftype curr_iftype,
1066 			  enum nl80211_iftype type,
1067 			  struct vif_params *params)
1068 {
1069 	struct mwifiex_private *priv;
1070 	struct mwifiex_adapter *adapter;
1071 
1072 	priv = mwifiex_netdev_get_priv(dev);
1073 
1074 	if (!priv)
1075 		return -1;
1076 
1077 	adapter = priv->adapter;
1078 
1079 	mwifiex_dbg(adapter, INFO,
1080 		    "%s: changing role to p2p\n", dev->name);
1081 
1082 	if (mwifiex_deinit_priv_params(priv))
1083 		return -1;
1084 	if (mwifiex_init_new_priv_params(priv, dev, type))
1085 		return -1;
1086 
1087 	update_vif_type_counter(adapter, curr_iftype, -1);
1088 	update_vif_type_counter(adapter, type, +1);
1089 	dev->ieee80211_ptr->iftype = type;
1090 
1091 	switch (type) {
1092 	case NL80211_IFTYPE_P2P_CLIENT:
1093 		if (mwifiex_cfg80211_init_p2p_client(priv))
1094 			return -EFAULT;
1095 		break;
1096 	case NL80211_IFTYPE_P2P_GO:
1097 		if (mwifiex_cfg80211_init_p2p_go(priv))
1098 			return -EFAULT;
1099 		break;
1100 	default:
1101 		mwifiex_dbg(adapter, ERROR,
1102 			    "%s: changing to %d not supported\n",
1103 			    dev->name, type);
1104 		return -EOPNOTSUPP;
1105 	}
1106 
1107 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1108 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
1109 		return -1;
1110 
1111 	if (mwifiex_sta_init_cmd(priv, false, false))
1112 		return -1;
1113 
1114 	return 0;
1115 }
1116 
1117 static int
mwifiex_change_vif_to_sta_adhoc(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)1118 mwifiex_change_vif_to_sta_adhoc(struct net_device *dev,
1119 				enum nl80211_iftype curr_iftype,
1120 				enum nl80211_iftype type,
1121 				struct vif_params *params)
1122 {
1123 	struct mwifiex_private *priv;
1124 	struct mwifiex_adapter *adapter;
1125 
1126 	priv = mwifiex_netdev_get_priv(dev);
1127 
1128 	if (!priv)
1129 		return -1;
1130 
1131 	adapter = priv->adapter;
1132 
1133 	if (type == NL80211_IFTYPE_STATION)
1134 		mwifiex_dbg(adapter, INFO,
1135 			    "%s: changing role to station\n", dev->name);
1136 	else
1137 		mwifiex_dbg(adapter, INFO,
1138 			    "%s: changing role to adhoc\n", dev->name);
1139 
1140 	if (mwifiex_deinit_priv_params(priv))
1141 		return -1;
1142 	if (mwifiex_init_new_priv_params(priv, dev, type))
1143 		return -1;
1144 
1145 	update_vif_type_counter(adapter, curr_iftype, -1);
1146 	update_vif_type_counter(adapter, type, +1);
1147 	dev->ieee80211_ptr->iftype = type;
1148 
1149 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1150 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
1151 		return -1;
1152 	if (mwifiex_sta_init_cmd(priv, false, false))
1153 		return -1;
1154 
1155 	return 0;
1156 }
1157 
1158 static int
mwifiex_change_vif_to_ap(struct net_device * dev,enum nl80211_iftype curr_iftype,enum nl80211_iftype type,struct vif_params * params)1159 mwifiex_change_vif_to_ap(struct net_device *dev,
1160 			 enum nl80211_iftype curr_iftype,
1161 			 enum nl80211_iftype type,
1162 			 struct vif_params *params)
1163 {
1164 	struct mwifiex_private *priv;
1165 	struct mwifiex_adapter *adapter;
1166 
1167 	priv = mwifiex_netdev_get_priv(dev);
1168 
1169 	if (!priv)
1170 		return -1;
1171 
1172 	adapter = priv->adapter;
1173 
1174 	mwifiex_dbg(adapter, INFO,
1175 		    "%s: changing role to AP\n", dev->name);
1176 
1177 	if (mwifiex_deinit_priv_params(priv))
1178 		return -1;
1179 	if (mwifiex_init_new_priv_params(priv, dev, type))
1180 		return -1;
1181 
1182 	update_vif_type_counter(adapter, curr_iftype, -1);
1183 	update_vif_type_counter(adapter, type, +1);
1184 	dev->ieee80211_ptr->iftype = type;
1185 
1186 	if (mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1187 			     HostCmd_ACT_GEN_SET, 0, NULL, true))
1188 		return -1;
1189 	if (mwifiex_sta_init_cmd(priv, false, false))
1190 		return -1;
1191 
1192 	return 0;
1193 }
1194 /*
1195  * CFG802.11 operation handler to change interface type.
1196  */
1197 static int
mwifiex_cfg80211_change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1198 mwifiex_cfg80211_change_virtual_intf(struct wiphy *wiphy,
1199 				     struct net_device *dev,
1200 				     enum nl80211_iftype type,
1201 				     struct vif_params *params)
1202 {
1203 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1204 	enum nl80211_iftype curr_iftype = dev->ieee80211_ptr->iftype;
1205 
1206 	if (priv->scan_request) {
1207 		mwifiex_dbg(priv->adapter, ERROR,
1208 			    "change virtual interface: scan in process\n");
1209 		return -EBUSY;
1210 	}
1211 
1212 	if (type == NL80211_IFTYPE_UNSPECIFIED) {
1213 		mwifiex_dbg(priv->adapter, INFO,
1214 			    "%s: no new type specified, keeping old type %d\n",
1215 			    dev->name, curr_iftype);
1216 		return 0;
1217 	}
1218 
1219 	if (curr_iftype == type) {
1220 		mwifiex_dbg(priv->adapter, INFO,
1221 			    "%s: interface already is of type %d\n",
1222 			    dev->name, curr_iftype);
1223 		return 0;
1224 	}
1225 
1226 	if (!is_vif_type_change_allowed(priv->adapter, curr_iftype, type)) {
1227 		mwifiex_dbg(priv->adapter, ERROR,
1228 			    "%s: change from type %d to %d is not allowed\n",
1229 			    dev->name, curr_iftype, type);
1230 		return -EOPNOTSUPP;
1231 	}
1232 
1233 	switch (curr_iftype) {
1234 	case NL80211_IFTYPE_ADHOC:
1235 		switch (type) {
1236 		case NL80211_IFTYPE_STATION:
1237 			priv->bss_mode = type;
1238 			priv->sec_info.authentication_mode =
1239 						   NL80211_AUTHTYPE_OPEN_SYSTEM;
1240 			dev->ieee80211_ptr->iftype = type;
1241 			mwifiex_deauthenticate(priv, NULL);
1242 			return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1243 						HostCmd_ACT_GEN_SET, 0, NULL,
1244 						true);
1245 		case NL80211_IFTYPE_P2P_CLIENT:
1246 		case NL80211_IFTYPE_P2P_GO:
1247 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1248 							 type, params);
1249 		case NL80211_IFTYPE_AP:
1250 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1251 							params);
1252 		default:
1253 			goto errnotsupp;
1254 		}
1255 
1256 	case NL80211_IFTYPE_STATION:
1257 		switch (type) {
1258 		case NL80211_IFTYPE_ADHOC:
1259 			priv->bss_mode = type;
1260 			priv->sec_info.authentication_mode =
1261 						   NL80211_AUTHTYPE_OPEN_SYSTEM;
1262 			dev->ieee80211_ptr->iftype = type;
1263 			mwifiex_deauthenticate(priv, NULL);
1264 			return mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
1265 						HostCmd_ACT_GEN_SET, 0, NULL,
1266 						true);
1267 		case NL80211_IFTYPE_P2P_CLIENT:
1268 		case NL80211_IFTYPE_P2P_GO:
1269 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1270 							 type, params);
1271 		case NL80211_IFTYPE_AP:
1272 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1273 							params);
1274 		default:
1275 			goto errnotsupp;
1276 		}
1277 
1278 	case NL80211_IFTYPE_AP:
1279 		switch (type) {
1280 		case NL80211_IFTYPE_ADHOC:
1281 		case NL80211_IFTYPE_STATION:
1282 			return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1283 							       type, params);
1284 			break;
1285 		case NL80211_IFTYPE_P2P_CLIENT:
1286 		case NL80211_IFTYPE_P2P_GO:
1287 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1288 							 type, params);
1289 		default:
1290 			goto errnotsupp;
1291 		}
1292 
1293 	case NL80211_IFTYPE_P2P_CLIENT:
1294 		if (mwifiex_cfg80211_deinit_p2p(priv))
1295 			return -EFAULT;
1296 
1297 		switch (type) {
1298 		case NL80211_IFTYPE_ADHOC:
1299 		case NL80211_IFTYPE_STATION:
1300 			return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1301 							       type, params);
1302 		case NL80211_IFTYPE_P2P_GO:
1303 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1304 							 type, params);
1305 		case NL80211_IFTYPE_AP:
1306 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1307 							params);
1308 		default:
1309 			goto errnotsupp;
1310 		}
1311 
1312 	case NL80211_IFTYPE_P2P_GO:
1313 		if (mwifiex_cfg80211_deinit_p2p(priv))
1314 			return -EFAULT;
1315 
1316 		switch (type) {
1317 		case NL80211_IFTYPE_ADHOC:
1318 		case NL80211_IFTYPE_STATION:
1319 			return mwifiex_change_vif_to_sta_adhoc(dev, curr_iftype,
1320 							       type, params);
1321 		case NL80211_IFTYPE_P2P_CLIENT:
1322 			return mwifiex_change_vif_to_p2p(dev, curr_iftype,
1323 							 type, params);
1324 		case NL80211_IFTYPE_AP:
1325 			return mwifiex_change_vif_to_ap(dev, curr_iftype, type,
1326 							params);
1327 		default:
1328 			goto errnotsupp;
1329 		}
1330 
1331 	default:
1332 		goto errnotsupp;
1333 	}
1334 
1335 
1336 	return 0;
1337 
1338 errnotsupp:
1339 	mwifiex_dbg(priv->adapter, ERROR,
1340 		    "unsupported interface type transition: %d to %d\n",
1341 		    curr_iftype, type);
1342 	return -EOPNOTSUPP;
1343 }
1344 
1345 static void
mwifiex_parse_htinfo(struct mwifiex_private * priv,u8 rateinfo,u8 htinfo,struct rate_info * rate)1346 mwifiex_parse_htinfo(struct mwifiex_private *priv, u8 rateinfo, u8 htinfo,
1347 		     struct rate_info *rate)
1348 {
1349 	struct mwifiex_adapter *adapter = priv->adapter;
1350 
1351 	if (adapter->is_hw_11ac_capable) {
1352 		/* bit[1-0]: 00=LG 01=HT 10=VHT */
1353 		if (htinfo & BIT(0)) {
1354 			/* HT */
1355 			rate->mcs = rateinfo;
1356 			rate->flags |= RATE_INFO_FLAGS_MCS;
1357 		}
1358 		if (htinfo & BIT(1)) {
1359 			/* VHT */
1360 			rate->mcs = rateinfo & 0x0F;
1361 			rate->flags |= RATE_INFO_FLAGS_VHT_MCS;
1362 		}
1363 
1364 		if (htinfo & (BIT(1) | BIT(0))) {
1365 			/* HT or VHT */
1366 			switch (htinfo & (BIT(3) | BIT(2))) {
1367 			case 0:
1368 				rate->bw = RATE_INFO_BW_20;
1369 				break;
1370 			case (BIT(2)):
1371 				rate->bw = RATE_INFO_BW_40;
1372 				break;
1373 			case (BIT(3)):
1374 				rate->bw = RATE_INFO_BW_80;
1375 				break;
1376 			case (BIT(3) | BIT(2)):
1377 				rate->bw = RATE_INFO_BW_160;
1378 				break;
1379 			}
1380 
1381 			if (htinfo & BIT(4))
1382 				rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1383 
1384 			if ((rateinfo >> 4) == 1)
1385 				rate->nss = 2;
1386 			else
1387 				rate->nss = 1;
1388 		}
1389 	} else {
1390 		/*
1391 		 * Bit 0 in htinfo indicates that current rate is 11n. Valid
1392 		 * MCS index values for us are 0 to 15.
1393 		 */
1394 		if ((htinfo & BIT(0)) && (rateinfo < 16)) {
1395 			rate->mcs = rateinfo;
1396 			rate->flags |= RATE_INFO_FLAGS_MCS;
1397 			rate->bw = RATE_INFO_BW_20;
1398 			if (htinfo & BIT(1))
1399 				rate->bw = RATE_INFO_BW_40;
1400 			if (htinfo & BIT(2))
1401 				rate->flags |= RATE_INFO_FLAGS_SHORT_GI;
1402 		}
1403 	}
1404 
1405 	/* Decode legacy rates for non-HT. */
1406 	if (!(htinfo & (BIT(0) | BIT(1)))) {
1407 		/* Bitrates in multiples of 100kb/s. */
1408 		static const int legacy_rates[] = {
1409 			[0] = 10,
1410 			[1] = 20,
1411 			[2] = 55,
1412 			[3] = 110,
1413 			[4] = 60, /* MWIFIEX_RATE_INDEX_OFDM0 */
1414 			[5] = 60,
1415 			[6] = 90,
1416 			[7] = 120,
1417 			[8] = 180,
1418 			[9] = 240,
1419 			[10] = 360,
1420 			[11] = 480,
1421 			[12] = 540,
1422 		};
1423 		if (rateinfo < ARRAY_SIZE(legacy_rates))
1424 			rate->legacy = legacy_rates[rateinfo];
1425 	}
1426 }
1427 
1428 /*
1429  * This function dumps the station information on a buffer.
1430  *
1431  * The following information are shown -
1432  *      - Total bytes transmitted
1433  *      - Total bytes received
1434  *      - Total packets transmitted
1435  *      - Total packets received
1436  *      - Signal quality level
1437  *      - Transmission rate
1438  */
1439 static int
mwifiex_dump_station_info(struct mwifiex_private * priv,struct mwifiex_sta_node * node,struct station_info * sinfo)1440 mwifiex_dump_station_info(struct mwifiex_private *priv,
1441 			  struct mwifiex_sta_node *node,
1442 			  struct station_info *sinfo)
1443 {
1444 	u32 rate;
1445 
1446 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_RX_BYTES) | BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1447 			BIT_ULL(NL80211_STA_INFO_RX_PACKETS) | BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1448 			BIT_ULL(NL80211_STA_INFO_TX_BITRATE) |
1449 			BIT_ULL(NL80211_STA_INFO_SIGNAL) | BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
1450 
1451 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1452 		if (!node)
1453 			return -ENOENT;
1454 
1455 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
1456 				BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1457 		sinfo->inactive_time =
1458 			jiffies_to_msecs(jiffies - node->stats.last_rx);
1459 
1460 		sinfo->signal = node->stats.rssi;
1461 		sinfo->signal_avg = node->stats.rssi;
1462 		sinfo->rx_bytes = node->stats.rx_bytes;
1463 		sinfo->tx_bytes = node->stats.tx_bytes;
1464 		sinfo->rx_packets = node->stats.rx_packets;
1465 		sinfo->tx_packets = node->stats.tx_packets;
1466 		sinfo->tx_failed = node->stats.tx_failed;
1467 
1468 		mwifiex_parse_htinfo(priv, priv->tx_rate,
1469 				     node->stats.last_tx_htinfo,
1470 				     &sinfo->txrate);
1471 		sinfo->txrate.legacy = node->stats.last_tx_rate * 5;
1472 
1473 		return 0;
1474 	}
1475 
1476 	/* Get signal information from the firmware */
1477 	if (mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
1478 			     HostCmd_ACT_GEN_GET, 0, NULL, true)) {
1479 		mwifiex_dbg(priv->adapter, ERROR,
1480 			    "failed to get signal information\n");
1481 		return -EFAULT;
1482 	}
1483 
1484 	if (mwifiex_drv_get_data_rate(priv, &rate)) {
1485 		mwifiex_dbg(priv->adapter, ERROR,
1486 			    "getting data rate error\n");
1487 		return -EFAULT;
1488 	}
1489 
1490 	/* Get DTIM period information from firmware */
1491 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
1492 			 HostCmd_ACT_GEN_GET, DTIM_PERIOD_I,
1493 			 &priv->dtim_period, true);
1494 
1495 	mwifiex_parse_htinfo(priv, priv->tx_rate, priv->tx_htinfo,
1496 			     &sinfo->txrate);
1497 
1498 	sinfo->signal_avg = priv->bcn_rssi_avg;
1499 	sinfo->rx_bytes = priv->stats.rx_bytes;
1500 	sinfo->tx_bytes = priv->stats.tx_bytes;
1501 	sinfo->rx_packets = priv->stats.rx_packets;
1502 	sinfo->tx_packets = priv->stats.tx_packets;
1503 	sinfo->signal = priv->bcn_rssi_avg;
1504 	/* bit rate is in 500 kb/s units. Convert it to 100kb/s units */
1505 	sinfo->txrate.legacy = rate * 5;
1506 
1507 	sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
1508 	mwifiex_parse_htinfo(priv, priv->rxpd_rate, priv->rxpd_htinfo,
1509 			     &sinfo->rxrate);
1510 
1511 	if (priv->bss_mode == NL80211_IFTYPE_STATION) {
1512 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM);
1513 		sinfo->bss_param.flags = 0;
1514 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1515 						WLAN_CAPABILITY_SHORT_PREAMBLE)
1516 			sinfo->bss_param.flags |=
1517 					BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1518 		if (priv->curr_bss_params.bss_descriptor.cap_info_bitmap &
1519 						WLAN_CAPABILITY_SHORT_SLOT_TIME)
1520 			sinfo->bss_param.flags |=
1521 					BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1522 		sinfo->bss_param.dtim_period = priv->dtim_period;
1523 		sinfo->bss_param.beacon_interval =
1524 			priv->curr_bss_params.bss_descriptor.beacon_period;
1525 	}
1526 
1527 	return 0;
1528 }
1529 
1530 /*
1531  * CFG802.11 operation handler to get station information.
1532  *
1533  * This function only works in connected mode, and dumps the
1534  * requested station information, if available.
1535  */
1536 static int
mwifiex_cfg80211_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)1537 mwifiex_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
1538 			     const u8 *mac, struct station_info *sinfo)
1539 {
1540 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1541 
1542 	if (!priv->media_connected)
1543 		return -ENOENT;
1544 	if (memcmp(mac, priv->cfg_bssid, ETH_ALEN))
1545 		return -ENOENT;
1546 
1547 	return mwifiex_dump_station_info(priv, NULL, sinfo);
1548 }
1549 
1550 /*
1551  * CFG802.11 operation handler to dump station information.
1552  */
1553 static int
mwifiex_cfg80211_dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)1554 mwifiex_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
1555 			      int idx, u8 *mac, struct station_info *sinfo)
1556 {
1557 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1558 	struct mwifiex_sta_node *node;
1559 	int i;
1560 
1561 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1562 	    priv->media_connected && idx == 0) {
1563 		ether_addr_copy(mac, priv->cfg_bssid);
1564 		return mwifiex_dump_station_info(priv, NULL, sinfo);
1565 	} else if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1566 		mwifiex_send_cmd(priv, HOST_CMD_APCMD_STA_LIST,
1567 				 HostCmd_ACT_GEN_GET, 0, NULL, true);
1568 
1569 		i = 0;
1570 		list_for_each_entry(node, &priv->sta_list, list) {
1571 			if (i++ != idx)
1572 				continue;
1573 			ether_addr_copy(mac, node->mac_addr);
1574 			return mwifiex_dump_station_info(priv, node, sinfo);
1575 		}
1576 	}
1577 
1578 	return -ENOENT;
1579 }
1580 
1581 static int
mwifiex_cfg80211_dump_survey(struct wiphy * wiphy,struct net_device * dev,int idx,struct survey_info * survey)1582 mwifiex_cfg80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
1583 			     int idx, struct survey_info *survey)
1584 {
1585 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1586 	struct mwifiex_chan_stats *pchan_stats = priv->adapter->chan_stats;
1587 	enum nl80211_band band;
1588 
1589 	mwifiex_dbg(priv->adapter, DUMP, "dump_survey idx=%d\n", idx);
1590 
1591 	memset(survey, 0, sizeof(struct survey_info));
1592 
1593 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
1594 	    priv->media_connected && idx == 0) {
1595 			u8 curr_bss_band = priv->curr_bss_params.band;
1596 			u32 chan = priv->curr_bss_params.bss_descriptor.channel;
1597 
1598 			band = mwifiex_band_to_radio_type(curr_bss_band);
1599 			survey->channel = ieee80211_get_channel(wiphy,
1600 				ieee80211_channel_to_frequency(chan, band));
1601 
1602 			if (priv->bcn_nf_last) {
1603 				survey->filled = SURVEY_INFO_NOISE_DBM;
1604 				survey->noise = priv->bcn_nf_last;
1605 			}
1606 			return 0;
1607 	}
1608 
1609 	if (idx >= priv->adapter->num_in_chan_stats)
1610 		return -ENOENT;
1611 
1612 	if (!pchan_stats[idx].cca_scan_dur)
1613 		return 0;
1614 
1615 	band = pchan_stats[idx].bandcfg;
1616 	survey->channel = ieee80211_get_channel(wiphy,
1617 	    ieee80211_channel_to_frequency(pchan_stats[idx].chan_num, band));
1618 	survey->filled = SURVEY_INFO_NOISE_DBM |
1619 			 SURVEY_INFO_TIME |
1620 			 SURVEY_INFO_TIME_BUSY;
1621 	survey->noise = pchan_stats[idx].noise;
1622 	survey->time = pchan_stats[idx].cca_scan_dur;
1623 	survey->time_busy = pchan_stats[idx].cca_busy_dur;
1624 
1625 	return 0;
1626 }
1627 
1628 /* Supported rates to be advertised to the cfg80211 */
1629 static struct ieee80211_rate mwifiex_rates[] = {
1630 	{.bitrate = 10, .hw_value = 2, },
1631 	{.bitrate = 20, .hw_value = 4, },
1632 	{.bitrate = 55, .hw_value = 11, },
1633 	{.bitrate = 110, .hw_value = 22, },
1634 	{.bitrate = 60, .hw_value = 12, },
1635 	{.bitrate = 90, .hw_value = 18, },
1636 	{.bitrate = 120, .hw_value = 24, },
1637 	{.bitrate = 180, .hw_value = 36, },
1638 	{.bitrate = 240, .hw_value = 48, },
1639 	{.bitrate = 360, .hw_value = 72, },
1640 	{.bitrate = 480, .hw_value = 96, },
1641 	{.bitrate = 540, .hw_value = 108, },
1642 };
1643 
1644 /* Channel definitions to be advertised to cfg80211 */
1645 static struct ieee80211_channel mwifiex_channels_2ghz[] = {
1646 	{.center_freq = 2412, .hw_value = 1, },
1647 	{.center_freq = 2417, .hw_value = 2, },
1648 	{.center_freq = 2422, .hw_value = 3, },
1649 	{.center_freq = 2427, .hw_value = 4, },
1650 	{.center_freq = 2432, .hw_value = 5, },
1651 	{.center_freq = 2437, .hw_value = 6, },
1652 	{.center_freq = 2442, .hw_value = 7, },
1653 	{.center_freq = 2447, .hw_value = 8, },
1654 	{.center_freq = 2452, .hw_value = 9, },
1655 	{.center_freq = 2457, .hw_value = 10, },
1656 	{.center_freq = 2462, .hw_value = 11, },
1657 	{.center_freq = 2467, .hw_value = 12, },
1658 	{.center_freq = 2472, .hw_value = 13, },
1659 	{.center_freq = 2484, .hw_value = 14, },
1660 };
1661 
1662 static struct ieee80211_supported_band mwifiex_band_2ghz = {
1663 	.channels = mwifiex_channels_2ghz,
1664 	.n_channels = ARRAY_SIZE(mwifiex_channels_2ghz),
1665 	.bitrates = mwifiex_rates,
1666 	.n_bitrates = ARRAY_SIZE(mwifiex_rates),
1667 };
1668 
1669 static struct ieee80211_channel mwifiex_channels_5ghz[] = {
1670 	{.center_freq = 5040, .hw_value = 8, },
1671 	{.center_freq = 5060, .hw_value = 12, },
1672 	{.center_freq = 5080, .hw_value = 16, },
1673 	{.center_freq = 5170, .hw_value = 34, },
1674 	{.center_freq = 5190, .hw_value = 38, },
1675 	{.center_freq = 5210, .hw_value = 42, },
1676 	{.center_freq = 5230, .hw_value = 46, },
1677 	{.center_freq = 5180, .hw_value = 36, },
1678 	{.center_freq = 5200, .hw_value = 40, },
1679 	{.center_freq = 5220, .hw_value = 44, },
1680 	{.center_freq = 5240, .hw_value = 48, },
1681 	{.center_freq = 5260, .hw_value = 52, },
1682 	{.center_freq = 5280, .hw_value = 56, },
1683 	{.center_freq = 5300, .hw_value = 60, },
1684 	{.center_freq = 5320, .hw_value = 64, },
1685 	{.center_freq = 5500, .hw_value = 100, },
1686 	{.center_freq = 5520, .hw_value = 104, },
1687 	{.center_freq = 5540, .hw_value = 108, },
1688 	{.center_freq = 5560, .hw_value = 112, },
1689 	{.center_freq = 5580, .hw_value = 116, },
1690 	{.center_freq = 5600, .hw_value = 120, },
1691 	{.center_freq = 5620, .hw_value = 124, },
1692 	{.center_freq = 5640, .hw_value = 128, },
1693 	{.center_freq = 5660, .hw_value = 132, },
1694 	{.center_freq = 5680, .hw_value = 136, },
1695 	{.center_freq = 5700, .hw_value = 140, },
1696 	{.center_freq = 5745, .hw_value = 149, },
1697 	{.center_freq = 5765, .hw_value = 153, },
1698 	{.center_freq = 5785, .hw_value = 157, },
1699 	{.center_freq = 5805, .hw_value = 161, },
1700 	{.center_freq = 5825, .hw_value = 165, },
1701 };
1702 
1703 static struct ieee80211_supported_band mwifiex_band_5ghz = {
1704 	.channels = mwifiex_channels_5ghz,
1705 	.n_channels = ARRAY_SIZE(mwifiex_channels_5ghz),
1706 	.bitrates = mwifiex_rates + 4,
1707 	.n_bitrates = ARRAY_SIZE(mwifiex_rates) - 4,
1708 };
1709 
1710 
1711 /* Supported crypto cipher suits to be advertised to cfg80211 */
1712 static const u32 mwifiex_cipher_suites[] = {
1713 	WLAN_CIPHER_SUITE_WEP40,
1714 	WLAN_CIPHER_SUITE_WEP104,
1715 	WLAN_CIPHER_SUITE_TKIP,
1716 	WLAN_CIPHER_SUITE_CCMP,
1717 	WLAN_CIPHER_SUITE_SMS4,
1718 	WLAN_CIPHER_SUITE_AES_CMAC,
1719 };
1720 
1721 /* Supported mgmt frame types to be advertised to cfg80211 */
1722 static const struct ieee80211_txrx_stypes
1723 mwifiex_mgmt_stypes[NUM_NL80211_IFTYPES] = {
1724 	[NL80211_IFTYPE_STATION] = {
1725 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1726 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1727 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1728 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1729 	},
1730 	[NL80211_IFTYPE_AP] = {
1731 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1732 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1733 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1734 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1735 	},
1736 	[NL80211_IFTYPE_P2P_CLIENT] = {
1737 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1738 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1739 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1740 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1741 	},
1742 	[NL80211_IFTYPE_P2P_GO] = {
1743 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1744 		      BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
1745 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
1746 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
1747 	},
1748 };
1749 
1750 /*
1751  * CFG802.11 operation handler for setting bit rates.
1752  *
1753  * Function configures data rates to firmware using bitrate mask
1754  * provided by cfg80211.
1755  */
1756 static int
mwifiex_cfg80211_set_bitrate_mask(struct wiphy * wiphy,struct net_device * dev,unsigned int link_id,const u8 * peer,const struct cfg80211_bitrate_mask * mask)1757 mwifiex_cfg80211_set_bitrate_mask(struct wiphy *wiphy,
1758 				  struct net_device *dev,
1759 				  unsigned int link_id,
1760 				  const u8 *peer,
1761 				  const struct cfg80211_bitrate_mask *mask)
1762 {
1763 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1764 	u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
1765 	enum nl80211_band band;
1766 	struct mwifiex_adapter *adapter = priv->adapter;
1767 
1768 	if (!priv->media_connected) {
1769 		mwifiex_dbg(adapter, ERROR,
1770 			    "Can not set Tx data rate in disconnected state\n");
1771 		return -EINVAL;
1772 	}
1773 
1774 	band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
1775 
1776 	memset(bitmap_rates, 0, sizeof(bitmap_rates));
1777 
1778 	/* Fill HR/DSSS rates. */
1779 	if (band == NL80211_BAND_2GHZ)
1780 		bitmap_rates[0] = mask->control[band].legacy & 0x000f;
1781 
1782 	/* Fill OFDM rates */
1783 	if (band == NL80211_BAND_2GHZ)
1784 		bitmap_rates[1] = (mask->control[band].legacy & 0x0ff0) >> 4;
1785 	else
1786 		bitmap_rates[1] = mask->control[band].legacy;
1787 
1788 	/* Fill HT MCS rates */
1789 	bitmap_rates[2] = mask->control[band].ht_mcs[0];
1790 	if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1791 		bitmap_rates[2] |= mask->control[band].ht_mcs[1] << 8;
1792 
1793        /* Fill VHT MCS rates */
1794 	if (adapter->fw_api_ver == MWIFIEX_FW_V15) {
1795 		bitmap_rates[10] = mask->control[band].vht_mcs[0];
1796 		if (adapter->hw_dev_mcs_support == HT_STREAM_2X2)
1797 			bitmap_rates[11] = mask->control[band].vht_mcs[1];
1798 	}
1799 
1800 	return mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
1801 				HostCmd_ACT_GEN_SET, 0, bitmap_rates, true);
1802 }
1803 
1804 /*
1805  * CFG802.11 operation handler for connection quality monitoring.
1806  *
1807  * This function subscribes/unsubscribes HIGH_RSSI and LOW_RSSI
1808  * events to FW.
1809  */
mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)1810 static int mwifiex_cfg80211_set_cqm_rssi_config(struct wiphy *wiphy,
1811 						struct net_device *dev,
1812 						s32 rssi_thold, u32 rssi_hyst)
1813 {
1814 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1815 	struct mwifiex_ds_misc_subsc_evt subsc_evt;
1816 
1817 	priv->cqm_rssi_thold = rssi_thold;
1818 	priv->cqm_rssi_hyst = rssi_hyst;
1819 
1820 	memset(&subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
1821 	subsc_evt.events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
1822 
1823 	/* Subscribe/unsubscribe low and high rssi events */
1824 	if (rssi_thold && rssi_hyst) {
1825 		subsc_evt.action = HostCmd_ACT_BITWISE_SET;
1826 		subsc_evt.bcn_l_rssi_cfg.abs_value = abs(rssi_thold);
1827 		subsc_evt.bcn_h_rssi_cfg.abs_value = abs(rssi_thold);
1828 		subsc_evt.bcn_l_rssi_cfg.evt_freq = 1;
1829 		subsc_evt.bcn_h_rssi_cfg.evt_freq = 1;
1830 		return mwifiex_send_cmd(priv,
1831 					HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1832 					0, 0, &subsc_evt, true);
1833 	} else {
1834 		subsc_evt.action = HostCmd_ACT_BITWISE_CLR;
1835 		return mwifiex_send_cmd(priv,
1836 					HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
1837 					0, 0, &subsc_evt, true);
1838 	}
1839 
1840 	return 0;
1841 }
1842 
1843 /* cfg80211 operation handler for change_beacon.
1844  * Function retrieves and sets modified management IEs to FW.
1845  */
mwifiex_cfg80211_change_beacon(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_beacon_data * data)1846 static int mwifiex_cfg80211_change_beacon(struct wiphy *wiphy,
1847 					  struct net_device *dev,
1848 					  struct cfg80211_beacon_data *data)
1849 {
1850 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1851 	struct mwifiex_adapter *adapter = priv->adapter;
1852 
1853 	mwifiex_cancel_scan(adapter);
1854 
1855 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP) {
1856 		mwifiex_dbg(priv->adapter, ERROR,
1857 			    "%s: bss_type mismatched\n", __func__);
1858 		return -EINVAL;
1859 	}
1860 
1861 	if (!priv->bss_started) {
1862 		mwifiex_dbg(priv->adapter, ERROR,
1863 			    "%s: bss not started\n", __func__);
1864 		return -EINVAL;
1865 	}
1866 
1867 	if (mwifiex_set_mgmt_ies(priv, data)) {
1868 		mwifiex_dbg(priv->adapter, ERROR,
1869 			    "%s: setting mgmt ies failed\n", __func__);
1870 		return -EFAULT;
1871 	}
1872 
1873 	return 0;
1874 }
1875 
1876 /* cfg80211 operation handler for del_station.
1877  * Function deauthenticates station which value is provided in mac parameter.
1878  * If mac is NULL/broadcast, all stations in associated station list are
1879  * deauthenticated. If bss is not started or there are no stations in
1880  * associated stations list, no action is taken.
1881  */
1882 static int
mwifiex_cfg80211_del_station(struct wiphy * wiphy,struct net_device * dev,struct station_del_parameters * params)1883 mwifiex_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
1884 			     struct station_del_parameters *params)
1885 {
1886 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1887 	struct mwifiex_sta_node *sta_node;
1888 	u8 deauth_mac[ETH_ALEN];
1889 
1890 	if (!priv->bss_started && priv->wdev.cac_started) {
1891 		mwifiex_dbg(priv->adapter, INFO, "%s: abort CAC!\n", __func__);
1892 		mwifiex_abort_cac(priv);
1893 	}
1894 
1895 	if (list_empty(&priv->sta_list) || !priv->bss_started)
1896 		return 0;
1897 
1898 	if (!params->mac || is_broadcast_ether_addr(params->mac))
1899 		return 0;
1900 
1901 	mwifiex_dbg(priv->adapter, INFO, "%s: mac address %pM\n",
1902 		    __func__, params->mac);
1903 
1904 	eth_zero_addr(deauth_mac);
1905 
1906 	spin_lock_bh(&priv->sta_list_spinlock);
1907 	sta_node = mwifiex_get_sta_entry(priv, params->mac);
1908 	if (sta_node)
1909 		ether_addr_copy(deauth_mac, params->mac);
1910 	spin_unlock_bh(&priv->sta_list_spinlock);
1911 
1912 	if (is_valid_ether_addr(deauth_mac)) {
1913 		if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_STA_DEAUTH,
1914 				     HostCmd_ACT_GEN_SET, 0,
1915 				     deauth_mac, true))
1916 			return -1;
1917 	}
1918 
1919 	return 0;
1920 }
1921 
1922 static int
mwifiex_cfg80211_set_antenna(struct wiphy * wiphy,u32 tx_ant,u32 rx_ant)1923 mwifiex_cfg80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1924 {
1925 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1926 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1927 							MWIFIEX_BSS_ROLE_ANY);
1928 	struct mwifiex_ds_ant_cfg ant_cfg;
1929 
1930 	if (!tx_ant || !rx_ant)
1931 		return -EOPNOTSUPP;
1932 
1933 	if (adapter->hw_dev_mcs_support != HT_STREAM_2X2) {
1934 		/* Not a MIMO chip. User should provide specific antenna number
1935 		 * for Tx/Rx path or enable all antennas for diversity
1936 		 */
1937 		if (tx_ant != rx_ant)
1938 			return -EOPNOTSUPP;
1939 
1940 		if ((tx_ant & (tx_ant - 1)) &&
1941 		    (tx_ant != BIT(adapter->number_of_antenna) - 1))
1942 			return -EOPNOTSUPP;
1943 
1944 		if ((tx_ant == BIT(adapter->number_of_antenna) - 1) &&
1945 		    (priv->adapter->number_of_antenna > 1)) {
1946 			tx_ant = RF_ANTENNA_AUTO;
1947 			rx_ant = RF_ANTENNA_AUTO;
1948 		}
1949 	} else {
1950 		struct ieee80211_sta_ht_cap *ht_info;
1951 		int rx_mcs_supp;
1952 		enum nl80211_band band;
1953 
1954 		if ((tx_ant == 0x1 && rx_ant == 0x1)) {
1955 			adapter->user_dev_mcs_support = HT_STREAM_1X1;
1956 			if (adapter->is_hw_11ac_capable)
1957 				adapter->usr_dot_11ac_mcs_support =
1958 						MWIFIEX_11AC_MCS_MAP_1X1;
1959 		} else {
1960 			adapter->user_dev_mcs_support = HT_STREAM_2X2;
1961 			if (adapter->is_hw_11ac_capable)
1962 				adapter->usr_dot_11ac_mcs_support =
1963 						MWIFIEX_11AC_MCS_MAP_2X2;
1964 		}
1965 
1966 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
1967 			if (!adapter->wiphy->bands[band])
1968 				continue;
1969 
1970 			ht_info = &adapter->wiphy->bands[band]->ht_cap;
1971 			rx_mcs_supp =
1972 				GET_RXMCSSUPP(adapter->user_dev_mcs_support);
1973 			memset(&ht_info->mcs, 0, adapter->number_of_antenna);
1974 			memset(&ht_info->mcs, 0xff, rx_mcs_supp);
1975 		}
1976 	}
1977 
1978 	ant_cfg.tx_ant = tx_ant;
1979 	ant_cfg.rx_ant = rx_ant;
1980 
1981 	return mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1982 				HostCmd_ACT_GEN_SET, 0, &ant_cfg, true);
1983 }
1984 
1985 static int
mwifiex_cfg80211_get_antenna(struct wiphy * wiphy,u32 * tx_ant,u32 * rx_ant)1986 mwifiex_cfg80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1987 {
1988 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
1989 	struct mwifiex_private *priv = mwifiex_get_priv(adapter,
1990 							MWIFIEX_BSS_ROLE_ANY);
1991 	mwifiex_send_cmd(priv, HostCmd_CMD_RF_ANTENNA,
1992 			 HostCmd_ACT_GEN_GET, 0, NULL, true);
1993 
1994 	*tx_ant = priv->tx_ant;
1995 	*rx_ant = priv->rx_ant;
1996 
1997 	return 0;
1998 }
1999 
2000 /* cfg80211 operation handler for stop ap.
2001  * Function stops BSS running at uAP interface.
2002  */
mwifiex_cfg80211_stop_ap(struct wiphy * wiphy,struct net_device * dev,unsigned int link_id)2003 static int mwifiex_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *dev,
2004 				    unsigned int link_id)
2005 {
2006 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2007 
2008 	mwifiex_abort_cac(priv);
2009 
2010 	if (mwifiex_del_mgmt_ies(priv))
2011 		mwifiex_dbg(priv->adapter, ERROR,
2012 			    "Failed to delete mgmt IEs!\n");
2013 
2014 	priv->ap_11n_enabled = 0;
2015 	memset(&priv->bss_cfg, 0, sizeof(priv->bss_cfg));
2016 
2017 	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
2018 			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
2019 		mwifiex_dbg(priv->adapter, ERROR,
2020 			    "Failed to stop the BSS\n");
2021 		return -1;
2022 	}
2023 
2024 	if (mwifiex_send_cmd(priv, HOST_CMD_APCMD_SYS_RESET,
2025 			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
2026 		mwifiex_dbg(priv->adapter, ERROR,
2027 			    "Failed to reset BSS\n");
2028 		return -1;
2029 	}
2030 
2031 	if (netif_carrier_ok(priv->netdev))
2032 		netif_carrier_off(priv->netdev);
2033 	mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
2034 
2035 	return 0;
2036 }
2037 
2038 /* cfg80211 operation handler for start_ap.
2039  * Function sets beacon period, DTIM period, SSID and security into
2040  * AP config structure.
2041  * AP is configured with these settings and BSS is started.
2042  */
mwifiex_cfg80211_start_ap(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ap_settings * params)2043 static int mwifiex_cfg80211_start_ap(struct wiphy *wiphy,
2044 				     struct net_device *dev,
2045 				     struct cfg80211_ap_settings *params)
2046 {
2047 	struct mwifiex_uap_bss_param *bss_cfg;
2048 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2049 
2050 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_UAP)
2051 		return -1;
2052 
2053 	bss_cfg = kzalloc(sizeof(struct mwifiex_uap_bss_param), GFP_KERNEL);
2054 	if (!bss_cfg)
2055 		return -ENOMEM;
2056 
2057 	mwifiex_set_sys_config_invalid_data(bss_cfg);
2058 
2059 	if (params->beacon_interval)
2060 		bss_cfg->beacon_period = params->beacon_interval;
2061 	if (params->dtim_period)
2062 		bss_cfg->dtim_period = params->dtim_period;
2063 
2064 	if (params->ssid && params->ssid_len) {
2065 		memcpy(bss_cfg->ssid.ssid, params->ssid, params->ssid_len);
2066 		bss_cfg->ssid.ssid_len = params->ssid_len;
2067 	}
2068 	if (params->inactivity_timeout > 0) {
2069 		/* sta_ao_timer/ps_sta_ao_timer is in unit of 100ms */
2070 		bss_cfg->sta_ao_timer = 10 * params->inactivity_timeout;
2071 		bss_cfg->ps_sta_ao_timer = 10 * params->inactivity_timeout;
2072 	}
2073 
2074 	switch (params->hidden_ssid) {
2075 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
2076 		bss_cfg->bcast_ssid_ctl = 1;
2077 		break;
2078 	case NL80211_HIDDEN_SSID_ZERO_LEN:
2079 		bss_cfg->bcast_ssid_ctl = 0;
2080 		break;
2081 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
2082 		bss_cfg->bcast_ssid_ctl = 2;
2083 		break;
2084 	default:
2085 		kfree(bss_cfg);
2086 		return -EINVAL;
2087 	}
2088 
2089 	mwifiex_uap_set_channel(priv, bss_cfg, params->chandef);
2090 	mwifiex_set_uap_rates(bss_cfg, params);
2091 
2092 	if (mwifiex_set_secure_params(priv, bss_cfg, params)) {
2093 		mwifiex_dbg(priv->adapter, ERROR,
2094 			    "Failed to parse security parameters!\n");
2095 		goto out;
2096 	}
2097 
2098 	mwifiex_set_ht_params(priv, bss_cfg, params);
2099 
2100 	if (priv->adapter->is_hw_11ac_capable) {
2101 		mwifiex_set_vht_params(priv, bss_cfg, params);
2102 		mwifiex_set_vht_width(priv, params->chandef.width,
2103 				      priv->ap_11ac_enabled);
2104 	}
2105 
2106 	if (priv->ap_11ac_enabled)
2107 		mwifiex_set_11ac_ba_params(priv);
2108 	else
2109 		mwifiex_set_ba_params(priv);
2110 
2111 	mwifiex_set_wmm_params(priv, bss_cfg, params);
2112 
2113 	if (mwifiex_is_11h_active(priv))
2114 		mwifiex_set_tpc_params(priv, bss_cfg, params);
2115 
2116 	if (mwifiex_is_11h_active(priv) &&
2117 	    !cfg80211_chandef_dfs_required(wiphy, &params->chandef,
2118 					   priv->bss_mode)) {
2119 		mwifiex_dbg(priv->adapter, INFO,
2120 			    "Disable 11h extensions in FW\n");
2121 		if (mwifiex_11h_activate(priv, false)) {
2122 			mwifiex_dbg(priv->adapter, ERROR,
2123 				    "Failed to disable 11h extensions!!");
2124 			goto out;
2125 		}
2126 		priv->state_11h.is_11h_active = false;
2127 	}
2128 
2129 	mwifiex_config_uap_11d(priv, &params->beacon);
2130 
2131 	if (mwifiex_config_start_uap(priv, bss_cfg)) {
2132 		mwifiex_dbg(priv->adapter, ERROR,
2133 			    "Failed to start AP\n");
2134 		goto out;
2135 	}
2136 
2137 	if (mwifiex_set_mgmt_ies(priv, &params->beacon))
2138 		goto out;
2139 
2140 	if (!netif_carrier_ok(priv->netdev))
2141 		netif_carrier_on(priv->netdev);
2142 	mwifiex_wake_up_net_dev_queue(priv->netdev, priv->adapter);
2143 
2144 	memcpy(&priv->bss_cfg, bss_cfg, sizeof(priv->bss_cfg));
2145 	kfree(bss_cfg);
2146 	return 0;
2147 
2148 out:
2149 	kfree(bss_cfg);
2150 	return -1;
2151 }
2152 
2153 /*
2154  * CFG802.11 operation handler for disconnection request.
2155  *
2156  * This function does not work when there is already a disconnection
2157  * procedure going on.
2158  */
2159 static int
mwifiex_cfg80211_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)2160 mwifiex_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
2161 			    u16 reason_code)
2162 {
2163 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2164 
2165 	if (!mwifiex_stop_bg_scan(priv))
2166 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2167 
2168 	if (mwifiex_deauthenticate(priv, NULL))
2169 		return -EFAULT;
2170 
2171 	eth_zero_addr(priv->cfg_bssid);
2172 	priv->hs2_enabled = false;
2173 
2174 	return 0;
2175 }
2176 
2177 /*
2178  * This function informs the CFG802.11 subsystem of a new IBSS.
2179  *
2180  * The following information are sent to the CFG802.11 subsystem
2181  * to register the new IBSS. If we do not register the new IBSS,
2182  * a kernel panic will result.
2183  *      - SSID
2184  *      - SSID length
2185  *      - BSSID
2186  *      - Channel
2187  */
mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private * priv)2188 static int mwifiex_cfg80211_inform_ibss_bss(struct mwifiex_private *priv)
2189 {
2190 	struct ieee80211_channel *chan;
2191 	struct mwifiex_bss_info bss_info;
2192 	struct cfg80211_bss *bss;
2193 	int ie_len;
2194 	u8 ie_buf[IEEE80211_MAX_SSID_LEN + sizeof(struct ieee_types_header)];
2195 	enum nl80211_band band;
2196 
2197 	if (mwifiex_get_bss_info(priv, &bss_info))
2198 		return -1;
2199 
2200 	ie_buf[0] = WLAN_EID_SSID;
2201 	ie_buf[1] = bss_info.ssid.ssid_len;
2202 
2203 	memcpy(&ie_buf[sizeof(struct ieee_types_header)],
2204 	       &bss_info.ssid.ssid, bss_info.ssid.ssid_len);
2205 	ie_len = ie_buf[1] + sizeof(struct ieee_types_header);
2206 
2207 	band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
2208 	chan = ieee80211_get_channel(priv->wdev.wiphy,
2209 			ieee80211_channel_to_frequency(bss_info.bss_chan,
2210 						       band));
2211 
2212 	bss = cfg80211_inform_bss(priv->wdev.wiphy, chan,
2213 				  CFG80211_BSS_FTYPE_UNKNOWN,
2214 				  bss_info.bssid, 0, WLAN_CAPABILITY_IBSS,
2215 				  0, ie_buf, ie_len, 0, GFP_KERNEL);
2216 	if (bss) {
2217 		cfg80211_put_bss(priv->wdev.wiphy, bss);
2218 		ether_addr_copy(priv->cfg_bssid, bss_info.bssid);
2219 	}
2220 
2221 	return 0;
2222 }
2223 
2224 /*
2225  * This function connects with a BSS.
2226  *
2227  * This function handles both Infra and Ad-Hoc modes. It also performs
2228  * validity checking on the provided parameters, disconnects from the
2229  * current BSS (if any), sets up the association/scan parameters,
2230  * including security settings, and performs specific SSID scan before
2231  * trying to connect.
2232  *
2233  * For Infra mode, the function returns failure if the specified SSID
2234  * is not found in scan table. However, for Ad-Hoc mode, it can create
2235  * the IBSS if it does not exist. On successful completion in either case,
2236  * the function notifies the CFG802.11 subsystem of the new BSS connection.
2237  */
2238 static int
mwifiex_cfg80211_assoc(struct mwifiex_private * priv,size_t ssid_len,const u8 * ssid,const u8 * bssid,int mode,struct ieee80211_channel * channel,struct cfg80211_connect_params * sme,bool privacy,struct cfg80211_bss ** sel_bss)2239 mwifiex_cfg80211_assoc(struct mwifiex_private *priv, size_t ssid_len,
2240 		       const u8 *ssid, const u8 *bssid, int mode,
2241 		       struct ieee80211_channel *channel,
2242 		       struct cfg80211_connect_params *sme, bool privacy,
2243 		       struct cfg80211_bss **sel_bss)
2244 {
2245 	struct cfg80211_ssid req_ssid;
2246 	int ret, auth_type = 0;
2247 	struct cfg80211_bss *bss = NULL;
2248 	u8 is_scanning_required = 0;
2249 
2250 	memset(&req_ssid, 0, sizeof(struct cfg80211_ssid));
2251 
2252 	req_ssid.ssid_len = ssid_len;
2253 	if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2254 		mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2255 		return -EINVAL;
2256 	}
2257 
2258 	memcpy(req_ssid.ssid, ssid, ssid_len);
2259 	if (!req_ssid.ssid_len || req_ssid.ssid[0] < 0x20) {
2260 		mwifiex_dbg(priv->adapter, ERROR, "invalid SSID - aborting\n");
2261 		return -EINVAL;
2262 	}
2263 
2264 	/* As this is new association, clear locally stored
2265 	 * keys and security related flags */
2266 	priv->sec_info.wpa_enabled = false;
2267 	priv->sec_info.wpa2_enabled = false;
2268 	priv->wep_key_curr_index = 0;
2269 	priv->sec_info.encryption_mode = 0;
2270 	priv->sec_info.is_authtype_auto = 0;
2271 	ret = mwifiex_set_encode(priv, NULL, NULL, 0, 0, NULL, 1);
2272 
2273 	if (mode == NL80211_IFTYPE_ADHOC) {
2274 		u16 enable = true;
2275 
2276 		/* set ibss coalescing_status */
2277 		ret = mwifiex_send_cmd(
2278 				priv,
2279 				HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
2280 				HostCmd_ACT_GEN_SET, 0, &enable, true);
2281 		if (ret)
2282 			return ret;
2283 
2284 		/* "privacy" is set only for ad-hoc mode */
2285 		if (privacy) {
2286 			/*
2287 			 * Keep WLAN_CIPHER_SUITE_WEP104 for now so that
2288 			 * the firmware can find a matching network from the
2289 			 * scan. The cfg80211 does not give us the encryption
2290 			 * mode at this stage so just setting it to WEP here.
2291 			 */
2292 			priv->sec_info.encryption_mode =
2293 					WLAN_CIPHER_SUITE_WEP104;
2294 			priv->sec_info.authentication_mode =
2295 					NL80211_AUTHTYPE_OPEN_SYSTEM;
2296 		}
2297 
2298 		goto done;
2299 	}
2300 
2301 	/* Now handle infra mode. "sme" is valid for infra mode only */
2302 	if (sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
2303 		auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2304 		priv->sec_info.is_authtype_auto = 1;
2305 	} else {
2306 		auth_type = sme->auth_type;
2307 	}
2308 
2309 	if (sme->crypto.n_ciphers_pairwise) {
2310 		priv->sec_info.encryption_mode =
2311 						sme->crypto.ciphers_pairwise[0];
2312 		priv->sec_info.authentication_mode = auth_type;
2313 	}
2314 
2315 	if (sme->crypto.cipher_group) {
2316 		priv->sec_info.encryption_mode = sme->crypto.cipher_group;
2317 		priv->sec_info.authentication_mode = auth_type;
2318 	}
2319 	if (sme->ie)
2320 		ret = mwifiex_set_gen_ie(priv, sme->ie, sme->ie_len);
2321 
2322 	if (sme->key) {
2323 		if (mwifiex_is_alg_wep(priv->sec_info.encryption_mode)) {
2324 			mwifiex_dbg(priv->adapter, INFO,
2325 				    "info: setting wep encryption\t"
2326 				    "with key len %d\n", sme->key_len);
2327 			priv->wep_key_curr_index = sme->key_idx;
2328 			ret = mwifiex_set_encode(priv, NULL, sme->key,
2329 						 sme->key_len, sme->key_idx,
2330 						 NULL, 0);
2331 		}
2332 	}
2333 done:
2334 	/*
2335 	 * Scan entries are valid for some time (15 sec). So we can save one
2336 	 * active scan time if we just try cfg80211_get_bss first. If it fails
2337 	 * then request scan and cfg80211_get_bss() again for final output.
2338 	 */
2339 	while (1) {
2340 		if (is_scanning_required) {
2341 			/* Do specific SSID scanning */
2342 			if (mwifiex_request_scan(priv, &req_ssid)) {
2343 				mwifiex_dbg(priv->adapter, ERROR, "scan error\n");
2344 				return -EFAULT;
2345 			}
2346 		}
2347 
2348 		/* Find the BSS we want using available scan results */
2349 		if (mode == NL80211_IFTYPE_ADHOC)
2350 			bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2351 					       bssid, ssid, ssid_len,
2352 					       IEEE80211_BSS_TYPE_IBSS,
2353 					       IEEE80211_PRIVACY_ANY);
2354 		else
2355 			bss = cfg80211_get_bss(priv->wdev.wiphy, channel,
2356 					       bssid, ssid, ssid_len,
2357 					       IEEE80211_BSS_TYPE_ESS,
2358 					       IEEE80211_PRIVACY_ANY);
2359 
2360 		if (!bss) {
2361 			if (is_scanning_required) {
2362 				mwifiex_dbg(priv->adapter, MSG,
2363 					    "assoc: requested bss not found in scan results\n");
2364 				break;
2365 			}
2366 			is_scanning_required = 1;
2367 		} else {
2368 			mwifiex_dbg(priv->adapter, MSG,
2369 				    "info: trying to associate to bssid %pM\n",
2370 				    bss->bssid);
2371 			memcpy(&priv->cfg_bssid, bss->bssid, ETH_ALEN);
2372 			break;
2373 		}
2374 	}
2375 
2376 	if (bss)
2377 		cfg80211_ref_bss(priv->adapter->wiphy, bss);
2378 
2379 	ret = mwifiex_bss_start(priv, bss, &req_ssid);
2380 	if (ret)
2381 		goto cleanup;
2382 
2383 	if (mode == NL80211_IFTYPE_ADHOC) {
2384 		/* Inform the BSS information to kernel, otherwise
2385 		 * kernel will give a panic after successful assoc */
2386 		if (mwifiex_cfg80211_inform_ibss_bss(priv)) {
2387 			ret = -EFAULT;
2388 			goto cleanup;
2389 		}
2390 	}
2391 
2392 	/* Pass the selected BSS entry to caller. */
2393 	if (sel_bss) {
2394 		*sel_bss = bss;
2395 		bss = NULL;
2396 	}
2397 
2398 cleanup:
2399 	if (bss)
2400 		cfg80211_put_bss(priv->adapter->wiphy, bss);
2401 	return ret;
2402 }
2403 
2404 /*
2405  * CFG802.11 operation handler for association request.
2406  *
2407  * This function does not work when the current mode is set to Ad-Hoc, or
2408  * when there is already an association procedure going on. The given BSS
2409  * information is used to associate.
2410  */
2411 static int
mwifiex_cfg80211_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)2412 mwifiex_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
2413 			 struct cfg80211_connect_params *sme)
2414 {
2415 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2416 	struct mwifiex_adapter *adapter = priv->adapter;
2417 	struct cfg80211_bss *bss = NULL;
2418 	int ret;
2419 
2420 	if (GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) {
2421 		mwifiex_dbg(adapter, ERROR,
2422 			    "%s: reject infra assoc request in non-STA role\n",
2423 			    dev->name);
2424 		return -EINVAL;
2425 	}
2426 
2427 	if (priv->wdev.connected) {
2428 		mwifiex_dbg(adapter, ERROR,
2429 			    "%s: already connected\n", dev->name);
2430 		return -EALREADY;
2431 	}
2432 
2433 	if (priv->scan_block)
2434 		priv->scan_block = false;
2435 
2436 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags) ||
2437 	    test_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags)) {
2438 		mwifiex_dbg(adapter, ERROR,
2439 			    "%s: Ignore connection.\t"
2440 			    "Card removed or FW in bad state\n",
2441 			    dev->name);
2442 		return -EFAULT;
2443 	}
2444 
2445 	mwifiex_dbg(adapter, INFO,
2446 		    "info: Trying to associate to bssid %pM\n", sme->bssid);
2447 
2448 	if (!mwifiex_stop_bg_scan(priv))
2449 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2450 
2451 	ret = mwifiex_cfg80211_assoc(priv, sme->ssid_len, sme->ssid, sme->bssid,
2452 				     priv->bss_mode, sme->channel, sme, 0,
2453 				     &bss);
2454 	if (!ret) {
2455 		cfg80211_connect_bss(priv->netdev, priv->cfg_bssid, bss, NULL,
2456 				     0, NULL, 0, WLAN_STATUS_SUCCESS,
2457 				     GFP_KERNEL, NL80211_TIMEOUT_UNSPECIFIED);
2458 		mwifiex_dbg(priv->adapter, MSG,
2459 			    "info: associated to bssid %pM successfully\n",
2460 			    priv->cfg_bssid);
2461 		if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
2462 		    priv->adapter->auto_tdls &&
2463 		    priv->bss_type == MWIFIEX_BSS_TYPE_STA)
2464 			mwifiex_setup_auto_tdls_timer(priv);
2465 	} else {
2466 		mwifiex_dbg(priv->adapter, ERROR,
2467 			    "info: association to bssid %pM failed\n",
2468 			    priv->cfg_bssid);
2469 		eth_zero_addr(priv->cfg_bssid);
2470 
2471 		if (ret > 0)
2472 			cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2473 						NULL, 0, NULL, 0, ret,
2474 						GFP_KERNEL);
2475 		else
2476 			cfg80211_connect_result(priv->netdev, priv->cfg_bssid,
2477 						NULL, 0, NULL, 0,
2478 						WLAN_STATUS_UNSPECIFIED_FAILURE,
2479 						GFP_KERNEL);
2480 	}
2481 
2482 	return 0;
2483 }
2484 
2485 /*
2486  * This function sets following parameters for ibss network.
2487  *  -  channel
2488  *  -  start band
2489  *  -  11n flag
2490  *  -  secondary channel offset
2491  */
mwifiex_set_ibss_params(struct mwifiex_private * priv,struct cfg80211_ibss_params * params)2492 static int mwifiex_set_ibss_params(struct mwifiex_private *priv,
2493 				   struct cfg80211_ibss_params *params)
2494 {
2495 	struct mwifiex_adapter *adapter = priv->adapter;
2496 	int index = 0, i;
2497 	u8 config_bands = 0;
2498 
2499 	if (params->chandef.chan->band == NL80211_BAND_2GHZ) {
2500 		if (!params->basic_rates) {
2501 			config_bands = BAND_B | BAND_G;
2502 		} else {
2503 			for (i = 0; i < mwifiex_band_2ghz.n_bitrates; i++) {
2504 				/*
2505 				 * Rates below 6 Mbps in the table are CCK
2506 				 * rates; 802.11b and from 6 they are OFDM;
2507 				 * 802.11G
2508 				 */
2509 				if (mwifiex_rates[i].bitrate == 60) {
2510 					index = 1 << i;
2511 					break;
2512 				}
2513 			}
2514 
2515 			if (params->basic_rates < index) {
2516 				config_bands = BAND_B;
2517 			} else {
2518 				config_bands = BAND_G;
2519 				if (params->basic_rates % index)
2520 					config_bands |= BAND_B;
2521 			}
2522 		}
2523 
2524 		if (cfg80211_get_chandef_type(&params->chandef) !=
2525 						NL80211_CHAN_NO_HT)
2526 			config_bands |= BAND_G | BAND_GN;
2527 	} else {
2528 		if (cfg80211_get_chandef_type(&params->chandef) ==
2529 						NL80211_CHAN_NO_HT)
2530 			config_bands = BAND_A;
2531 		else
2532 			config_bands = BAND_AN | BAND_A;
2533 	}
2534 
2535 	if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands)) {
2536 		adapter->config_bands = config_bands;
2537 		adapter->adhoc_start_band = config_bands;
2538 
2539 		if ((config_bands & BAND_GN) || (config_bands & BAND_AN))
2540 			adapter->adhoc_11n_enabled = true;
2541 		else
2542 			adapter->adhoc_11n_enabled = false;
2543 	}
2544 
2545 	adapter->sec_chan_offset =
2546 		mwifiex_chan_type_to_sec_chan_offset(
2547 			cfg80211_get_chandef_type(&params->chandef));
2548 	priv->adhoc_channel = ieee80211_frequency_to_channel(
2549 				params->chandef.chan->center_freq);
2550 
2551 	mwifiex_dbg(adapter, INFO,
2552 		    "info: set ibss band %d, chan %d, chan offset %d\n",
2553 		    config_bands, priv->adhoc_channel,
2554 		    adapter->sec_chan_offset);
2555 
2556 	return 0;
2557 }
2558 
2559 /*
2560  * CFG802.11 operation handler to join an IBSS.
2561  *
2562  * This function does not work in any mode other than Ad-Hoc, or if
2563  * a join operation is already in progress.
2564  */
2565 static int
mwifiex_cfg80211_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)2566 mwifiex_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2567 			   struct cfg80211_ibss_params *params)
2568 {
2569 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2570 	int ret = 0;
2571 
2572 	if (priv->bss_mode != NL80211_IFTYPE_ADHOC) {
2573 		mwifiex_dbg(priv->adapter, ERROR,
2574 			    "request to join ibss received\t"
2575 			    "when station is not in ibss mode\n");
2576 		goto done;
2577 	}
2578 
2579 	mwifiex_dbg(priv->adapter, MSG, "info: trying to join to bssid %pM\n",
2580 		    params->bssid);
2581 
2582 	mwifiex_set_ibss_params(priv, params);
2583 
2584 	ret = mwifiex_cfg80211_assoc(priv, params->ssid_len, params->ssid,
2585 				     params->bssid, priv->bss_mode,
2586 				     params->chandef.chan, NULL,
2587 				     params->privacy, NULL);
2588 done:
2589 	if (!ret) {
2590 		cfg80211_ibss_joined(priv->netdev, priv->cfg_bssid,
2591 				     params->chandef.chan, GFP_KERNEL);
2592 		mwifiex_dbg(priv->adapter, MSG,
2593 			    "info: joined/created adhoc network with bssid\t"
2594 			    "%pM successfully\n", priv->cfg_bssid);
2595 	} else {
2596 		mwifiex_dbg(priv->adapter, ERROR,
2597 			    "info: failed creating/joining adhoc network\n");
2598 	}
2599 
2600 	return ret;
2601 }
2602 
2603 /*
2604  * CFG802.11 operation handler to leave an IBSS.
2605  *
2606  * This function does not work if a leave operation is
2607  * already in progress.
2608  */
2609 static int
mwifiex_cfg80211_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2610 mwifiex_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2611 {
2612 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2613 
2614 	mwifiex_dbg(priv->adapter, MSG, "info: disconnecting from essid %pM\n",
2615 		    priv->cfg_bssid);
2616 	if (mwifiex_deauthenticate(priv, NULL))
2617 		return -EFAULT;
2618 
2619 	eth_zero_addr(priv->cfg_bssid);
2620 
2621 	return 0;
2622 }
2623 
2624 /*
2625  * CFG802.11 operation handler for scan request.
2626  *
2627  * This function issues a scan request to the firmware based upon
2628  * the user specified scan configuration. On successful completion,
2629  * it also informs the results.
2630  */
2631 static int
mwifiex_cfg80211_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)2632 mwifiex_cfg80211_scan(struct wiphy *wiphy,
2633 		      struct cfg80211_scan_request *request)
2634 {
2635 	struct net_device *dev = request->wdev->netdev;
2636 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2637 	int i, offset, ret;
2638 	struct ieee80211_channel *chan;
2639 	struct ieee_types_header *ie;
2640 	struct mwifiex_user_scan_cfg *user_scan_cfg;
2641 	u8 mac_addr[ETH_ALEN];
2642 
2643 	mwifiex_dbg(priv->adapter, CMD,
2644 		    "info: received scan request on %s\n", dev->name);
2645 
2646 	/* Block scan request if scan operation or scan cleanup when interface
2647 	 * is disabled is in process
2648 	 */
2649 	if (priv->scan_request || priv->scan_aborting) {
2650 		mwifiex_dbg(priv->adapter, WARN,
2651 			    "cmd: Scan already in process..\n");
2652 		return -EBUSY;
2653 	}
2654 
2655 	if (!priv->wdev.connected && priv->scan_block)
2656 		priv->scan_block = false;
2657 
2658 	if (!mwifiex_stop_bg_scan(priv))
2659 		cfg80211_sched_scan_stopped_locked(priv->wdev.wiphy, 0);
2660 
2661 	user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
2662 	if (!user_scan_cfg)
2663 		return -ENOMEM;
2664 
2665 	priv->scan_request = request;
2666 
2667 	if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2668 		get_random_mask_addr(mac_addr, request->mac_addr,
2669 				     request->mac_addr_mask);
2670 		ether_addr_copy(request->mac_addr, mac_addr);
2671 		ether_addr_copy(user_scan_cfg->random_mac, mac_addr);
2672 	}
2673 
2674 	user_scan_cfg->num_ssids = request->n_ssids;
2675 	user_scan_cfg->ssid_list = request->ssids;
2676 
2677 	if (request->ie && request->ie_len) {
2678 		offset = 0;
2679 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2680 			if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2681 				continue;
2682 			priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_SCAN;
2683 			ie = (struct ieee_types_header *)(request->ie + offset);
2684 			memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2685 			offset += sizeof(*ie) + ie->len;
2686 
2687 			if (offset >= request->ie_len)
2688 				break;
2689 		}
2690 	}
2691 
2692 	for (i = 0; i < min_t(u32, request->n_channels,
2693 			      MWIFIEX_USER_SCAN_CHAN_MAX); i++) {
2694 		chan = request->channels[i];
2695 		user_scan_cfg->chan_list[i].chan_number = chan->hw_value;
2696 		user_scan_cfg->chan_list[i].radio_type = chan->band;
2697 
2698 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2699 			user_scan_cfg->chan_list[i].scan_type =
2700 						MWIFIEX_SCAN_TYPE_PASSIVE;
2701 		else
2702 			user_scan_cfg->chan_list[i].scan_type =
2703 						MWIFIEX_SCAN_TYPE_ACTIVE;
2704 
2705 		user_scan_cfg->chan_list[i].scan_time = 0;
2706 	}
2707 
2708 	if (priv->adapter->scan_chan_gap_enabled &&
2709 	    mwifiex_is_any_intf_active(priv))
2710 		user_scan_cfg->scan_chan_gap =
2711 					      priv->adapter->scan_chan_gap_time;
2712 
2713 	ret = mwifiex_scan_networks(priv, user_scan_cfg);
2714 	kfree(user_scan_cfg);
2715 	if (ret) {
2716 		mwifiex_dbg(priv->adapter, ERROR,
2717 			    "scan failed: %d\n", ret);
2718 		priv->scan_aborting = false;
2719 		priv->scan_request = NULL;
2720 		return ret;
2721 	}
2722 
2723 	if (request->ie && request->ie_len) {
2724 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2725 			if (priv->vs_ie[i].mask == MWIFIEX_VSIE_MASK_SCAN) {
2726 				priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_CLEAR;
2727 				memset(&priv->vs_ie[i].ie, 0,
2728 				       MWIFIEX_MAX_VSIE_LEN);
2729 			}
2730 		}
2731 	}
2732 	return 0;
2733 }
2734 
2735 /* CFG802.11 operation handler for sched_scan_start.
2736  *
2737  * This function issues a bgscan config request to the firmware based upon
2738  * the user specified sched_scan configuration. On successful completion,
2739  * firmware will generate BGSCAN_REPORT event, driver should issue bgscan
2740  * query command to get sched_scan results from firmware.
2741  */
2742 static int
mwifiex_cfg80211_sched_scan_start(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_sched_scan_request * request)2743 mwifiex_cfg80211_sched_scan_start(struct wiphy *wiphy,
2744 				  struct net_device *dev,
2745 				  struct cfg80211_sched_scan_request *request)
2746 {
2747 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2748 	int i, offset;
2749 	struct ieee80211_channel *chan;
2750 	struct mwifiex_bg_scan_cfg *bgscan_cfg;
2751 	struct ieee_types_header *ie;
2752 
2753 	if (!request || (!request->n_ssids && !request->n_match_sets)) {
2754 		wiphy_err(wiphy, "%s : Invalid Sched_scan parameters",
2755 			  __func__);
2756 		return -EINVAL;
2757 	}
2758 
2759 	wiphy_info(wiphy, "sched_scan start : n_ssids=%d n_match_sets=%d ",
2760 		   request->n_ssids, request->n_match_sets);
2761 	wiphy_info(wiphy, "n_channels=%d interval=%d ie_len=%d\n",
2762 		   request->n_channels, request->scan_plans->interval,
2763 		   (int)request->ie_len);
2764 
2765 	bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2766 	if (!bgscan_cfg)
2767 		return -ENOMEM;
2768 
2769 	if (priv->scan_request || priv->scan_aborting)
2770 		bgscan_cfg->start_later = true;
2771 
2772 	bgscan_cfg->num_ssids = request->n_match_sets;
2773 	bgscan_cfg->ssid_list = request->match_sets;
2774 
2775 	if (request->ie && request->ie_len) {
2776 		offset = 0;
2777 		for (i = 0; i < MWIFIEX_MAX_VSIE_NUM; i++) {
2778 			if (priv->vs_ie[i].mask != MWIFIEX_VSIE_MASK_CLEAR)
2779 				continue;
2780 			priv->vs_ie[i].mask = MWIFIEX_VSIE_MASK_BGSCAN;
2781 			ie = (struct ieee_types_header *)(request->ie + offset);
2782 			memcpy(&priv->vs_ie[i].ie, ie, sizeof(*ie) + ie->len);
2783 			offset += sizeof(*ie) + ie->len;
2784 
2785 			if (offset >= request->ie_len)
2786 				break;
2787 		}
2788 	}
2789 
2790 	for (i = 0; i < min_t(u32, request->n_channels,
2791 			      MWIFIEX_BG_SCAN_CHAN_MAX); i++) {
2792 		chan = request->channels[i];
2793 		bgscan_cfg->chan_list[i].chan_number = chan->hw_value;
2794 		bgscan_cfg->chan_list[i].radio_type = chan->band;
2795 
2796 		if ((chan->flags & IEEE80211_CHAN_NO_IR) || !request->n_ssids)
2797 			bgscan_cfg->chan_list[i].scan_type =
2798 						MWIFIEX_SCAN_TYPE_PASSIVE;
2799 		else
2800 			bgscan_cfg->chan_list[i].scan_type =
2801 						MWIFIEX_SCAN_TYPE_ACTIVE;
2802 
2803 		bgscan_cfg->chan_list[i].scan_time = 0;
2804 	}
2805 
2806 	bgscan_cfg->chan_per_scan = min_t(u32, request->n_channels,
2807 					  MWIFIEX_BG_SCAN_CHAN_MAX);
2808 
2809 	/* Use at least 15 second for per scan cycle */
2810 	bgscan_cfg->scan_interval = (request->scan_plans->interval >
2811 				     MWIFIEX_BGSCAN_INTERVAL) ?
2812 				request->scan_plans->interval :
2813 				MWIFIEX_BGSCAN_INTERVAL;
2814 
2815 	bgscan_cfg->repeat_count = MWIFIEX_BGSCAN_REPEAT_COUNT;
2816 	bgscan_cfg->report_condition = MWIFIEX_BGSCAN_SSID_MATCH |
2817 				MWIFIEX_BGSCAN_WAIT_ALL_CHAN_DONE;
2818 	bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2819 	bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2820 	bgscan_cfg->enable = true;
2821 	if (request->min_rssi_thold != NL80211_SCAN_RSSI_THOLD_OFF) {
2822 		bgscan_cfg->report_condition |= MWIFIEX_BGSCAN_SSID_RSSI_MATCH;
2823 		bgscan_cfg->rssi_threshold = request->min_rssi_thold;
2824 	}
2825 
2826 	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2827 			     HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2828 		kfree(bgscan_cfg);
2829 		return -EFAULT;
2830 	}
2831 
2832 	priv->sched_scanning = true;
2833 
2834 	kfree(bgscan_cfg);
2835 	return 0;
2836 }
2837 
2838 /* CFG802.11 operation handler for sched_scan_stop.
2839  *
2840  * This function issues a bgscan config command to disable
2841  * previous bgscan configuration in the firmware
2842  */
mwifiex_cfg80211_sched_scan_stop(struct wiphy * wiphy,struct net_device * dev,u64 reqid)2843 static int mwifiex_cfg80211_sched_scan_stop(struct wiphy *wiphy,
2844 					    struct net_device *dev, u64 reqid)
2845 {
2846 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
2847 
2848 	wiphy_info(wiphy, "sched scan stop!");
2849 	mwifiex_stop_bg_scan(priv);
2850 
2851 	return 0;
2852 }
2853 
mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap * vht_info,struct mwifiex_private * priv)2854 static void mwifiex_setup_vht_caps(struct ieee80211_sta_vht_cap *vht_info,
2855 				   struct mwifiex_private *priv)
2856 {
2857 	struct mwifiex_adapter *adapter = priv->adapter;
2858 
2859 	vht_info->vht_supported = true;
2860 
2861 	vht_info->cap = adapter->hw_dot_11ac_dev_cap;
2862 	/* Update MCS support for VHT */
2863 	vht_info->vht_mcs.rx_mcs_map = cpu_to_le16(
2864 				adapter->hw_dot_11ac_mcs_support & 0xFFFF);
2865 	vht_info->vht_mcs.rx_highest = 0;
2866 	vht_info->vht_mcs.tx_mcs_map = cpu_to_le16(
2867 				adapter->hw_dot_11ac_mcs_support >> 16);
2868 	vht_info->vht_mcs.tx_highest = 0;
2869 }
2870 
2871 /*
2872  * This function sets up the CFG802.11 specific HT capability fields
2873  * with default values.
2874  *
2875  * The following default values are set -
2876  *      - HT Supported = True
2877  *      - Maximum AMPDU length factor = IEEE80211_HT_MAX_AMPDU_64K
2878  *      - Minimum AMPDU spacing = IEEE80211_HT_MPDU_DENSITY_NONE
2879  *      - HT Capabilities supported by firmware
2880  *      - MCS information, Rx mask = 0xff
2881  *      - MCD information, Tx parameters = IEEE80211_HT_MCS_TX_DEFINED (0x01)
2882  */
2883 static void
mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap * ht_info,struct mwifiex_private * priv)2884 mwifiex_setup_ht_caps(struct ieee80211_sta_ht_cap *ht_info,
2885 		      struct mwifiex_private *priv)
2886 {
2887 	int rx_mcs_supp;
2888 	struct ieee80211_mcs_info mcs_set;
2889 	u8 *mcs = (u8 *)&mcs_set;
2890 	struct mwifiex_adapter *adapter = priv->adapter;
2891 
2892 	ht_info->ht_supported = true;
2893 	ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2894 	ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2895 
2896 	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
2897 
2898 	/* Fill HT capability information */
2899 	if (ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2900 		ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2901 	else
2902 		ht_info->cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2903 
2904 	if (ISSUPP_SHORTGI20(adapter->hw_dot_11n_dev_cap))
2905 		ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
2906 	else
2907 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_20;
2908 
2909 	if (ISSUPP_SHORTGI40(adapter->hw_dot_11n_dev_cap))
2910 		ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
2911 	else
2912 		ht_info->cap &= ~IEEE80211_HT_CAP_SGI_40;
2913 
2914 	if (adapter->user_dev_mcs_support == HT_STREAM_2X2)
2915 		ht_info->cap |= 2 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2916 	else
2917 		ht_info->cap |= 1 << IEEE80211_HT_CAP_RX_STBC_SHIFT;
2918 
2919 	if (ISSUPP_TXSTBC(adapter->hw_dot_11n_dev_cap))
2920 		ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
2921 	else
2922 		ht_info->cap &= ~IEEE80211_HT_CAP_TX_STBC;
2923 
2924 	if (ISSUPP_GREENFIELD(adapter->hw_dot_11n_dev_cap))
2925 		ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
2926 	else
2927 		ht_info->cap &= ~IEEE80211_HT_CAP_GRN_FLD;
2928 
2929 	if (ISENABLED_40MHZ_INTOLERANT(adapter->hw_dot_11n_dev_cap))
2930 		ht_info->cap |= IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2931 	else
2932 		ht_info->cap &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
2933 
2934 	if (ISSUPP_RXLDPC(adapter->hw_dot_11n_dev_cap))
2935 		ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
2936 	else
2937 		ht_info->cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
2938 
2939 	ht_info->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
2940 	ht_info->cap |= IEEE80211_HT_CAP_SM_PS;
2941 
2942 	rx_mcs_supp = GET_RXMCSSUPP(adapter->user_dev_mcs_support);
2943 	/* Set MCS for 1x1/2x2 */
2944 	memset(mcs, 0xff, rx_mcs_supp);
2945 	/* Clear all the other values */
2946 	memset(&mcs[rx_mcs_supp], 0,
2947 	       sizeof(struct ieee80211_mcs_info) - rx_mcs_supp);
2948 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2949 	    ISSUPP_CHANWIDTH40(adapter->hw_dot_11n_dev_cap))
2950 		/* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
2951 		SETHT_MCS32(mcs_set.rx_mask);
2952 
2953 	memcpy((u8 *) &ht_info->mcs, mcs, sizeof(struct ieee80211_mcs_info));
2954 
2955 	ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2956 }
2957 
2958 /*
2959  *  create a new virtual interface with the given name and name assign type
2960  */
mwifiex_add_virtual_intf(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)2961 struct wireless_dev *mwifiex_add_virtual_intf(struct wiphy *wiphy,
2962 					      const char *name,
2963 					      unsigned char name_assign_type,
2964 					      enum nl80211_iftype type,
2965 					      struct vif_params *params)
2966 {
2967 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
2968 	struct mwifiex_private *priv;
2969 	struct net_device *dev;
2970 	void *mdev_priv;
2971 	int ret;
2972 
2973 	if (!adapter)
2974 		return ERR_PTR(-EFAULT);
2975 
2976 	switch (type) {
2977 	case NL80211_IFTYPE_UNSPECIFIED:
2978 	case NL80211_IFTYPE_STATION:
2979 	case NL80211_IFTYPE_ADHOC:
2980 		if (adapter->curr_iface_comb.sta_intf ==
2981 		    adapter->iface_limit.sta_intf) {
2982 			mwifiex_dbg(adapter, ERROR,
2983 				    "cannot create multiple sta/adhoc ifaces\n");
2984 			return ERR_PTR(-EINVAL);
2985 		}
2986 
2987 		priv = mwifiex_get_unused_priv_by_bss_type(
2988 						adapter, MWIFIEX_BSS_TYPE_STA);
2989 		if (!priv) {
2990 			mwifiex_dbg(adapter, ERROR,
2991 				    "could not get free private struct\n");
2992 			return ERR_PTR(-EFAULT);
2993 		}
2994 
2995 		priv->wdev.wiphy = wiphy;
2996 		priv->wdev.iftype = NL80211_IFTYPE_STATION;
2997 
2998 		if (type == NL80211_IFTYPE_UNSPECIFIED)
2999 			priv->bss_mode = NL80211_IFTYPE_STATION;
3000 		else
3001 			priv->bss_mode = type;
3002 
3003 		priv->bss_type = MWIFIEX_BSS_TYPE_STA;
3004 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
3005 		priv->bss_priority = 0;
3006 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
3007 
3008 		break;
3009 	case NL80211_IFTYPE_AP:
3010 		if (adapter->curr_iface_comb.uap_intf ==
3011 		    adapter->iface_limit.uap_intf) {
3012 			mwifiex_dbg(adapter, ERROR,
3013 				    "cannot create multiple AP ifaces\n");
3014 			return ERR_PTR(-EINVAL);
3015 		}
3016 
3017 		priv = mwifiex_get_unused_priv_by_bss_type(
3018 						adapter, MWIFIEX_BSS_TYPE_UAP);
3019 		if (!priv) {
3020 			mwifiex_dbg(adapter, ERROR,
3021 				    "could not get free private struct\n");
3022 			return ERR_PTR(-EFAULT);
3023 		}
3024 
3025 		priv->wdev.wiphy = wiphy;
3026 		priv->wdev.iftype = NL80211_IFTYPE_AP;
3027 
3028 		priv->bss_type = MWIFIEX_BSS_TYPE_UAP;
3029 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
3030 		priv->bss_priority = 0;
3031 		priv->bss_role = MWIFIEX_BSS_ROLE_UAP;
3032 		priv->bss_started = 0;
3033 		priv->bss_mode = type;
3034 
3035 		break;
3036 	case NL80211_IFTYPE_P2P_CLIENT:
3037 		if (adapter->curr_iface_comb.p2p_intf ==
3038 		    adapter->iface_limit.p2p_intf) {
3039 			mwifiex_dbg(adapter, ERROR,
3040 				    "cannot create multiple P2P ifaces\n");
3041 			return ERR_PTR(-EINVAL);
3042 		}
3043 
3044 		priv = mwifiex_get_unused_priv_by_bss_type(
3045 						adapter, MWIFIEX_BSS_TYPE_P2P);
3046 		if (!priv) {
3047 			mwifiex_dbg(adapter, ERROR,
3048 				    "could not get free private struct\n");
3049 			return ERR_PTR(-EFAULT);
3050 		}
3051 
3052 		priv->wdev.wiphy = wiphy;
3053 		/* At start-up, wpa_supplicant tries to change the interface
3054 		 * to NL80211_IFTYPE_STATION if it is not managed mode.
3055 		 */
3056 		priv->wdev.iftype = NL80211_IFTYPE_P2P_CLIENT;
3057 		priv->bss_mode = NL80211_IFTYPE_P2P_CLIENT;
3058 
3059 		/* Setting bss_type to P2P tells firmware that this interface
3060 		 * is receiving P2P peers found during find phase and doing
3061 		 * action frame handshake.
3062 		 */
3063 		priv->bss_type = MWIFIEX_BSS_TYPE_P2P;
3064 
3065 		priv->frame_type = MWIFIEX_DATA_FRAME_TYPE_ETH_II;
3066 		priv->bss_priority = 0;
3067 		priv->bss_role = MWIFIEX_BSS_ROLE_STA;
3068 		priv->bss_started = 0;
3069 
3070 		if (mwifiex_cfg80211_init_p2p_client(priv)) {
3071 			memset(&priv->wdev, 0, sizeof(priv->wdev));
3072 			priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3073 			return ERR_PTR(-EFAULT);
3074 		}
3075 
3076 		break;
3077 	default:
3078 		mwifiex_dbg(adapter, ERROR, "type not supported\n");
3079 		return ERR_PTR(-EINVAL);
3080 	}
3081 
3082 	dev = alloc_netdev_mqs(sizeof(struct mwifiex_private *), name,
3083 			       name_assign_type, ether_setup,
3084 			       IEEE80211_NUM_ACS, 1);
3085 	if (!dev) {
3086 		mwifiex_dbg(adapter, ERROR,
3087 			    "no memory available for netdevice\n");
3088 		ret = -ENOMEM;
3089 		goto err_alloc_netdev;
3090 	}
3091 
3092 	mwifiex_init_priv_params(priv, dev);
3093 
3094 	priv->netdev = dev;
3095 
3096 	if (!adapter->mfg_mode) {
3097 		mwifiex_set_mac_address(priv, dev, false, NULL);
3098 
3099 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_SET_BSS_MODE,
3100 				       HostCmd_ACT_GEN_SET, 0, NULL, true);
3101 		if (ret)
3102 			goto err_set_bss_mode;
3103 
3104 		ret = mwifiex_sta_init_cmd(priv, false, false);
3105 		if (ret)
3106 			goto err_sta_init;
3107 	}
3108 
3109 	mwifiex_setup_ht_caps(&wiphy->bands[NL80211_BAND_2GHZ]->ht_cap, priv);
3110 	if (adapter->is_hw_11ac_capable)
3111 		mwifiex_setup_vht_caps(
3112 			&wiphy->bands[NL80211_BAND_2GHZ]->vht_cap, priv);
3113 
3114 	if (adapter->config_bands & BAND_A)
3115 		mwifiex_setup_ht_caps(
3116 			&wiphy->bands[NL80211_BAND_5GHZ]->ht_cap, priv);
3117 
3118 	if ((adapter->config_bands & BAND_A) && adapter->is_hw_11ac_capable)
3119 		mwifiex_setup_vht_caps(
3120 			&wiphy->bands[NL80211_BAND_5GHZ]->vht_cap, priv);
3121 
3122 	dev_net_set(dev, wiphy_net(wiphy));
3123 	dev->ieee80211_ptr = &priv->wdev;
3124 	dev->ieee80211_ptr->iftype = priv->bss_mode;
3125 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
3126 
3127 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
3128 	dev->watchdog_timeo = MWIFIEX_DEFAULT_WATCHDOG_TIMEOUT;
3129 	dev->needed_headroom = MWIFIEX_MIN_DATA_HEADER_LEN;
3130 	dev->ethtool_ops = &mwifiex_ethtool_ops;
3131 
3132 	mdev_priv = netdev_priv(dev);
3133 	*((unsigned long *) mdev_priv) = (unsigned long) priv;
3134 
3135 	SET_NETDEV_DEV(dev, adapter->dev);
3136 
3137 	priv->dfs_cac_workqueue = alloc_workqueue("MWIFIEX_DFS_CAC%s",
3138 						  WQ_HIGHPRI |
3139 						  WQ_MEM_RECLAIM |
3140 						  WQ_UNBOUND, 1, name);
3141 	if (!priv->dfs_cac_workqueue) {
3142 		mwifiex_dbg(adapter, ERROR, "cannot alloc DFS CAC queue\n");
3143 		ret = -ENOMEM;
3144 		goto err_alloc_cac;
3145 	}
3146 
3147 	INIT_DELAYED_WORK(&priv->dfs_cac_work, mwifiex_dfs_cac_work_queue);
3148 
3149 	priv->dfs_chan_sw_workqueue = alloc_workqueue("MWIFIEX_DFS_CHSW%s",
3150 						      WQ_HIGHPRI | WQ_UNBOUND |
3151 						      WQ_MEM_RECLAIM, 1, name);
3152 	if (!priv->dfs_chan_sw_workqueue) {
3153 		mwifiex_dbg(adapter, ERROR, "cannot alloc DFS channel sw queue\n");
3154 		ret = -ENOMEM;
3155 		goto err_alloc_chsw;
3156 	}
3157 
3158 	INIT_DELAYED_WORK(&priv->dfs_chan_sw_work,
3159 			  mwifiex_dfs_chan_sw_work_queue);
3160 
3161 	mutex_init(&priv->async_mutex);
3162 
3163 	/* Register network device */
3164 	if (cfg80211_register_netdevice(dev)) {
3165 		mwifiex_dbg(adapter, ERROR, "cannot register network device\n");
3166 		ret = -EFAULT;
3167 		goto err_reg_netdev;
3168 	}
3169 
3170 	mwifiex_dbg(adapter, INFO,
3171 		    "info: %s: Marvell 802.11 Adapter\n", dev->name);
3172 
3173 #ifdef CONFIG_DEBUG_FS
3174 	mwifiex_dev_debugfs_init(priv);
3175 #endif
3176 
3177 	update_vif_type_counter(adapter, type, +1);
3178 
3179 	return &priv->wdev;
3180 
3181 err_reg_netdev:
3182 	destroy_workqueue(priv->dfs_chan_sw_workqueue);
3183 	priv->dfs_chan_sw_workqueue = NULL;
3184 err_alloc_chsw:
3185 	destroy_workqueue(priv->dfs_cac_workqueue);
3186 	priv->dfs_cac_workqueue = NULL;
3187 err_alloc_cac:
3188 	free_netdev(dev);
3189 	priv->netdev = NULL;
3190 err_sta_init:
3191 err_set_bss_mode:
3192 err_alloc_netdev:
3193 	memset(&priv->wdev, 0, sizeof(priv->wdev));
3194 	priv->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
3195 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3196 	return ERR_PTR(ret);
3197 }
3198 EXPORT_SYMBOL_GPL(mwifiex_add_virtual_intf);
3199 
3200 /*
3201  * del_virtual_intf: remove the virtual interface determined by dev
3202  */
mwifiex_del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)3203 int mwifiex_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
3204 {
3205 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
3206 	struct mwifiex_adapter *adapter = priv->adapter;
3207 	struct sk_buff *skb, *tmp;
3208 
3209 #ifdef CONFIG_DEBUG_FS
3210 	mwifiex_dev_debugfs_remove(priv);
3211 #endif
3212 
3213 	if (priv->sched_scanning)
3214 		priv->sched_scanning = false;
3215 
3216 	mwifiex_stop_net_dev_queue(priv->netdev, adapter);
3217 
3218 	skb_queue_walk_safe(&priv->bypass_txq, skb, tmp) {
3219 		skb_unlink(skb, &priv->bypass_txq);
3220 		mwifiex_write_data_complete(priv->adapter, skb, 0, -1);
3221 	}
3222 
3223 	if (netif_carrier_ok(priv->netdev))
3224 		netif_carrier_off(priv->netdev);
3225 
3226 	if (wdev->netdev->reg_state == NETREG_REGISTERED)
3227 		cfg80211_unregister_netdevice(wdev->netdev);
3228 
3229 	if (priv->dfs_cac_workqueue) {
3230 		destroy_workqueue(priv->dfs_cac_workqueue);
3231 		priv->dfs_cac_workqueue = NULL;
3232 	}
3233 
3234 	if (priv->dfs_chan_sw_workqueue) {
3235 		destroy_workqueue(priv->dfs_chan_sw_workqueue);
3236 		priv->dfs_chan_sw_workqueue = NULL;
3237 	}
3238 	/* Clear the priv in adapter */
3239 	priv->netdev = NULL;
3240 
3241 	update_vif_type_counter(adapter, priv->bss_mode, -1);
3242 
3243 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
3244 
3245 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
3246 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
3247 		kfree(priv->hist_data);
3248 
3249 	return 0;
3250 }
3251 EXPORT_SYMBOL_GPL(mwifiex_del_virtual_intf);
3252 
3253 static bool
mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern * pat,s8 * byte_seq,u8 max_byte_seq)3254 mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq,
3255 			     u8 max_byte_seq)
3256 {
3257 	int j, k, valid_byte_cnt = 0;
3258 	bool dont_care_byte = false;
3259 
3260 	for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++) {
3261 		for (k = 0; k < 8; k++) {
3262 			if (pat->mask[j] & 1 << k) {
3263 				memcpy(byte_seq + valid_byte_cnt,
3264 				       &pat->pattern[j * 8 + k], 1);
3265 				valid_byte_cnt++;
3266 				if (dont_care_byte)
3267 					return false;
3268 			} else {
3269 				if (valid_byte_cnt)
3270 					dont_care_byte = true;
3271 			}
3272 
3273 			/* wildcard bytes record as the offset
3274 			 * before the valid byte
3275 			 */
3276 			if (!valid_byte_cnt && !dont_care_byte)
3277 				pat->pkt_offset++;
3278 
3279 			if (valid_byte_cnt > max_byte_seq)
3280 				return false;
3281 		}
3282 	}
3283 
3284 	byte_seq[max_byte_seq] = valid_byte_cnt;
3285 
3286 	return true;
3287 }
3288 
3289 #ifdef CONFIG_PM
mwifiex_set_auto_arp_mef_entry(struct mwifiex_private * priv,struct mwifiex_mef_entry * mef_entry)3290 static void mwifiex_set_auto_arp_mef_entry(struct mwifiex_private *priv,
3291 					   struct mwifiex_mef_entry *mef_entry)
3292 {
3293 	int i, filt_num = 0, num_ipv4 = 0;
3294 	struct in_device *in_dev;
3295 	struct in_ifaddr *ifa;
3296 	__be32 ips[MWIFIEX_MAX_SUPPORTED_IPADDR];
3297 	struct mwifiex_adapter *adapter = priv->adapter;
3298 
3299 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
3300 	mef_entry->action = MEF_ACTION_AUTO_ARP;
3301 
3302 	/* Enable ARP offload feature */
3303 	memset(ips, 0, sizeof(ips));
3304 	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
3305 		if (adapter->priv[i]->netdev) {
3306 			in_dev = __in_dev_get_rtnl(adapter->priv[i]->netdev);
3307 			if (!in_dev)
3308 				continue;
3309 			ifa = rtnl_dereference(in_dev->ifa_list);
3310 			if (!ifa || !ifa->ifa_local)
3311 				continue;
3312 			ips[i] = ifa->ifa_local;
3313 			num_ipv4++;
3314 		}
3315 	}
3316 
3317 	for (i = 0; i < num_ipv4; i++) {
3318 		if (!ips[i])
3319 			continue;
3320 		mef_entry->filter[filt_num].repeat = 1;
3321 		memcpy(mef_entry->filter[filt_num].byte_seq,
3322 		       (u8 *)&ips[i], sizeof(ips[i]));
3323 		mef_entry->filter[filt_num].
3324 			byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3325 			sizeof(ips[i]);
3326 		mef_entry->filter[filt_num].offset = 46;
3327 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3328 		if (filt_num) {
3329 			mef_entry->filter[filt_num].filt_action =
3330 				TYPE_OR;
3331 		}
3332 		filt_num++;
3333 	}
3334 
3335 	mef_entry->filter[filt_num].repeat = 1;
3336 	mef_entry->filter[filt_num].byte_seq[0] = 0x08;
3337 	mef_entry->filter[filt_num].byte_seq[1] = 0x06;
3338 	mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] = 2;
3339 	mef_entry->filter[filt_num].offset = 20;
3340 	mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3341 	mef_entry->filter[filt_num].filt_action = TYPE_AND;
3342 }
3343 
mwifiex_set_wowlan_mef_entry(struct mwifiex_private * priv,struct mwifiex_ds_mef_cfg * mef_cfg,struct mwifiex_mef_entry * mef_entry,struct cfg80211_wowlan * wowlan)3344 static int mwifiex_set_wowlan_mef_entry(struct mwifiex_private *priv,
3345 					struct mwifiex_ds_mef_cfg *mef_cfg,
3346 					struct mwifiex_mef_entry *mef_entry,
3347 					struct cfg80211_wowlan *wowlan)
3348 {
3349 	int i, filt_num = 0, ret = 0;
3350 	bool first_pat = true;
3351 	u8 byte_seq[MWIFIEX_MEF_MAX_BYTESEQ + 1];
3352 	static const u8 ipv4_mc_mac[] = {0x33, 0x33};
3353 	static const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3354 
3355 	mef_entry->mode = MEF_MODE_HOST_SLEEP;
3356 	mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
3357 
3358 	for (i = 0; i < wowlan->n_patterns; i++) {
3359 		memset(byte_seq, 0, sizeof(byte_seq));
3360 		if (!mwifiex_is_pattern_supported(&wowlan->patterns[i],
3361 					byte_seq,
3362 					MWIFIEX_MEF_MAX_BYTESEQ)) {
3363 			mwifiex_dbg(priv->adapter, ERROR,
3364 				    "Pattern not supported\n");
3365 			return -EOPNOTSUPP;
3366 		}
3367 
3368 		if (!wowlan->patterns[i].pkt_offset) {
3369 			if (!(byte_seq[0] & 0x01) &&
3370 			    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 1)) {
3371 				mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3372 				continue;
3373 			} else if (is_broadcast_ether_addr(byte_seq)) {
3374 				mef_cfg->criteria |= MWIFIEX_CRITERIA_BROADCAST;
3375 				continue;
3376 			} else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3377 				    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 2)) ||
3378 				   (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3379 				    (byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] == 3))) {
3380 				mef_cfg->criteria |= MWIFIEX_CRITERIA_MULTICAST;
3381 				continue;
3382 			}
3383 		}
3384 		mef_entry->filter[filt_num].repeat = 1;
3385 		mef_entry->filter[filt_num].offset =
3386 			wowlan->patterns[i].pkt_offset;
3387 		memcpy(mef_entry->filter[filt_num].byte_seq, byte_seq,
3388 				sizeof(byte_seq));
3389 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3390 
3391 		if (first_pat) {
3392 			first_pat = false;
3393 			mwifiex_dbg(priv->adapter, INFO, "Wake on patterns\n");
3394 		} else {
3395 			mef_entry->filter[filt_num].filt_action = TYPE_AND;
3396 		}
3397 
3398 		filt_num++;
3399 	}
3400 
3401 	if (wowlan->magic_pkt) {
3402 		mef_cfg->criteria |= MWIFIEX_CRITERIA_UNICAST;
3403 		mef_entry->filter[filt_num].repeat = 16;
3404 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3405 				ETH_ALEN);
3406 		mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3407 			ETH_ALEN;
3408 		mef_entry->filter[filt_num].offset = 28;
3409 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3410 		if (filt_num)
3411 			mef_entry->filter[filt_num].filt_action = TYPE_OR;
3412 
3413 		filt_num++;
3414 		mef_entry->filter[filt_num].repeat = 16;
3415 		memcpy(mef_entry->filter[filt_num].byte_seq, priv->curr_addr,
3416 				ETH_ALEN);
3417 		mef_entry->filter[filt_num].byte_seq[MWIFIEX_MEF_MAX_BYTESEQ] =
3418 			ETH_ALEN;
3419 		mef_entry->filter[filt_num].offset = 56;
3420 		mef_entry->filter[filt_num].filt_type = TYPE_EQ;
3421 		mef_entry->filter[filt_num].filt_action = TYPE_OR;
3422 		mwifiex_dbg(priv->adapter, INFO, "Wake on magic packet\n");
3423 	}
3424 	return ret;
3425 }
3426 
mwifiex_set_mef_filter(struct mwifiex_private * priv,struct cfg80211_wowlan * wowlan)3427 static int mwifiex_set_mef_filter(struct mwifiex_private *priv,
3428 				  struct cfg80211_wowlan *wowlan)
3429 {
3430 	int ret = 0, num_entries = 1;
3431 	struct mwifiex_ds_mef_cfg mef_cfg;
3432 	struct mwifiex_mef_entry *mef_entry;
3433 
3434 	if (wowlan->n_patterns || wowlan->magic_pkt)
3435 		num_entries++;
3436 
3437 	mef_entry = kcalloc(num_entries, sizeof(*mef_entry), GFP_KERNEL);
3438 	if (!mef_entry)
3439 		return -ENOMEM;
3440 
3441 	memset(&mef_cfg, 0, sizeof(mef_cfg));
3442 	mef_cfg.criteria |= MWIFIEX_CRITERIA_BROADCAST |
3443 		MWIFIEX_CRITERIA_UNICAST;
3444 	mef_cfg.num_entries = num_entries;
3445 	mef_cfg.mef_entry = mef_entry;
3446 
3447 	mwifiex_set_auto_arp_mef_entry(priv, &mef_entry[0]);
3448 
3449 	if (wowlan->n_patterns || wowlan->magic_pkt) {
3450 		ret = mwifiex_set_wowlan_mef_entry(priv, &mef_cfg,
3451 						   &mef_entry[1], wowlan);
3452 		if (ret)
3453 			goto err;
3454 	}
3455 
3456 	if (!mef_cfg.criteria)
3457 		mef_cfg.criteria = MWIFIEX_CRITERIA_BROADCAST |
3458 			MWIFIEX_CRITERIA_UNICAST |
3459 			MWIFIEX_CRITERIA_MULTICAST;
3460 
3461 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_MEF_CFG,
3462 			HostCmd_ACT_GEN_SET, 0,
3463 			&mef_cfg, true);
3464 
3465 err:
3466 	kfree(mef_entry);
3467 	return ret;
3468 }
3469 
mwifiex_cfg80211_suspend(struct wiphy * wiphy,struct cfg80211_wowlan * wowlan)3470 static int mwifiex_cfg80211_suspend(struct wiphy *wiphy,
3471 				    struct cfg80211_wowlan *wowlan)
3472 {
3473 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3474 	struct mwifiex_ds_hs_cfg hs_cfg;
3475 	int i, ret = 0, retry_num = 10;
3476 	struct mwifiex_private *priv;
3477 	struct mwifiex_private *sta_priv =
3478 			mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3479 
3480 	sta_priv->scan_aborting = true;
3481 	for (i = 0; i < adapter->priv_num; i++) {
3482 		priv = adapter->priv[i];
3483 		mwifiex_abort_cac(priv);
3484 	}
3485 
3486 	mwifiex_cancel_all_pending_cmd(adapter);
3487 
3488 	for (i = 0; i < adapter->priv_num; i++) {
3489 		priv = adapter->priv[i];
3490 		if (priv && priv->netdev)
3491 			netif_device_detach(priv->netdev);
3492 	}
3493 
3494 	for (i = 0; i < retry_num; i++) {
3495 		if (!mwifiex_wmm_lists_empty(adapter) ||
3496 		    !mwifiex_bypass_txlist_empty(adapter) ||
3497 		    !skb_queue_empty(&adapter->tx_data_q))
3498 			usleep_range(10000, 15000);
3499 		else
3500 			break;
3501 	}
3502 
3503 	if (!wowlan) {
3504 		mwifiex_dbg(adapter, INFO,
3505 			    "None of the WOWLAN triggers enabled\n");
3506 		ret = 0;
3507 		goto done;
3508 	}
3509 
3510 	if (!sta_priv->media_connected && !wowlan->nd_config) {
3511 		mwifiex_dbg(adapter, ERROR,
3512 			    "Can not configure WOWLAN in disconnected state\n");
3513 		ret = 0;
3514 		goto done;
3515 	}
3516 
3517 	ret = mwifiex_set_mef_filter(sta_priv, wowlan);
3518 	if (ret) {
3519 		mwifiex_dbg(adapter, ERROR, "Failed to set MEF filter\n");
3520 		goto done;
3521 	}
3522 
3523 	memset(&hs_cfg, 0, sizeof(hs_cfg));
3524 	hs_cfg.conditions = le32_to_cpu(adapter->hs_cfg.conditions);
3525 
3526 	if (wowlan->nd_config) {
3527 		mwifiex_dbg(adapter, INFO, "Wake on net detect\n");
3528 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3529 		mwifiex_cfg80211_sched_scan_start(wiphy, sta_priv->netdev,
3530 						  wowlan->nd_config);
3531 	}
3532 
3533 	if (wowlan->disconnect) {
3534 		hs_cfg.conditions |= HS_CFG_COND_MAC_EVENT;
3535 		mwifiex_dbg(sta_priv->adapter, INFO, "Wake on device disconnect\n");
3536 	}
3537 
3538 	hs_cfg.is_invoke_hostcmd = false;
3539 	hs_cfg.gpio = adapter->hs_cfg.gpio;
3540 	hs_cfg.gap = adapter->hs_cfg.gap;
3541 	ret = mwifiex_set_hs_params(sta_priv, HostCmd_ACT_GEN_SET,
3542 				    MWIFIEX_SYNC_CMD, &hs_cfg);
3543 	if (ret)
3544 		mwifiex_dbg(adapter, ERROR, "Failed to set HS params\n");
3545 
3546 done:
3547 	sta_priv->scan_aborting = false;
3548 	return ret;
3549 }
3550 
mwifiex_cfg80211_resume(struct wiphy * wiphy)3551 static int mwifiex_cfg80211_resume(struct wiphy *wiphy)
3552 {
3553 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3554 	struct mwifiex_private *priv;
3555 	struct mwifiex_ds_wakeup_reason wakeup_reason;
3556 	struct cfg80211_wowlan_wakeup wakeup_report;
3557 	int i;
3558 	bool report_wakeup_reason = true;
3559 
3560 	for (i = 0; i < adapter->priv_num; i++) {
3561 		priv = adapter->priv[i];
3562 		if (priv && priv->netdev)
3563 			netif_device_attach(priv->netdev);
3564 	}
3565 
3566 	if (!wiphy->wowlan_config)
3567 		goto done;
3568 
3569 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3570 	mwifiex_get_wakeup_reason(priv, HostCmd_ACT_GEN_GET, MWIFIEX_SYNC_CMD,
3571 				  &wakeup_reason);
3572 	memset(&wakeup_report, 0, sizeof(struct cfg80211_wowlan_wakeup));
3573 
3574 	wakeup_report.pattern_idx = -1;
3575 
3576 	switch (wakeup_reason.hs_wakeup_reason) {
3577 	case NO_HSWAKEUP_REASON:
3578 		break;
3579 	case BCAST_DATA_MATCHED:
3580 		break;
3581 	case MCAST_DATA_MATCHED:
3582 		break;
3583 	case UCAST_DATA_MATCHED:
3584 		break;
3585 	case MASKTABLE_EVENT_MATCHED:
3586 		break;
3587 	case NON_MASKABLE_EVENT_MATCHED:
3588 		if (wiphy->wowlan_config->disconnect)
3589 			wakeup_report.disconnect = true;
3590 		if (wiphy->wowlan_config->nd_config)
3591 			wakeup_report.net_detect = adapter->nd_info;
3592 		break;
3593 	case NON_MASKABLE_CONDITION_MATCHED:
3594 		break;
3595 	case MAGIC_PATTERN_MATCHED:
3596 		if (wiphy->wowlan_config->magic_pkt)
3597 			wakeup_report.magic_pkt = true;
3598 		if (wiphy->wowlan_config->n_patterns)
3599 			wakeup_report.pattern_idx = 1;
3600 		break;
3601 	case GTK_REKEY_FAILURE:
3602 		if (wiphy->wowlan_config->gtk_rekey_failure)
3603 			wakeup_report.gtk_rekey_failure = true;
3604 		break;
3605 	default:
3606 		report_wakeup_reason = false;
3607 		break;
3608 	}
3609 
3610 	if (report_wakeup_reason)
3611 		cfg80211_report_wowlan_wakeup(&priv->wdev, &wakeup_report,
3612 					      GFP_KERNEL);
3613 
3614 done:
3615 	if (adapter->nd_info) {
3616 		for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
3617 			kfree(adapter->nd_info->matches[i]);
3618 		kfree(adapter->nd_info);
3619 		adapter->nd_info = NULL;
3620 	}
3621 
3622 	return 0;
3623 }
3624 
mwifiex_cfg80211_set_wakeup(struct wiphy * wiphy,bool enabled)3625 static void mwifiex_cfg80211_set_wakeup(struct wiphy *wiphy,
3626 				       bool enabled)
3627 {
3628 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3629 
3630 	device_set_wakeup_enable(adapter->dev, enabled);
3631 }
3632 
mwifiex_set_rekey_data(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_gtk_rekey_data * data)3633 static int mwifiex_set_rekey_data(struct wiphy *wiphy, struct net_device *dev,
3634 				  struct cfg80211_gtk_rekey_data *data)
3635 {
3636 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3637 
3638 	if (!ISSUPP_FIRMWARE_SUPPLICANT(priv->adapter->fw_cap_info))
3639 		return -EOPNOTSUPP;
3640 
3641 	return mwifiex_send_cmd(priv, HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG,
3642 				HostCmd_ACT_GEN_SET, 0, data, true);
3643 }
3644 
3645 #endif
3646 
mwifiex_get_coalesce_pkt_type(u8 * byte_seq)3647 static int mwifiex_get_coalesce_pkt_type(u8 *byte_seq)
3648 {
3649 	static const u8 ipv4_mc_mac[] = {0x33, 0x33};
3650 	static const u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
3651 	static const u8 bc_mac[] = {0xff, 0xff, 0xff, 0xff};
3652 
3653 	if ((byte_seq[0] & 0x01) &&
3654 	    (byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 1))
3655 		return PACKET_TYPE_UNICAST;
3656 	else if (!memcmp(byte_seq, bc_mac, 4))
3657 		return PACKET_TYPE_BROADCAST;
3658 	else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) &&
3659 		  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 2) ||
3660 		 (!memcmp(byte_seq, ipv6_mc_mac, 3) &&
3661 		  byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ] == 3))
3662 		return PACKET_TYPE_MULTICAST;
3663 
3664 	return 0;
3665 }
3666 
3667 static int
mwifiex_fill_coalesce_rule_info(struct mwifiex_private * priv,struct cfg80211_coalesce_rules * crule,struct mwifiex_coalesce_rule * mrule)3668 mwifiex_fill_coalesce_rule_info(struct mwifiex_private *priv,
3669 				struct cfg80211_coalesce_rules *crule,
3670 				struct mwifiex_coalesce_rule *mrule)
3671 {
3672 	u8 byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ + 1];
3673 	struct filt_field_param *param;
3674 	int i;
3675 
3676 	mrule->max_coalescing_delay = crule->delay;
3677 
3678 	param = mrule->params;
3679 
3680 	for (i = 0; i < crule->n_patterns; i++) {
3681 		memset(byte_seq, 0, sizeof(byte_seq));
3682 		if (!mwifiex_is_pattern_supported(&crule->patterns[i],
3683 						  byte_seq,
3684 						MWIFIEX_COALESCE_MAX_BYTESEQ)) {
3685 			mwifiex_dbg(priv->adapter, ERROR,
3686 				    "Pattern not supported\n");
3687 			return -EOPNOTSUPP;
3688 		}
3689 
3690 		if (!crule->patterns[i].pkt_offset) {
3691 			u8 pkt_type;
3692 
3693 			pkt_type = mwifiex_get_coalesce_pkt_type(byte_seq);
3694 			if (pkt_type && mrule->pkt_type) {
3695 				mwifiex_dbg(priv->adapter, ERROR,
3696 					    "Multiple packet types not allowed\n");
3697 				return -EOPNOTSUPP;
3698 			} else if (pkt_type) {
3699 				mrule->pkt_type = pkt_type;
3700 				continue;
3701 			}
3702 		}
3703 
3704 		if (crule->condition == NL80211_COALESCE_CONDITION_MATCH)
3705 			param->operation = RECV_FILTER_MATCH_TYPE_EQ;
3706 		else
3707 			param->operation = RECV_FILTER_MATCH_TYPE_NE;
3708 
3709 		param->operand_len = byte_seq[MWIFIEX_COALESCE_MAX_BYTESEQ];
3710 		memcpy(param->operand_byte_stream, byte_seq,
3711 		       param->operand_len);
3712 		param->offset = crule->patterns[i].pkt_offset;
3713 		param++;
3714 
3715 		mrule->num_of_fields++;
3716 	}
3717 
3718 	if (!mrule->pkt_type) {
3719 		mwifiex_dbg(priv->adapter, ERROR,
3720 			    "Packet type can not be determined\n");
3721 		return -EOPNOTSUPP;
3722 	}
3723 
3724 	return 0;
3725 }
3726 
mwifiex_cfg80211_set_coalesce(struct wiphy * wiphy,struct cfg80211_coalesce * coalesce)3727 static int mwifiex_cfg80211_set_coalesce(struct wiphy *wiphy,
3728 					 struct cfg80211_coalesce *coalesce)
3729 {
3730 	struct mwifiex_adapter *adapter = mwifiex_cfg80211_get_adapter(wiphy);
3731 	int i, ret;
3732 	struct mwifiex_ds_coalesce_cfg coalesce_cfg;
3733 	struct mwifiex_private *priv =
3734 			mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
3735 
3736 	memset(&coalesce_cfg, 0, sizeof(coalesce_cfg));
3737 	if (!coalesce) {
3738 		mwifiex_dbg(adapter, WARN,
3739 			    "Disable coalesce and reset all previous rules\n");
3740 		return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3741 					HostCmd_ACT_GEN_SET, 0,
3742 					&coalesce_cfg, true);
3743 	}
3744 
3745 	coalesce_cfg.num_of_rules = coalesce->n_rules;
3746 	for (i = 0; i < coalesce->n_rules; i++) {
3747 		ret = mwifiex_fill_coalesce_rule_info(priv, &coalesce->rules[i],
3748 						      &coalesce_cfg.rule[i]);
3749 		if (ret) {
3750 			mwifiex_dbg(adapter, ERROR,
3751 				    "Recheck the patterns provided for rule %d\n",
3752 				i + 1);
3753 			return ret;
3754 		}
3755 	}
3756 
3757 	return mwifiex_send_cmd(priv, HostCmd_CMD_COALESCE_CFG,
3758 				HostCmd_ACT_GEN_SET, 0, &coalesce_cfg, true);
3759 }
3760 
3761 /* cfg80211 ops handler for tdls_mgmt.
3762  * Function prepares TDLS action frame packets and forwards them to FW
3763  */
3764 static int
mwifiex_cfg80211_tdls_mgmt(struct wiphy * wiphy,struct net_device * dev,const u8 * peer,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capability,bool initiator,const u8 * extra_ies,size_t extra_ies_len)3765 mwifiex_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
3766 			   const u8 *peer, u8 action_code, u8 dialog_token,
3767 			   u16 status_code, u32 peer_capability,
3768 			   bool initiator, const u8 *extra_ies,
3769 			   size_t extra_ies_len)
3770 {
3771 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3772 	int ret;
3773 
3774 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
3775 		return -EOPNOTSUPP;
3776 
3777 	/* make sure we are in station mode and connected */
3778 	if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3779 		return -EOPNOTSUPP;
3780 
3781 	switch (action_code) {
3782 	case WLAN_TDLS_SETUP_REQUEST:
3783 		mwifiex_dbg(priv->adapter, MSG,
3784 			    "Send TDLS Setup Request to %pM status_code=%d\n",
3785 			    peer, status_code);
3786 		mwifiex_add_auto_tdls_peer(priv, peer);
3787 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3788 						   dialog_token, status_code,
3789 						   extra_ies, extra_ies_len);
3790 		break;
3791 	case WLAN_TDLS_SETUP_RESPONSE:
3792 		mwifiex_add_auto_tdls_peer(priv, peer);
3793 		mwifiex_dbg(priv->adapter, MSG,
3794 			    "Send TDLS Setup Response to %pM status_code=%d\n",
3795 			    peer, status_code);
3796 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3797 						   dialog_token, status_code,
3798 						   extra_ies, extra_ies_len);
3799 		break;
3800 	case WLAN_TDLS_SETUP_CONFIRM:
3801 		mwifiex_dbg(priv->adapter, MSG,
3802 			    "Send TDLS Confirm to %pM status_code=%d\n", peer,
3803 			    status_code);
3804 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3805 						   dialog_token, status_code,
3806 						   extra_ies, extra_ies_len);
3807 		break;
3808 	case WLAN_TDLS_TEARDOWN:
3809 		mwifiex_dbg(priv->adapter, MSG,
3810 			    "Send TDLS Tear down to %pM\n", peer);
3811 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3812 						   dialog_token, status_code,
3813 						   extra_ies, extra_ies_len);
3814 		break;
3815 	case WLAN_TDLS_DISCOVERY_REQUEST:
3816 		mwifiex_dbg(priv->adapter, MSG,
3817 			    "Send TDLS Discovery Request to %pM\n", peer);
3818 		ret = mwifiex_send_tdls_data_frame(priv, peer, action_code,
3819 						   dialog_token, status_code,
3820 						   extra_ies, extra_ies_len);
3821 		break;
3822 	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
3823 		mwifiex_dbg(priv->adapter, MSG,
3824 			    "Send TDLS Discovery Response to %pM\n", peer);
3825 		ret = mwifiex_send_tdls_action_frame(priv, peer, action_code,
3826 						   dialog_token, status_code,
3827 						   extra_ies, extra_ies_len);
3828 		break;
3829 	default:
3830 		mwifiex_dbg(priv->adapter, ERROR,
3831 			    "Unknown TDLS mgmt/action frame %pM\n", peer);
3832 		ret = -EINVAL;
3833 		break;
3834 	}
3835 
3836 	return ret;
3837 }
3838 
3839 static int
mwifiex_cfg80211_tdls_oper(struct wiphy * wiphy,struct net_device * dev,const u8 * peer,enum nl80211_tdls_operation action)3840 mwifiex_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3841 			   const u8 *peer, enum nl80211_tdls_operation action)
3842 {
3843 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3844 
3845 	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
3846 	    !(wiphy->flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3847 		return -EOPNOTSUPP;
3848 
3849 	/* make sure we are in station mode and connected */
3850 	if (!(priv->bss_type == MWIFIEX_BSS_TYPE_STA && priv->media_connected))
3851 		return -EOPNOTSUPP;
3852 
3853 	mwifiex_dbg(priv->adapter, MSG,
3854 		    "TDLS peer=%pM, oper=%d\n", peer, action);
3855 
3856 	switch (action) {
3857 	case NL80211_TDLS_ENABLE_LINK:
3858 		action = MWIFIEX_TDLS_ENABLE_LINK;
3859 		break;
3860 	case NL80211_TDLS_DISABLE_LINK:
3861 		action = MWIFIEX_TDLS_DISABLE_LINK;
3862 		break;
3863 	case NL80211_TDLS_TEARDOWN:
3864 		/* shouldn't happen!*/
3865 		mwifiex_dbg(priv->adapter, ERROR,
3866 			    "tdls_oper: teardown from driver not supported\n");
3867 		return -EINVAL;
3868 	case NL80211_TDLS_SETUP:
3869 		/* shouldn't happen!*/
3870 		mwifiex_dbg(priv->adapter, ERROR,
3871 			    "tdls_oper: setup from driver not supported\n");
3872 		return -EINVAL;
3873 	case NL80211_TDLS_DISCOVERY_REQ:
3874 		/* shouldn't happen!*/
3875 		mwifiex_dbg(priv->adapter, ERROR,
3876 			    "tdls_oper: discovery from driver not supported\n");
3877 		return -EINVAL;
3878 	default:
3879 		mwifiex_dbg(priv->adapter, ERROR,
3880 			    "tdls_oper: operation not supported\n");
3881 		return -EOPNOTSUPP;
3882 	}
3883 
3884 	return mwifiex_tdls_oper(priv, peer, action);
3885 }
3886 
3887 static int
mwifiex_cfg80211_tdls_chan_switch(struct wiphy * wiphy,struct net_device * dev,const u8 * addr,u8 oper_class,struct cfg80211_chan_def * chandef)3888 mwifiex_cfg80211_tdls_chan_switch(struct wiphy *wiphy, struct net_device *dev,
3889 				  const u8 *addr, u8 oper_class,
3890 				  struct cfg80211_chan_def *chandef)
3891 {
3892 	struct mwifiex_sta_node *sta_ptr;
3893 	u16 chan;
3894 	u8 second_chan_offset, band;
3895 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3896 
3897 	spin_lock_bh(&priv->sta_list_spinlock);
3898 	sta_ptr = mwifiex_get_sta_entry(priv, addr);
3899 	if (!sta_ptr) {
3900 		spin_unlock_bh(&priv->sta_list_spinlock);
3901 		wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3902 			  __func__, addr);
3903 		return -ENOENT;
3904 	}
3905 
3906 	if (!(sta_ptr->tdls_cap.extcap.ext_capab[3] &
3907 	      WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH)) {
3908 		spin_unlock_bh(&priv->sta_list_spinlock);
3909 		wiphy_err(wiphy, "%pM do not support tdls cs\n", addr);
3910 		return -ENOENT;
3911 	}
3912 
3913 	if (sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3914 	    sta_ptr->tdls_status == TDLS_IN_OFF_CHAN) {
3915 		spin_unlock_bh(&priv->sta_list_spinlock);
3916 		wiphy_err(wiphy, "channel switch is running, abort request\n");
3917 		return -EALREADY;
3918 	}
3919 	spin_unlock_bh(&priv->sta_list_spinlock);
3920 
3921 	chan = chandef->chan->hw_value;
3922 	second_chan_offset = mwifiex_get_sec_chan_offset(chan);
3923 	band = chandef->chan->band;
3924 	mwifiex_start_tdls_cs(priv, addr, chan, second_chan_offset, band);
3925 
3926 	return 0;
3927 }
3928 
3929 static void
mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy * wiphy,struct net_device * dev,const u8 * addr)3930 mwifiex_cfg80211_tdls_cancel_chan_switch(struct wiphy *wiphy,
3931 					 struct net_device *dev,
3932 					 const u8 *addr)
3933 {
3934 	struct mwifiex_sta_node *sta_ptr;
3935 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3936 
3937 	spin_lock_bh(&priv->sta_list_spinlock);
3938 	sta_ptr = mwifiex_get_sta_entry(priv, addr);
3939 	if (!sta_ptr) {
3940 		spin_unlock_bh(&priv->sta_list_spinlock);
3941 		wiphy_err(wiphy, "%s: Invalid TDLS peer %pM\n",
3942 			  __func__, addr);
3943 	} else if (!(sta_ptr->tdls_status == TDLS_CHAN_SWITCHING ||
3944 		     sta_ptr->tdls_status == TDLS_IN_BASE_CHAN ||
3945 		     sta_ptr->tdls_status == TDLS_IN_OFF_CHAN)) {
3946 		spin_unlock_bh(&priv->sta_list_spinlock);
3947 		wiphy_err(wiphy, "tdls chan switch not initialize by %pM\n",
3948 			  addr);
3949 	} else {
3950 		spin_unlock_bh(&priv->sta_list_spinlock);
3951 		mwifiex_stop_tdls_cs(priv, addr);
3952 	}
3953 }
3954 
3955 static int
mwifiex_cfg80211_add_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)3956 mwifiex_cfg80211_add_station(struct wiphy *wiphy, struct net_device *dev,
3957 			     const u8 *mac, struct station_parameters *params)
3958 {
3959 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3960 
3961 	if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3962 		return -EOPNOTSUPP;
3963 
3964 	/* make sure we are in station mode and connected */
3965 	if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
3966 		return -EOPNOTSUPP;
3967 
3968 	return mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CREATE_LINK);
3969 }
3970 
3971 static int
mwifiex_cfg80211_channel_switch(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_csa_settings * params)3972 mwifiex_cfg80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
3973 				struct cfg80211_csa_settings *params)
3974 {
3975 	struct ieee_types_header *chsw_ie;
3976 	struct ieee80211_channel_sw_ie *channel_sw;
3977 	int chsw_msec;
3978 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
3979 
3980 	if (priv->adapter->scan_processing) {
3981 		mwifiex_dbg(priv->adapter, ERROR,
3982 			    "radar detection: scan in process...\n");
3983 		return -EBUSY;
3984 	}
3985 
3986 	if (priv->wdev.cac_started)
3987 		return -EBUSY;
3988 
3989 	if (cfg80211_chandef_identical(&params->chandef,
3990 				       &priv->dfs_chandef))
3991 		return -EINVAL;
3992 
3993 	chsw_ie = (void *)cfg80211_find_ie(WLAN_EID_CHANNEL_SWITCH,
3994 					   params->beacon_csa.tail,
3995 					   params->beacon_csa.tail_len);
3996 	if (!chsw_ie) {
3997 		mwifiex_dbg(priv->adapter, ERROR,
3998 			    "Could not parse channel switch announcement IE\n");
3999 		return -EINVAL;
4000 	}
4001 
4002 	channel_sw = (void *)(chsw_ie + 1);
4003 	if (channel_sw->mode) {
4004 		if (netif_carrier_ok(priv->netdev))
4005 			netif_carrier_off(priv->netdev);
4006 		mwifiex_stop_net_dev_queue(priv->netdev, priv->adapter);
4007 	}
4008 
4009 	if (mwifiex_del_mgmt_ies(priv))
4010 		mwifiex_dbg(priv->adapter, ERROR,
4011 			    "Failed to delete mgmt IEs!\n");
4012 
4013 	if (mwifiex_set_mgmt_ies(priv, &params->beacon_csa)) {
4014 		mwifiex_dbg(priv->adapter, ERROR,
4015 			    "%s: setting mgmt ies failed\n", __func__);
4016 		return -EFAULT;
4017 	}
4018 
4019 	memcpy(&priv->dfs_chandef, &params->chandef, sizeof(priv->dfs_chandef));
4020 	memcpy(&priv->beacon_after, &params->beacon_after,
4021 	       sizeof(priv->beacon_after));
4022 
4023 	chsw_msec = max(channel_sw->count * priv->bss_cfg.beacon_period, 100);
4024 	queue_delayed_work(priv->dfs_chan_sw_workqueue, &priv->dfs_chan_sw_work,
4025 			   msecs_to_jiffies(chsw_msec));
4026 	return 0;
4027 }
4028 
mwifiex_cfg80211_get_channel(struct wiphy * wiphy,struct wireless_dev * wdev,unsigned int link_id,struct cfg80211_chan_def * chandef)4029 static int mwifiex_cfg80211_get_channel(struct wiphy *wiphy,
4030 					struct wireless_dev *wdev,
4031 					unsigned int link_id,
4032 					struct cfg80211_chan_def *chandef)
4033 {
4034 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
4035 	struct mwifiex_bssdescriptor *curr_bss;
4036 	struct ieee80211_channel *chan;
4037 	enum nl80211_channel_type chan_type;
4038 	enum nl80211_band band;
4039 	int freq;
4040 	int ret = -ENODATA;
4041 
4042 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP &&
4043 	    cfg80211_chandef_valid(&priv->bss_chandef)) {
4044 		*chandef = priv->bss_chandef;
4045 		ret = 0;
4046 	} else if (priv->media_connected) {
4047 		curr_bss = &priv->curr_bss_params.bss_descriptor;
4048 		band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
4049 		freq = ieee80211_channel_to_frequency(curr_bss->channel, band);
4050 		chan = ieee80211_get_channel(wiphy, freq);
4051 
4052 		if (priv->ht_param_present) {
4053 			chan_type = mwifiex_get_chan_type(priv);
4054 			cfg80211_chandef_create(chandef, chan, chan_type);
4055 		} else {
4056 			cfg80211_chandef_create(chandef, chan,
4057 						NL80211_CHAN_NO_HT);
4058 		}
4059 		ret = 0;
4060 	}
4061 
4062 	return ret;
4063 }
4064 
4065 #ifdef CONFIG_NL80211_TESTMODE
4066 
4067 enum mwifiex_tm_attr {
4068 	__MWIFIEX_TM_ATTR_INVALID	= 0,
4069 	MWIFIEX_TM_ATTR_CMD		= 1,
4070 	MWIFIEX_TM_ATTR_DATA		= 2,
4071 
4072 	/* keep last */
4073 	__MWIFIEX_TM_ATTR_AFTER_LAST,
4074 	MWIFIEX_TM_ATTR_MAX		= __MWIFIEX_TM_ATTR_AFTER_LAST - 1,
4075 };
4076 
4077 static const struct nla_policy mwifiex_tm_policy[MWIFIEX_TM_ATTR_MAX + 1] = {
4078 	[MWIFIEX_TM_ATTR_CMD]		= { .type = NLA_U32 },
4079 	[MWIFIEX_TM_ATTR_DATA]		= { .type = NLA_BINARY,
4080 					    .len = MWIFIEX_SIZE_OF_CMD_BUFFER },
4081 };
4082 
4083 enum mwifiex_tm_command {
4084 	MWIFIEX_TM_CMD_HOSTCMD	= 0,
4085 };
4086 
mwifiex_tm_cmd(struct wiphy * wiphy,struct wireless_dev * wdev,void * data,int len)4087 static int mwifiex_tm_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
4088 			  void *data, int len)
4089 {
4090 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(wdev->netdev);
4091 	struct mwifiex_ds_misc_cmd *hostcmd;
4092 	struct nlattr *tb[MWIFIEX_TM_ATTR_MAX + 1];
4093 	struct sk_buff *skb;
4094 	int err;
4095 
4096 	if (!priv)
4097 		return -EINVAL;
4098 
4099 	err = nla_parse_deprecated(tb, MWIFIEX_TM_ATTR_MAX, data, len,
4100 				   mwifiex_tm_policy, NULL);
4101 	if (err)
4102 		return err;
4103 
4104 	if (!tb[MWIFIEX_TM_ATTR_CMD])
4105 		return -EINVAL;
4106 
4107 	switch (nla_get_u32(tb[MWIFIEX_TM_ATTR_CMD])) {
4108 	case MWIFIEX_TM_CMD_HOSTCMD:
4109 		if (!tb[MWIFIEX_TM_ATTR_DATA])
4110 			return -EINVAL;
4111 
4112 		hostcmd = kzalloc(sizeof(*hostcmd), GFP_KERNEL);
4113 		if (!hostcmd)
4114 			return -ENOMEM;
4115 
4116 		hostcmd->len = nla_len(tb[MWIFIEX_TM_ATTR_DATA]);
4117 		memcpy(hostcmd->cmd, nla_data(tb[MWIFIEX_TM_ATTR_DATA]),
4118 		       hostcmd->len);
4119 
4120 		if (mwifiex_send_cmd(priv, 0, 0, 0, hostcmd, true)) {
4121 			dev_err(priv->adapter->dev, "Failed to process hostcmd\n");
4122 			kfree(hostcmd);
4123 			return -EFAULT;
4124 		}
4125 
4126 		/* process hostcmd response*/
4127 		skb = cfg80211_testmode_alloc_reply_skb(wiphy, hostcmd->len);
4128 		if (!skb) {
4129 			kfree(hostcmd);
4130 			return -ENOMEM;
4131 		}
4132 		err = nla_put(skb, MWIFIEX_TM_ATTR_DATA,
4133 			      hostcmd->len, hostcmd->cmd);
4134 		if (err) {
4135 			kfree(hostcmd);
4136 			kfree_skb(skb);
4137 			return -EMSGSIZE;
4138 		}
4139 
4140 		err = cfg80211_testmode_reply(skb);
4141 		kfree(hostcmd);
4142 		return err;
4143 	default:
4144 		return -EOPNOTSUPP;
4145 	}
4146 }
4147 #endif
4148 
4149 static int
mwifiex_cfg80211_start_radar_detection(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_chan_def * chandef,u32 cac_time_ms)4150 mwifiex_cfg80211_start_radar_detection(struct wiphy *wiphy,
4151 				       struct net_device *dev,
4152 				       struct cfg80211_chan_def *chandef,
4153 				       u32 cac_time_ms)
4154 {
4155 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4156 	struct mwifiex_radar_params radar_params;
4157 
4158 	if (priv->adapter->scan_processing) {
4159 		mwifiex_dbg(priv->adapter, ERROR,
4160 			    "radar detection: scan already in process...\n");
4161 		return -EBUSY;
4162 	}
4163 
4164 	if (!mwifiex_is_11h_active(priv)) {
4165 		mwifiex_dbg(priv->adapter, INFO,
4166 			    "Enable 11h extensions in FW\n");
4167 		if (mwifiex_11h_activate(priv, true)) {
4168 			mwifiex_dbg(priv->adapter, ERROR,
4169 				    "Failed to activate 11h extensions!!");
4170 			return -1;
4171 		}
4172 		priv->state_11h.is_11h_active = true;
4173 	}
4174 
4175 	memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
4176 	radar_params.chandef = chandef;
4177 	radar_params.cac_time_ms = cac_time_ms;
4178 
4179 	memcpy(&priv->dfs_chandef, chandef, sizeof(priv->dfs_chandef));
4180 
4181 	if (mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
4182 			     HostCmd_ACT_GEN_SET, 0, &radar_params, true))
4183 		return -1;
4184 
4185 	queue_delayed_work(priv->dfs_cac_workqueue, &priv->dfs_cac_work,
4186 			   msecs_to_jiffies(cac_time_ms));
4187 	return 0;
4188 }
4189 
4190 static int
mwifiex_cfg80211_change_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_parameters * params)4191 mwifiex_cfg80211_change_station(struct wiphy *wiphy, struct net_device *dev,
4192 				const u8 *mac,
4193 				struct station_parameters *params)
4194 {
4195 	int ret;
4196 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
4197 
4198 	/* we support change_station handler only for TDLS peers*/
4199 	if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4200 		return -EOPNOTSUPP;
4201 
4202 	/* make sure we are in station mode and connected */
4203 	if ((priv->bss_type != MWIFIEX_BSS_TYPE_STA) || !priv->media_connected)
4204 		return -EOPNOTSUPP;
4205 
4206 	priv->sta_params = params;
4207 
4208 	ret = mwifiex_tdls_oper(priv, mac, MWIFIEX_TDLS_CONFIG_LINK);
4209 	priv->sta_params = NULL;
4210 
4211 	return ret;
4212 }
4213 
4214 /* station cfg80211 operations */
4215 static struct cfg80211_ops mwifiex_cfg80211_ops = {
4216 	.add_virtual_intf = mwifiex_add_virtual_intf,
4217 	.del_virtual_intf = mwifiex_del_virtual_intf,
4218 	.change_virtual_intf = mwifiex_cfg80211_change_virtual_intf,
4219 	.scan = mwifiex_cfg80211_scan,
4220 	.connect = mwifiex_cfg80211_connect,
4221 	.disconnect = mwifiex_cfg80211_disconnect,
4222 	.get_station = mwifiex_cfg80211_get_station,
4223 	.dump_station = mwifiex_cfg80211_dump_station,
4224 	.dump_survey = mwifiex_cfg80211_dump_survey,
4225 	.set_wiphy_params = mwifiex_cfg80211_set_wiphy_params,
4226 	.join_ibss = mwifiex_cfg80211_join_ibss,
4227 	.leave_ibss = mwifiex_cfg80211_leave_ibss,
4228 	.add_key = mwifiex_cfg80211_add_key,
4229 	.del_key = mwifiex_cfg80211_del_key,
4230 	.set_default_mgmt_key = mwifiex_cfg80211_set_default_mgmt_key,
4231 	.mgmt_tx = mwifiex_cfg80211_mgmt_tx,
4232 	.update_mgmt_frame_registrations =
4233 		mwifiex_cfg80211_update_mgmt_frame_registrations,
4234 	.remain_on_channel = mwifiex_cfg80211_remain_on_channel,
4235 	.cancel_remain_on_channel = mwifiex_cfg80211_cancel_remain_on_channel,
4236 	.set_default_key = mwifiex_cfg80211_set_default_key,
4237 	.set_power_mgmt = mwifiex_cfg80211_set_power_mgmt,
4238 	.set_tx_power = mwifiex_cfg80211_set_tx_power,
4239 	.get_tx_power = mwifiex_cfg80211_get_tx_power,
4240 	.set_bitrate_mask = mwifiex_cfg80211_set_bitrate_mask,
4241 	.start_ap = mwifiex_cfg80211_start_ap,
4242 	.stop_ap = mwifiex_cfg80211_stop_ap,
4243 	.change_beacon = mwifiex_cfg80211_change_beacon,
4244 	.set_cqm_rssi_config = mwifiex_cfg80211_set_cqm_rssi_config,
4245 	.set_antenna = mwifiex_cfg80211_set_antenna,
4246 	.get_antenna = mwifiex_cfg80211_get_antenna,
4247 	.del_station = mwifiex_cfg80211_del_station,
4248 	.sched_scan_start = mwifiex_cfg80211_sched_scan_start,
4249 	.sched_scan_stop = mwifiex_cfg80211_sched_scan_stop,
4250 #ifdef CONFIG_PM
4251 	.suspend = mwifiex_cfg80211_suspend,
4252 	.resume = mwifiex_cfg80211_resume,
4253 	.set_wakeup = mwifiex_cfg80211_set_wakeup,
4254 	.set_rekey_data = mwifiex_set_rekey_data,
4255 #endif
4256 	.set_coalesce = mwifiex_cfg80211_set_coalesce,
4257 	.tdls_mgmt = mwifiex_cfg80211_tdls_mgmt,
4258 	.tdls_oper = mwifiex_cfg80211_tdls_oper,
4259 	.tdls_channel_switch = mwifiex_cfg80211_tdls_chan_switch,
4260 	.tdls_cancel_channel_switch = mwifiex_cfg80211_tdls_cancel_chan_switch,
4261 	.add_station = mwifiex_cfg80211_add_station,
4262 	.change_station = mwifiex_cfg80211_change_station,
4263 	CFG80211_TESTMODE_CMD(mwifiex_tm_cmd)
4264 	.get_channel = mwifiex_cfg80211_get_channel,
4265 	.start_radar_detection = mwifiex_cfg80211_start_radar_detection,
4266 	.channel_switch = mwifiex_cfg80211_channel_switch,
4267 };
4268 
4269 #ifdef CONFIG_PM
4270 static const struct wiphy_wowlan_support mwifiex_wowlan_support = {
4271 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4272 		WIPHY_WOWLAN_NET_DETECT | WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
4273 		WIPHY_WOWLAN_GTK_REKEY_FAILURE,
4274 	.n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4275 	.pattern_min_len = 1,
4276 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4277 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4278 	.max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4279 };
4280 
4281 static const struct wiphy_wowlan_support mwifiex_wowlan_support_no_gtk = {
4282 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
4283 		 WIPHY_WOWLAN_NET_DETECT,
4284 	.n_patterns = MWIFIEX_MEF_MAX_FILTERS,
4285 	.pattern_min_len = 1,
4286 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4287 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4288 	.max_nd_match_sets = MWIFIEX_MAX_ND_MATCH_SETS,
4289 };
4290 #endif
4291 
mwifiex_is_valid_alpha2(const char * alpha2)4292 static bool mwifiex_is_valid_alpha2(const char *alpha2)
4293 {
4294 	if (!alpha2 || strlen(alpha2) != 2)
4295 		return false;
4296 
4297 	if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
4298 		return true;
4299 
4300 	return false;
4301 }
4302 
4303 static const struct wiphy_coalesce_support mwifiex_coalesce_support = {
4304 	.n_rules = MWIFIEX_COALESCE_MAX_RULES,
4305 	.max_delay = MWIFIEX_MAX_COALESCING_DELAY,
4306 	.n_patterns = MWIFIEX_COALESCE_MAX_FILTERS,
4307 	.pattern_min_len = 1,
4308 	.pattern_max_len = MWIFIEX_MAX_PATTERN_LEN,
4309 	.max_pkt_offset = MWIFIEX_MAX_OFFSET_LEN,
4310 };
4311 
mwifiex_init_channel_scan_gap(struct mwifiex_adapter * adapter)4312 int mwifiex_init_channel_scan_gap(struct mwifiex_adapter *adapter)
4313 {
4314 	u32 n_channels_bg, n_channels_a = 0;
4315 
4316 	n_channels_bg = mwifiex_band_2ghz.n_channels;
4317 
4318 	if (adapter->config_bands & BAND_A)
4319 		n_channels_a = mwifiex_band_5ghz.n_channels;
4320 
4321 	/* allocate twice the number total channels, since the driver issues an
4322 	 * additional active scan request for hidden SSIDs on passive channels.
4323 	 */
4324 	adapter->num_in_chan_stats = 2 * (n_channels_bg + n_channels_a);
4325 	adapter->chan_stats = vmalloc(array_size(sizeof(*adapter->chan_stats),
4326 						 adapter->num_in_chan_stats));
4327 
4328 	if (!adapter->chan_stats)
4329 		return -ENOMEM;
4330 
4331 	return 0;
4332 }
4333 
4334 /*
4335  * This function registers the device with CFG802.11 subsystem.
4336  *
4337  * The function creates the wireless device/wiphy, populates it with
4338  * default parameters and handler function pointers, and finally
4339  * registers the device.
4340  */
4341 
mwifiex_register_cfg80211(struct mwifiex_adapter * adapter)4342 int mwifiex_register_cfg80211(struct mwifiex_adapter *adapter)
4343 {
4344 	int ret;
4345 	void *wdev_priv;
4346 	struct wiphy *wiphy;
4347 	struct mwifiex_private *priv = adapter->priv[MWIFIEX_BSS_TYPE_STA];
4348 	u8 *country_code;
4349 	u32 thr, retry;
4350 
4351 	/* create a new wiphy for use with cfg80211 */
4352 	wiphy = wiphy_new(&mwifiex_cfg80211_ops,
4353 			  sizeof(struct mwifiex_adapter *));
4354 	if (!wiphy) {
4355 		mwifiex_dbg(adapter, ERROR,
4356 			    "%s: creating new wiphy\n", __func__);
4357 		return -ENOMEM;
4358 	}
4359 	wiphy->max_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4360 	wiphy->max_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4361 	wiphy->mgmt_stypes = mwifiex_mgmt_stypes;
4362 	wiphy->max_remain_on_channel_duration = 5000;
4363 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4364 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
4365 				 BIT(NL80211_IFTYPE_P2P_GO) |
4366 				 BIT(NL80211_IFTYPE_AP);
4367 
4368 	if (ISSUPP_ADHOC_ENABLED(adapter->fw_cap_info))
4369 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
4370 
4371 	wiphy->bands[NL80211_BAND_2GHZ] = &mwifiex_band_2ghz;
4372 	if (adapter->config_bands & BAND_A)
4373 		wiphy->bands[NL80211_BAND_5GHZ] = &mwifiex_band_5ghz;
4374 	else
4375 		wiphy->bands[NL80211_BAND_5GHZ] = NULL;
4376 
4377 	if (adapter->drcs_enabled && ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
4378 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_drcs;
4379 	else if (adapter->is_hw_11ac_capable)
4380 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta_vht;
4381 	else
4382 		wiphy->iface_combinations = &mwifiex_iface_comb_ap_sta;
4383 	wiphy->n_iface_combinations = 1;
4384 
4385 	if (adapter->max_sta_conn > adapter->max_p2p_conn)
4386 		wiphy->max_ap_assoc_sta = adapter->max_sta_conn;
4387 	else
4388 		wiphy->max_ap_assoc_sta = adapter->max_p2p_conn;
4389 
4390 	/* Initialize cipher suits */
4391 	wiphy->cipher_suites = mwifiex_cipher_suites;
4392 	wiphy->n_cipher_suites = ARRAY_SIZE(mwifiex_cipher_suites);
4393 
4394 	if (adapter->regd) {
4395 		wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
4396 					   REGULATORY_DISABLE_BEACON_HINTS |
4397 					   REGULATORY_COUNTRY_IE_IGNORE;
4398 		wiphy_apply_custom_regulatory(wiphy, adapter->regd);
4399 	}
4400 
4401 	ether_addr_copy(wiphy->perm_addr, adapter->perm_addr);
4402 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
4403 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
4404 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
4405 			WIPHY_FLAG_AP_UAPSD |
4406 			WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
4407 			WIPHY_FLAG_HAS_CHANNEL_SWITCH |
4408 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
4409 
4410 	if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4411 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
4412 				WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
4413 
4414 #ifdef CONFIG_PM
4415 	if (ISSUPP_FIRMWARE_SUPPLICANT(priv->adapter->fw_cap_info))
4416 		wiphy->wowlan = &mwifiex_wowlan_support;
4417 	else
4418 		wiphy->wowlan = &mwifiex_wowlan_support_no_gtk;
4419 #endif
4420 
4421 	wiphy->coalesce = &mwifiex_coalesce_support;
4422 
4423 	wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
4424 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
4425 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
4426 
4427 	wiphy->max_sched_scan_reqs = 1;
4428 	wiphy->max_sched_scan_ssids = MWIFIEX_MAX_SSID_LIST_LENGTH;
4429 	wiphy->max_sched_scan_ie_len = MWIFIEX_MAX_VSIE_LEN;
4430 	wiphy->max_match_sets = MWIFIEX_MAX_SSID_LIST_LENGTH;
4431 
4432 	wiphy->available_antennas_tx = BIT(adapter->number_of_antenna) - 1;
4433 	wiphy->available_antennas_rx = BIT(adapter->number_of_antenna) - 1;
4434 
4435 	wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER |
4436 			   NL80211_FEATURE_LOW_PRIORITY_SCAN |
4437 			   NL80211_FEATURE_NEED_OBSS_SCAN;
4438 
4439 	if (ISSUPP_ADHOC_ENABLED(adapter->fw_cap_info))
4440 		wiphy->features |= NL80211_FEATURE_HT_IBSS;
4441 
4442 	if (ISSUPP_RANDOM_MAC(adapter->fw_cap_info))
4443 		wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
4444 				   NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
4445 				   NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
4446 
4447 	if (ISSUPP_TDLS_ENABLED(adapter->fw_cap_info))
4448 		wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
4449 
4450 	if (adapter->fw_api_ver == MWIFIEX_FW_V15)
4451 		wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
4452 
4453 	/* Reserve space for mwifiex specific private data for BSS */
4454 	wiphy->bss_priv_size = sizeof(struct mwifiex_bss_priv);
4455 
4456 	wiphy->reg_notifier = mwifiex_reg_notifier;
4457 
4458 	/* Set struct mwifiex_adapter pointer in wiphy_priv */
4459 	wdev_priv = wiphy_priv(wiphy);
4460 	*(unsigned long *)wdev_priv = (unsigned long)adapter;
4461 
4462 	set_wiphy_dev(wiphy, priv->adapter->dev);
4463 
4464 	ret = wiphy_register(wiphy);
4465 	if (ret < 0) {
4466 		mwifiex_dbg(adapter, ERROR,
4467 			    "%s: wiphy_register failed: %d\n", __func__, ret);
4468 		wiphy_free(wiphy);
4469 		return ret;
4470 	}
4471 
4472 	if (!adapter->regd) {
4473 		if (reg_alpha2 && mwifiex_is_valid_alpha2(reg_alpha2)) {
4474 			mwifiex_dbg(adapter, INFO,
4475 				    "driver hint alpha2: %2.2s\n", reg_alpha2);
4476 			regulatory_hint(wiphy, reg_alpha2);
4477 		} else {
4478 			if (adapter->region_code == 0x00) {
4479 				mwifiex_dbg(adapter, WARN,
4480 					    "Ignore world regulatory domain\n");
4481 			} else {
4482 				wiphy->regulatory_flags |=
4483 					REGULATORY_DISABLE_BEACON_HINTS |
4484 					REGULATORY_COUNTRY_IE_IGNORE;
4485 				country_code =
4486 					mwifiex_11d_code_2_region(
4487 						adapter->region_code);
4488 				if (country_code &&
4489 				    regulatory_hint(wiphy, country_code))
4490 					mwifiex_dbg(priv->adapter, ERROR,
4491 						    "regulatory_hint() failed\n");
4492 			}
4493 		}
4494 	}
4495 
4496 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4497 			 HostCmd_ACT_GEN_GET, FRAG_THRESH_I, &thr, true);
4498 	wiphy->frag_threshold = thr;
4499 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4500 			 HostCmd_ACT_GEN_GET, RTS_THRESH_I, &thr, true);
4501 	wiphy->rts_threshold = thr;
4502 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4503 			 HostCmd_ACT_GEN_GET, SHORT_RETRY_LIM_I, &retry, true);
4504 	wiphy->retry_short = (u8) retry;
4505 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
4506 			 HostCmd_ACT_GEN_GET, LONG_RETRY_LIM_I, &retry, true);
4507 	wiphy->retry_long = (u8) retry;
4508 
4509 	adapter->wiphy = wiphy;
4510 	return ret;
4511 }
4512