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