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