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