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