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