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