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