1 /*
2  * Marvell Wireless LAN device driver: functions for station ioctl
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27 #include "cfg80211.h"
28 
29 /*
30  * Copies the multicast address list from device to driver.
31  *
32  * This function does not validate the destination memory for
33  * size, and the calling function must ensure enough memory is
34  * available.
35  */
mwifiex_copy_mcast_addr(struct mwifiex_multicast_list * mlist,struct net_device * dev)36 int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
37 			    struct net_device *dev)
38 {
39 	int i = 0;
40 	struct netdev_hw_addr *ha;
41 
42 	netdev_for_each_mc_addr(ha, dev)
43 		memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);
44 
45 	return i;
46 }
47 
48 /*
49  * Wait queue completion handler.
50  *
51  * This function waits on a cmd wait queue. It also cancels the pending
52  * request after waking up, in case of errors.
53  */
mwifiex_wait_queue_complete(struct mwifiex_adapter * adapter)54 int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter)
55 {
56 	int status;
57 	struct cmd_ctrl_node *cmd_queued;
58 
59 	if (!adapter->cmd_queued)
60 		return 0;
61 
62 	cmd_queued = adapter->cmd_queued;
63 	adapter->cmd_queued = NULL;
64 
65 	dev_dbg(adapter->dev, "cmd pending\n");
66 	atomic_inc(&adapter->cmd_pending);
67 
68 	/* Status pending, wake up main process */
69 	queue_work(adapter->workqueue, &adapter->main_work);
70 
71 	/* Wait for completion */
72 	status = wait_event_interruptible(adapter->cmd_wait_q.wait,
73 					  *(cmd_queued->condition));
74 	if (status) {
75 		dev_err(adapter->dev, "cmd_wait_q terminated: %d\n", status);
76 		return status;
77 	}
78 
79 	status = adapter->cmd_wait_q.status;
80 	adapter->cmd_wait_q.status = 0;
81 
82 	return status;
83 }
84 
85 /*
86  * This function prepares the correct firmware command and
87  * issues it to set the multicast list.
88  *
89  * This function can be used to enable promiscuous mode, or enable all
90  * multicast packets, or to enable selective multicast.
91  */
mwifiex_request_set_multicast_list(struct mwifiex_private * priv,struct mwifiex_multicast_list * mcast_list)92 int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
93 				struct mwifiex_multicast_list *mcast_list)
94 {
95 	int ret = 0;
96 	u16 old_pkt_filter;
97 
98 	old_pkt_filter = priv->curr_pkt_filter;
99 
100 	if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
101 		dev_dbg(priv->adapter->dev, "info: Enable Promiscuous mode\n");
102 		priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
103 		priv->curr_pkt_filter &=
104 			~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
105 	} else {
106 		/* Multicast */
107 		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
108 		if (mcast_list->mode == MWIFIEX_ALL_MULTI_MODE) {
109 			dev_dbg(priv->adapter->dev,
110 				"info: Enabling All Multicast!\n");
111 			priv->curr_pkt_filter |=
112 				HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
113 		} else {
114 			priv->curr_pkt_filter &=
115 				~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
116 			if (mcast_list->num_multicast_addr) {
117 				dev_dbg(priv->adapter->dev,
118 					"info: Set multicast list=%d\n",
119 				       mcast_list->num_multicast_addr);
120 				/* Send multicast addresses to firmware */
121 				ret = mwifiex_send_cmd_async(priv,
122 					HostCmd_CMD_MAC_MULTICAST_ADR,
123 					HostCmd_ACT_GEN_SET, 0,
124 					mcast_list);
125 			}
126 		}
127 	}
128 	dev_dbg(priv->adapter->dev,
129 		"info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
130 	       old_pkt_filter, priv->curr_pkt_filter);
131 	if (old_pkt_filter != priv->curr_pkt_filter) {
132 		ret = mwifiex_send_cmd_async(priv, HostCmd_CMD_MAC_CONTROL,
133 					     HostCmd_ACT_GEN_SET,
134 					     0, &priv->curr_pkt_filter);
135 	}
136 
137 	return ret;
138 }
139 
140 /*
141  * This function fills bss descriptor structure using provided
142  * information.
143  */
mwifiex_fill_new_bss_desc(struct mwifiex_private * priv,u8 * bssid,s32 rssi,u8 * ie_buf,size_t ie_len,u16 beacon_period,u16 cap_info_bitmap,u8 band,struct mwifiex_bssdescriptor * bss_desc)144 int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
145 			      u8 *bssid, s32 rssi, u8 *ie_buf,
146 			      size_t ie_len, u16 beacon_period,
147 			      u16 cap_info_bitmap, u8 band,
148 			      struct mwifiex_bssdescriptor *bss_desc)
149 {
150 	int ret;
151 
152 	memcpy(bss_desc->mac_address, bssid, ETH_ALEN);
153 	bss_desc->rssi = rssi;
154 	bss_desc->beacon_buf = ie_buf;
155 	bss_desc->beacon_buf_size = ie_len;
156 	bss_desc->beacon_period = beacon_period;
157 	bss_desc->cap_info_bitmap = cap_info_bitmap;
158 	bss_desc->bss_band = band;
159 	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
160 		dev_dbg(priv->adapter->dev, "info: InterpretIE: AP WEP enabled\n");
161 		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
162 	} else {
163 		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
164 	}
165 	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
166 		bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
167 	else
168 		bss_desc->bss_mode = NL80211_IFTYPE_STATION;
169 
170 	ret = mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc,
171 					      ie_buf, ie_len);
172 
173 	return ret;
174 }
175 
176 /*
177  * In Ad-Hoc mode, the IBSS is created if not found in scan list.
178  * In both Ad-Hoc and infra mode, an deauthentication is performed
179  * first.
180  */
mwifiex_bss_start(struct mwifiex_private * priv,struct cfg80211_bss * bss,struct cfg80211_ssid * req_ssid)181 int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
182 		      struct cfg80211_ssid *req_ssid)
183 {
184 	int ret;
185 	struct mwifiex_adapter *adapter = priv->adapter;
186 	struct mwifiex_bssdescriptor *bss_desc = NULL;
187 	u8 *beacon_ie = NULL;
188 
189 	priv->scan_block = false;
190 
191 	if (bss) {
192 		/* Allocate and fill new bss descriptor */
193 		bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
194 				GFP_KERNEL);
195 		if (!bss_desc) {
196 			dev_err(priv->adapter->dev, " failed to alloc bss_desc\n");
197 			return -ENOMEM;
198 		}
199 
200 		beacon_ie = kmemdup(bss->information_elements,
201 					bss->len_beacon_ies, GFP_KERNEL);
202 		if (!beacon_ie) {
203 			kfree(bss_desc);
204 			dev_err(priv->adapter->dev, " failed to alloc beacon_ie\n");
205 			return -ENOMEM;
206 		}
207 
208 		ret = mwifiex_fill_new_bss_desc(priv, bss->bssid, bss->signal,
209 						beacon_ie, bss->len_beacon_ies,
210 						bss->beacon_interval,
211 						bss->capability,
212 						*(u8 *)bss->priv, bss_desc);
213 		if (ret)
214 			goto done;
215 	}
216 
217 	if (priv->bss_mode == NL80211_IFTYPE_STATION) {
218 		/* Infra mode */
219 		ret = mwifiex_deauthenticate(priv, NULL);
220 		if (ret)
221 			goto done;
222 
223 		ret = mwifiex_check_network_compatibility(priv, bss_desc);
224 		if (ret)
225 			goto done;
226 
227 		dev_dbg(adapter->dev, "info: SSID found in scan list ... "
228 				      "associating...\n");
229 
230 		if (!netif_queue_stopped(priv->netdev))
231 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
232 		if (netif_carrier_ok(priv->netdev))
233 			netif_carrier_off(priv->netdev);
234 
235 		/* Clear any past association response stored for
236 		 * application retrieval */
237 		priv->assoc_rsp_size = 0;
238 		ret = mwifiex_associate(priv, bss_desc);
239 
240 		/* If auth type is auto and association fails using open mode,
241 		 * try to connect using shared mode */
242 		if (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
243 		    priv->sec_info.is_authtype_auto &&
244 		    priv->sec_info.wep_enabled) {
245 			priv->sec_info.authentication_mode =
246 						NL80211_AUTHTYPE_SHARED_KEY;
247 			ret = mwifiex_associate(priv, bss_desc);
248 		}
249 
250 		if (bss)
251 			cfg80211_put_bss(bss);
252 	} else {
253 		/* Adhoc mode */
254 		/* If the requested SSID matches current SSID, return */
255 		if (bss_desc && bss_desc->ssid.ssid_len &&
256 		    (!mwifiex_ssid_cmp(&priv->curr_bss_params.bss_descriptor.
257 				       ssid, &bss_desc->ssid))) {
258 			kfree(bss_desc);
259 			kfree(beacon_ie);
260 			return 0;
261 		}
262 
263 		/* Exit Adhoc mode first */
264 		dev_dbg(adapter->dev, "info: Sending Adhoc Stop\n");
265 		ret = mwifiex_deauthenticate(priv, NULL);
266 		if (ret)
267 			goto done;
268 
269 		priv->adhoc_is_link_sensed = false;
270 
271 		ret = mwifiex_check_network_compatibility(priv, bss_desc);
272 
273 		if (!netif_queue_stopped(priv->netdev))
274 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
275 		if (netif_carrier_ok(priv->netdev))
276 			netif_carrier_off(priv->netdev);
277 
278 		if (!ret) {
279 			dev_dbg(adapter->dev, "info: network found in scan"
280 							" list. Joining...\n");
281 			ret = mwifiex_adhoc_join(priv, bss_desc);
282 			if (bss)
283 				cfg80211_put_bss(bss);
284 		} else {
285 			dev_dbg(adapter->dev, "info: Network not found in "
286 				"the list, creating adhoc with ssid = %s\n",
287 				req_ssid->ssid);
288 			ret = mwifiex_adhoc_start(priv, req_ssid);
289 		}
290 	}
291 
292 done:
293 	kfree(bss_desc);
294 	kfree(beacon_ie);
295 	return ret;
296 }
297 
298 /*
299  * IOCTL request handler to set host sleep configuration.
300  *
301  * This function prepares the correct firmware command and
302  * issues it.
303  */
mwifiex_set_hs_params(struct mwifiex_private * priv,u16 action,int cmd_type,struct mwifiex_ds_hs_cfg * hs_cfg)304 static int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
305 				 int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
306 
307 {
308 	struct mwifiex_adapter *adapter = priv->adapter;
309 	int status = 0;
310 	u32 prev_cond = 0;
311 
312 	if (!hs_cfg)
313 		return -ENOMEM;
314 
315 	switch (action) {
316 	case HostCmd_ACT_GEN_SET:
317 		if (adapter->pps_uapsd_mode) {
318 			dev_dbg(adapter->dev, "info: Host Sleep IOCTL"
319 				" is blocked in UAPSD/PPS mode\n");
320 			status = -1;
321 			break;
322 		}
323 		if (hs_cfg->is_invoke_hostcmd) {
324 			if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL) {
325 				if (!adapter->is_hs_configured)
326 					/* Already cancelled */
327 					break;
328 				/* Save previous condition */
329 				prev_cond = le32_to_cpu(adapter->hs_cfg
330 							.conditions);
331 				adapter->hs_cfg.conditions =
332 						cpu_to_le32(hs_cfg->conditions);
333 			} else if (hs_cfg->conditions) {
334 				adapter->hs_cfg.conditions =
335 						cpu_to_le32(hs_cfg->conditions);
336 				adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
337 				if (hs_cfg->gap)
338 					adapter->hs_cfg.gap = (u8)hs_cfg->gap;
339 			} else if (adapter->hs_cfg.conditions
340 				   == cpu_to_le32(HOST_SLEEP_CFG_CANCEL)) {
341 				/* Return failure if no parameters for HS
342 				   enable */
343 				status = -1;
344 				break;
345 			}
346 			if (cmd_type == MWIFIEX_SYNC_CMD)
347 				status = mwifiex_send_cmd_sync(priv,
348 						HostCmd_CMD_802_11_HS_CFG_ENH,
349 						HostCmd_ACT_GEN_SET, 0,
350 						&adapter->hs_cfg);
351 			else
352 				status = mwifiex_send_cmd_async(priv,
353 						HostCmd_CMD_802_11_HS_CFG_ENH,
354 						HostCmd_ACT_GEN_SET, 0,
355 						&adapter->hs_cfg);
356 			if (hs_cfg->conditions == HOST_SLEEP_CFG_CANCEL)
357 				/* Restore previous condition */
358 				adapter->hs_cfg.conditions =
359 						cpu_to_le32(prev_cond);
360 		} else {
361 			adapter->hs_cfg.conditions =
362 						cpu_to_le32(hs_cfg->conditions);
363 			adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
364 			adapter->hs_cfg.gap = (u8)hs_cfg->gap;
365 		}
366 		break;
367 	case HostCmd_ACT_GEN_GET:
368 		hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
369 		hs_cfg->gpio = adapter->hs_cfg.gpio;
370 		hs_cfg->gap = adapter->hs_cfg.gap;
371 		break;
372 	default:
373 		status = -1;
374 		break;
375 	}
376 
377 	return status;
378 }
379 
380 /*
381  * Sends IOCTL request to cancel the existing Host Sleep configuration.
382  *
383  * This function allocates the IOCTL request buffer, fills it
384  * with requisite parameters and calls the IOCTL handler.
385  */
mwifiex_cancel_hs(struct mwifiex_private * priv,int cmd_type)386 int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
387 {
388 	struct mwifiex_ds_hs_cfg hscfg;
389 
390 	hscfg.conditions = HOST_SLEEP_CFG_CANCEL;
391 	hscfg.is_invoke_hostcmd = true;
392 
393 	return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
394 				    cmd_type, &hscfg);
395 }
396 EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);
397 
398 /*
399  * Sends IOCTL request to cancel the existing Host Sleep configuration.
400  *
401  * This function allocates the IOCTL request buffer, fills it
402  * with requisite parameters and calls the IOCTL handler.
403  */
mwifiex_enable_hs(struct mwifiex_adapter * adapter)404 int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
405 {
406 	struct mwifiex_ds_hs_cfg hscfg;
407 
408 	if (adapter->hs_activated) {
409 		dev_dbg(adapter->dev, "cmd: HS Already actived\n");
410 		return true;
411 	}
412 
413 	adapter->hs_activate_wait_q_woken = false;
414 
415 	memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
416 	hscfg.is_invoke_hostcmd = true;
417 
418 	if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
419 						   MWIFIEX_BSS_ROLE_STA),
420 				  HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
421 				  &hscfg)) {
422 		dev_err(adapter->dev, "IOCTL request HS enable failed\n");
423 		return false;
424 	}
425 
426 	if (wait_event_interruptible(adapter->hs_activate_wait_q,
427 				     adapter->hs_activate_wait_q_woken)) {
428 		dev_err(adapter->dev, "hs_activate_wait_q terminated\n");
429 		return false;
430 	}
431 
432 	return true;
433 }
434 EXPORT_SYMBOL_GPL(mwifiex_enable_hs);
435 
436 /*
437  * IOCTL request handler to get BSS information.
438  *
439  * This function collates the information from different driver structures
440  * to send to the user.
441  */
mwifiex_get_bss_info(struct mwifiex_private * priv,struct mwifiex_bss_info * info)442 int mwifiex_get_bss_info(struct mwifiex_private *priv,
443 			 struct mwifiex_bss_info *info)
444 {
445 	struct mwifiex_adapter *adapter = priv->adapter;
446 	struct mwifiex_bssdescriptor *bss_desc;
447 
448 	if (!info)
449 		return -1;
450 
451 	bss_desc = &priv->curr_bss_params.bss_descriptor;
452 
453 	info->bss_mode = priv->bss_mode;
454 
455 	memcpy(&info->ssid, &bss_desc->ssid, sizeof(struct cfg80211_ssid));
456 
457 	memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);
458 
459 	info->bss_chan = bss_desc->channel;
460 
461 	info->region_code = adapter->region_code;
462 
463 	info->media_connected = priv->media_connected;
464 
465 	info->max_power_level = priv->max_tx_power_level;
466 	info->min_power_level = priv->min_tx_power_level;
467 
468 	info->adhoc_state = priv->adhoc_state;
469 
470 	info->bcn_nf_last = priv->bcn_nf_last;
471 
472 	if (priv->sec_info.wep_enabled)
473 		info->wep_status = true;
474 	else
475 		info->wep_status = false;
476 
477 	info->is_hs_configured = adapter->is_hs_configured;
478 	info->is_deep_sleep = adapter->is_deep_sleep;
479 
480 	return 0;
481 }
482 
483 /*
484  * The function disables auto deep sleep mode.
485  */
mwifiex_disable_auto_ds(struct mwifiex_private * priv)486 int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
487 {
488 	struct mwifiex_ds_auto_ds auto_ds;
489 
490 	auto_ds.auto_ds = DEEP_SLEEP_OFF;
491 
492 	return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
493 				     DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds);
494 }
495 EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);
496 
497 /*
498  * IOCTL request handler to set/get active channel.
499  *
500  * This function performs validity checking on channel/frequency
501  * compatibility and returns failure if not valid.
502  */
mwifiex_bss_set_channel(struct mwifiex_private * priv,struct mwifiex_chan_freq_power * chan)503 int mwifiex_bss_set_channel(struct mwifiex_private *priv,
504 			    struct mwifiex_chan_freq_power *chan)
505 {
506 	struct mwifiex_adapter *adapter = priv->adapter;
507 	struct mwifiex_chan_freq_power *cfp = NULL;
508 
509 	if (!chan)
510 		return -1;
511 
512 	if (!chan->channel && !chan->freq)
513 		return -1;
514 	if (adapter->adhoc_start_band & BAND_AN)
515 		adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
516 	else if (adapter->adhoc_start_band & BAND_A)
517 		adapter->adhoc_start_band = BAND_G | BAND_B;
518 	if (chan->channel) {
519 		if (chan->channel <= MAX_CHANNEL_BAND_BG)
520 			cfp = mwifiex_get_cfp(priv, 0, (u16) chan->channel, 0);
521 		if (!cfp) {
522 			cfp = mwifiex_get_cfp(priv, BAND_A,
523 					      (u16) chan->channel, 0);
524 			if (cfp) {
525 				if (adapter->adhoc_11n_enabled)
526 					adapter->adhoc_start_band = BAND_A
527 								    | BAND_AN;
528 				else
529 					adapter->adhoc_start_band = BAND_A;
530 			}
531 		}
532 	} else {
533 		if (chan->freq <= MAX_FREQUENCY_BAND_BG)
534 			cfp = mwifiex_get_cfp(priv, 0, 0, chan->freq);
535 		if (!cfp) {
536 			cfp = mwifiex_get_cfp(priv, BAND_A, 0, chan->freq);
537 			if (cfp) {
538 				if (adapter->adhoc_11n_enabled)
539 					adapter->adhoc_start_band = BAND_A
540 								    | BAND_AN;
541 				else
542 					adapter->adhoc_start_band = BAND_A;
543 			}
544 		}
545 	}
546 	if (!cfp || !cfp->channel) {
547 		dev_err(adapter->dev, "invalid channel/freq\n");
548 		return -1;
549 	}
550 	priv->adhoc_channel = (u8) cfp->channel;
551 	chan->channel = cfp->channel;
552 	chan->freq = cfp->freq;
553 
554 	return 0;
555 }
556 
557 /*
558  * IOCTL request handler to set/get Ad-Hoc channel.
559  *
560  * This function prepares the correct firmware command and
561  * issues it to set or get the ad-hoc channel.
562  */
mwifiex_bss_ioctl_ibss_channel(struct mwifiex_private * priv,u16 action,u16 * channel)563 static int mwifiex_bss_ioctl_ibss_channel(struct mwifiex_private *priv,
564 					  u16 action, u16 *channel)
565 {
566 	if (action == HostCmd_ACT_GEN_GET) {
567 		if (!priv->media_connected) {
568 			*channel = priv->adhoc_channel;
569 			return 0;
570 		}
571 	} else {
572 		priv->adhoc_channel = (u8) *channel;
573 	}
574 
575 	return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_RF_CHANNEL,
576 				     action, 0, channel);
577 }
578 
579 /*
580  * IOCTL request handler to change Ad-Hoc channel.
581  *
582  * This function allocates the IOCTL request buffer, fills it
583  * with requisite parameters and calls the IOCTL handler.
584  *
585  * The function follows the following steps to perform the change -
586  *      - Get current IBSS information
587  *      - Get current channel
588  *      - If no change is required, return
589  *      - If not connected, change channel and return
590  *      - If connected,
591  *          - Disconnect
592  *          - Change channel
593  *          - Perform specific SSID scan with same SSID
594  *          - Start/Join the IBSS
595  */
596 int
mwifiex_drv_change_adhoc_chan(struct mwifiex_private * priv,u16 channel)597 mwifiex_drv_change_adhoc_chan(struct mwifiex_private *priv, u16 channel)
598 {
599 	int ret;
600 	struct mwifiex_bss_info bss_info;
601 	struct mwifiex_ssid_bssid ssid_bssid;
602 	u16 curr_chan = 0;
603 	struct cfg80211_bss *bss = NULL;
604 	struct ieee80211_channel *chan;
605 	enum ieee80211_band band;
606 
607 	memset(&bss_info, 0, sizeof(bss_info));
608 
609 	/* Get BSS information */
610 	if (mwifiex_get_bss_info(priv, &bss_info))
611 		return -1;
612 
613 	/* Get current channel */
614 	ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_GET,
615 					     &curr_chan);
616 
617 	if (curr_chan == channel) {
618 		ret = 0;
619 		goto done;
620 	}
621 	dev_dbg(priv->adapter->dev, "cmd: updating channel from %d to %d\n",
622 		curr_chan, channel);
623 
624 	if (!bss_info.media_connected) {
625 		ret = 0;
626 		goto done;
627 	}
628 
629 	/* Do disonnect */
630 	memset(&ssid_bssid, 0, ETH_ALEN);
631 	ret = mwifiex_deauthenticate(priv, ssid_bssid.bssid);
632 
633 	ret = mwifiex_bss_ioctl_ibss_channel(priv, HostCmd_ACT_GEN_SET,
634 					     &channel);
635 
636 	/* Do specific SSID scanning */
637 	if (mwifiex_request_scan(priv, &bss_info.ssid)) {
638 		ret = -1;
639 		goto done;
640 	}
641 
642 	band = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
643 	chan = __ieee80211_get_channel(priv->wdev->wiphy,
644 				       ieee80211_channel_to_frequency(channel,
645 								      band));
646 
647 	/* Find the BSS we want using available scan results */
648 	bss = cfg80211_get_bss(priv->wdev->wiphy, chan, bss_info.bssid,
649 			       bss_info.ssid.ssid, bss_info.ssid.ssid_len,
650 			       WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
651 	if (!bss)
652 		wiphy_warn(priv->wdev->wiphy, "assoc: bss %pM not in scan results\n",
653 			   bss_info.bssid);
654 
655 	ret = mwifiex_bss_start(priv, bss, &bss_info.ssid);
656 done:
657 	return ret;
658 }
659 
660 /*
661  * IOCTL request handler to get rate.
662  *
663  * This function prepares the correct firmware command and
664  * issues it to get the current rate if it is connected,
665  * otherwise, the function returns the lowest supported rate
666  * for the band.
667  */
mwifiex_rate_ioctl_get_rate_value(struct mwifiex_private * priv,struct mwifiex_rate_cfg * rate_cfg)668 static int mwifiex_rate_ioctl_get_rate_value(struct mwifiex_private *priv,
669 					     struct mwifiex_rate_cfg *rate_cfg)
670 {
671 	rate_cfg->is_rate_auto = priv->is_data_rate_auto;
672 	return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
673 				     HostCmd_ACT_GEN_GET, 0, NULL);
674 }
675 
676 /*
677  * IOCTL request handler to set rate.
678  *
679  * This function prepares the correct firmware command and
680  * issues it to set the current rate.
681  *
682  * The function also performs validation checking on the supplied value.
683  */
mwifiex_rate_ioctl_set_rate_value(struct mwifiex_private * priv,struct mwifiex_rate_cfg * rate_cfg)684 static int mwifiex_rate_ioctl_set_rate_value(struct mwifiex_private *priv,
685 					     struct mwifiex_rate_cfg *rate_cfg)
686 {
687 	u8 rates[MWIFIEX_SUPPORTED_RATES];
688 	u8 *rate;
689 	int rate_index, ret;
690 	u16 bitmap_rates[MAX_BITMAP_RATES_SIZE];
691 	u32 i;
692 	struct mwifiex_adapter *adapter = priv->adapter;
693 
694 	if (rate_cfg->is_rate_auto) {
695 		memset(bitmap_rates, 0, sizeof(bitmap_rates));
696 		/* Support all HR/DSSS rates */
697 		bitmap_rates[0] = 0x000F;
698 		/* Support all OFDM rates */
699 		bitmap_rates[1] = 0x00FF;
700 		/* Support all HT-MCSs rate */
701 		for (i = 0; i < ARRAY_SIZE(priv->bitmap_rates) - 3; i++)
702 			bitmap_rates[i + 2] = 0xFFFF;
703 		bitmap_rates[9] = 0x3FFF;
704 	} else {
705 		memset(rates, 0, sizeof(rates));
706 		mwifiex_get_active_data_rates(priv, rates);
707 		rate = rates;
708 		for (i = 0; (rate[i] && i < MWIFIEX_SUPPORTED_RATES); i++) {
709 			dev_dbg(adapter->dev, "info: rate=%#x wanted=%#x\n",
710 				rate[i], rate_cfg->rate);
711 			if ((rate[i] & 0x7f) == (rate_cfg->rate & 0x7f))
712 				break;
713 		}
714 		if ((i == MWIFIEX_SUPPORTED_RATES) || !rate[i]) {
715 			dev_err(adapter->dev, "fixed data rate %#x is out "
716 			       "of range\n", rate_cfg->rate);
717 			return -1;
718 		}
719 		memset(bitmap_rates, 0, sizeof(bitmap_rates));
720 
721 		rate_index = mwifiex_data_rate_to_index(rate_cfg->rate);
722 
723 		/* Only allow b/g rates to be set */
724 		if (rate_index >= MWIFIEX_RATE_INDEX_HRDSSS0 &&
725 		    rate_index <= MWIFIEX_RATE_INDEX_HRDSSS3) {
726 			bitmap_rates[0] = 1 << rate_index;
727 		} else {
728 			rate_index -= 1; /* There is a 0x00 in the table */
729 			if (rate_index >= MWIFIEX_RATE_INDEX_OFDM0 &&
730 			    rate_index <= MWIFIEX_RATE_INDEX_OFDM7)
731 				bitmap_rates[1] = 1 << (rate_index -
732 						   MWIFIEX_RATE_INDEX_OFDM0);
733 		}
734 	}
735 
736 	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TX_RATE_CFG,
737 				    HostCmd_ACT_GEN_SET, 0, bitmap_rates);
738 
739 	return ret;
740 }
741 
742 /*
743  * IOCTL request handler to set/get rate.
744  *
745  * This function can be used to set/get either the rate value or the
746  * rate index.
747  */
mwifiex_rate_ioctl_cfg(struct mwifiex_private * priv,struct mwifiex_rate_cfg * rate_cfg)748 static int mwifiex_rate_ioctl_cfg(struct mwifiex_private *priv,
749 				  struct mwifiex_rate_cfg *rate_cfg)
750 {
751 	int status;
752 
753 	if (!rate_cfg)
754 		return -1;
755 
756 	if (rate_cfg->action == HostCmd_ACT_GEN_GET)
757 		status = mwifiex_rate_ioctl_get_rate_value(priv, rate_cfg);
758 	else
759 		status = mwifiex_rate_ioctl_set_rate_value(priv, rate_cfg);
760 
761 	return status;
762 }
763 
764 /*
765  * Sends IOCTL request to get the data rate.
766  *
767  * This function allocates the IOCTL request buffer, fills it
768  * with requisite parameters and calls the IOCTL handler.
769  */
mwifiex_drv_get_data_rate(struct mwifiex_private * priv,struct mwifiex_rate_cfg * rate)770 int mwifiex_drv_get_data_rate(struct mwifiex_private *priv,
771 			      struct mwifiex_rate_cfg *rate)
772 {
773 	int ret;
774 
775 	memset(rate, 0, sizeof(struct mwifiex_rate_cfg));
776 	rate->action = HostCmd_ACT_GEN_GET;
777 	ret = mwifiex_rate_ioctl_cfg(priv, rate);
778 
779 	if (!ret) {
780 		if (rate->is_rate_auto)
781 			rate->rate = mwifiex_index_to_data_rate(priv,
782 								priv->tx_rate,
783 								priv->tx_htinfo
784 								);
785 		else
786 			rate->rate = priv->data_rate;
787 	} else {
788 		ret = -1;
789 	}
790 
791 	return ret;
792 }
793 
794 /*
795  * IOCTL request handler to set tx power configuration.
796  *
797  * This function prepares the correct firmware command and
798  * issues it.
799  *
800  * For non-auto power mode, all the following power groups are set -
801  *      - Modulation class HR/DSSS
802  *      - Modulation class OFDM
803  *      - Modulation class HTBW20
804  *      - Modulation class HTBW40
805  */
mwifiex_set_tx_power(struct mwifiex_private * priv,struct mwifiex_power_cfg * power_cfg)806 int mwifiex_set_tx_power(struct mwifiex_private *priv,
807 			 struct mwifiex_power_cfg *power_cfg)
808 {
809 	int ret;
810 	struct host_cmd_ds_txpwr_cfg *txp_cfg;
811 	struct mwifiex_types_power_group *pg_tlv;
812 	struct mwifiex_power_group *pg;
813 	u8 *buf;
814 	u16 dbm = 0;
815 
816 	if (!power_cfg->is_power_auto) {
817 		dbm = (u16) power_cfg->power_level;
818 		if ((dbm < priv->min_tx_power_level) ||
819 		    (dbm > priv->max_tx_power_level)) {
820 			dev_err(priv->adapter->dev, "txpower value %d dBm"
821 				" is out of range (%d dBm-%d dBm)\n",
822 				dbm, priv->min_tx_power_level,
823 				priv->max_tx_power_level);
824 			return -1;
825 		}
826 	}
827 	buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
828 	if (!buf) {
829 		dev_err(priv->adapter->dev, "%s: failed to alloc cmd buffer\n",
830 			__func__);
831 		return -ENOMEM;
832 	}
833 
834 	txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
835 	txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
836 	if (!power_cfg->is_power_auto) {
837 		txp_cfg->mode = cpu_to_le32(1);
838 		pg_tlv = (struct mwifiex_types_power_group *)
839 			 (buf + sizeof(struct host_cmd_ds_txpwr_cfg));
840 		pg_tlv->type = TLV_TYPE_POWER_GROUP;
841 		pg_tlv->length = 4 * sizeof(struct mwifiex_power_group);
842 		pg = (struct mwifiex_power_group *)
843 		     (buf + sizeof(struct host_cmd_ds_txpwr_cfg)
844 		      + sizeof(struct mwifiex_types_power_group));
845 		/* Power group for modulation class HR/DSSS */
846 		pg->first_rate_code = 0x00;
847 		pg->last_rate_code = 0x03;
848 		pg->modulation_class = MOD_CLASS_HR_DSSS;
849 		pg->power_step = 0;
850 		pg->power_min = (s8) dbm;
851 		pg->power_max = (s8) dbm;
852 		pg++;
853 		/* Power group for modulation class OFDM */
854 		pg->first_rate_code = 0x00;
855 		pg->last_rate_code = 0x07;
856 		pg->modulation_class = MOD_CLASS_OFDM;
857 		pg->power_step = 0;
858 		pg->power_min = (s8) dbm;
859 		pg->power_max = (s8) dbm;
860 		pg++;
861 		/* Power group for modulation class HTBW20 */
862 		pg->first_rate_code = 0x00;
863 		pg->last_rate_code = 0x20;
864 		pg->modulation_class = MOD_CLASS_HT;
865 		pg->power_step = 0;
866 		pg->power_min = (s8) dbm;
867 		pg->power_max = (s8) dbm;
868 		pg->ht_bandwidth = HT_BW_20;
869 		pg++;
870 		/* Power group for modulation class HTBW40 */
871 		pg->first_rate_code = 0x00;
872 		pg->last_rate_code = 0x20;
873 		pg->modulation_class = MOD_CLASS_HT;
874 		pg->power_step = 0;
875 		pg->power_min = (s8) dbm;
876 		pg->power_max = (s8) dbm;
877 		pg->ht_bandwidth = HT_BW_40;
878 	}
879 	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_TXPWR_CFG,
880 				    HostCmd_ACT_GEN_SET, 0, buf);
881 
882 	kfree(buf);
883 	return ret;
884 }
885 
886 /*
887  * IOCTL request handler to get power save mode.
888  *
889  * This function prepares the correct firmware command and
890  * issues it.
891  */
mwifiex_drv_set_power(struct mwifiex_private * priv,u32 * ps_mode)892 int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
893 {
894 	int ret;
895 	struct mwifiex_adapter *adapter = priv->adapter;
896 	u16 sub_cmd;
897 
898 	if (*ps_mode)
899 		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
900 	else
901 		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
902 	sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
903 	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
904 				    sub_cmd, BITMAP_STA_PS, NULL);
905 	if ((!ret) && (sub_cmd == DIS_AUTO_PS))
906 		ret = mwifiex_send_cmd_async(priv,
907 					     HostCmd_CMD_802_11_PS_MODE_ENH,
908 					     GET_PS, 0, NULL);
909 
910 	return ret;
911 }
912 
913 /*
914  * IOCTL request handler to set/reset WPA IE.
915  *
916  * The supplied WPA IE is treated as a opaque buffer. Only the first field
917  * is checked to determine WPA version. If buffer length is zero, the existing
918  * WPA IE is reset.
919  */
mwifiex_set_wpa_ie_helper(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)920 static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
921 				     u8 *ie_data_ptr, u16 ie_len)
922 {
923 	if (ie_len) {
924 		if (ie_len > sizeof(priv->wpa_ie)) {
925 			dev_err(priv->adapter->dev,
926 				"failed to copy WPA IE, too big\n");
927 			return -1;
928 		}
929 		memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
930 		priv->wpa_ie_len = (u8) ie_len;
931 		dev_dbg(priv->adapter->dev, "cmd: Set Wpa_ie_len=%d IE=%#x\n",
932 			priv->wpa_ie_len, priv->wpa_ie[0]);
933 
934 		if (priv->wpa_ie[0] == WLAN_EID_WPA) {
935 			priv->sec_info.wpa_enabled = true;
936 		} else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
937 			priv->sec_info.wpa2_enabled = true;
938 		} else {
939 			priv->sec_info.wpa_enabled = false;
940 			priv->sec_info.wpa2_enabled = false;
941 		}
942 	} else {
943 		memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
944 		priv->wpa_ie_len = 0;
945 		dev_dbg(priv->adapter->dev, "info: reset wpa_ie_len=%d IE=%#x\n",
946 			priv->wpa_ie_len, priv->wpa_ie[0]);
947 		priv->sec_info.wpa_enabled = false;
948 		priv->sec_info.wpa2_enabled = false;
949 	}
950 
951 	return 0;
952 }
953 
954 /*
955  * IOCTL request handler to set/reset WAPI IE.
956  *
957  * The supplied WAPI IE is treated as a opaque buffer. Only the first field
958  * is checked to internally enable WAPI. If buffer length is zero, the existing
959  * WAPI IE is reset.
960  */
mwifiex_set_wapi_ie(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)961 static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
962 			       u8 *ie_data_ptr, u16 ie_len)
963 {
964 	if (ie_len) {
965 		if (ie_len > sizeof(priv->wapi_ie)) {
966 			dev_dbg(priv->adapter->dev,
967 				"info: failed to copy WAPI IE, too big\n");
968 			return -1;
969 		}
970 		memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
971 		priv->wapi_ie_len = ie_len;
972 		dev_dbg(priv->adapter->dev, "cmd: Set wapi_ie_len=%d IE=%#x\n",
973 			priv->wapi_ie_len, priv->wapi_ie[0]);
974 
975 		if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
976 			priv->sec_info.wapi_enabled = true;
977 	} else {
978 		memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
979 		priv->wapi_ie_len = ie_len;
980 		dev_dbg(priv->adapter->dev,
981 			"info: Reset wapi_ie_len=%d IE=%#x\n",
982 		       priv->wapi_ie_len, priv->wapi_ie[0]);
983 		priv->sec_info.wapi_enabled = false;
984 	}
985 	return 0;
986 }
987 
988 /*
989  * IOCTL request handler to set WAPI key.
990  *
991  * This function prepares the correct firmware command and
992  * issues it.
993  */
mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)994 static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
995 			       struct mwifiex_ds_encrypt_key *encrypt_key)
996 {
997 
998 	return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
999 				     HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
1000 				     encrypt_key);
1001 }
1002 
1003 /*
1004  * IOCTL request handler to set WEP network key.
1005  *
1006  * This function prepares the correct firmware command and
1007  * issues it, after validation checks.
1008  */
mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)1009 static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
1010 			      struct mwifiex_ds_encrypt_key *encrypt_key)
1011 {
1012 	int ret;
1013 	struct mwifiex_wep_key *wep_key;
1014 	int index;
1015 
1016 	if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
1017 		priv->wep_key_curr_index = 0;
1018 	wep_key = &priv->wep_key[priv->wep_key_curr_index];
1019 	index = encrypt_key->key_index;
1020 	if (encrypt_key->key_disable) {
1021 		priv->sec_info.wep_enabled = 0;
1022 	} else if (!encrypt_key->key_len) {
1023 		/* Copy the required key as the current key */
1024 		wep_key = &priv->wep_key[index];
1025 		if (!wep_key->key_length) {
1026 			dev_err(priv->adapter->dev,
1027 				"key not set, so cannot enable it\n");
1028 			return -1;
1029 		}
1030 		priv->wep_key_curr_index = (u16) index;
1031 		priv->sec_info.wep_enabled = 1;
1032 	} else {
1033 		wep_key = &priv->wep_key[index];
1034 		memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
1035 		/* Copy the key in the driver */
1036 		memcpy(wep_key->key_material,
1037 		       encrypt_key->key_material,
1038 		       encrypt_key->key_len);
1039 		wep_key->key_index = index;
1040 		wep_key->key_length = encrypt_key->key_len;
1041 		priv->sec_info.wep_enabled = 1;
1042 	}
1043 	if (wep_key->key_length) {
1044 		/* Send request to firmware */
1045 		ret = mwifiex_send_cmd_async(priv,
1046 					     HostCmd_CMD_802_11_KEY_MATERIAL,
1047 					     HostCmd_ACT_GEN_SET, 0, NULL);
1048 		if (ret)
1049 			return ret;
1050 	}
1051 	if (priv->sec_info.wep_enabled)
1052 		priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
1053 	else
1054 		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
1055 
1056 	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_MAC_CONTROL,
1057 				    HostCmd_ACT_GEN_SET, 0,
1058 				    &priv->curr_pkt_filter);
1059 
1060 	return ret;
1061 }
1062 
1063 /*
1064  * IOCTL request handler to set WPA key.
1065  *
1066  * This function prepares the correct firmware command and
1067  * issues it, after validation checks.
1068  *
1069  * Current driver only supports key length of up to 32 bytes.
1070  *
1071  * This function can also be used to disable a currently set key.
1072  */
mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)1073 static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
1074 			      struct mwifiex_ds_encrypt_key *encrypt_key)
1075 {
1076 	int ret;
1077 	u8 remove_key = false;
1078 	struct host_cmd_ds_802_11_key_material *ibss_key;
1079 
1080 	/* Current driver only supports key length of up to 32 bytes */
1081 	if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
1082 		dev_err(priv->adapter->dev, "key length too long\n");
1083 		return -1;
1084 	}
1085 
1086 	if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
1087 		/*
1088 		 * IBSS/WPA-None uses only one key (Group) for both receiving
1089 		 * and sending unicast and multicast packets.
1090 		 */
1091 		/* Send the key as PTK to firmware */
1092 		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1093 		ret = mwifiex_send_cmd_async(priv,
1094 					     HostCmd_CMD_802_11_KEY_MATERIAL,
1095 					     HostCmd_ACT_GEN_SET,
1096 					     KEY_INFO_ENABLED, encrypt_key);
1097 		if (ret)
1098 			return ret;
1099 
1100 		ibss_key = &priv->aes_key;
1101 		memset(ibss_key, 0,
1102 		       sizeof(struct host_cmd_ds_802_11_key_material));
1103 		/* Copy the key in the driver */
1104 		memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
1105 		       encrypt_key->key_len);
1106 		memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
1107 		       sizeof(ibss_key->key_param_set.key_len));
1108 		ibss_key->key_param_set.key_type_id
1109 			= cpu_to_le16(KEY_TYPE_ID_TKIP);
1110 		ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
1111 
1112 		/* Send the key as GTK to firmware */
1113 		encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
1114 	}
1115 
1116 	if (!encrypt_key->key_index)
1117 		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
1118 
1119 	if (remove_key)
1120 		ret = mwifiex_send_cmd_sync(priv,
1121 					    HostCmd_CMD_802_11_KEY_MATERIAL,
1122 					    HostCmd_ACT_GEN_SET,
1123 					    !KEY_INFO_ENABLED, encrypt_key);
1124 	else
1125 		ret = mwifiex_send_cmd_sync(priv,
1126 					    HostCmd_CMD_802_11_KEY_MATERIAL,
1127 					    HostCmd_ACT_GEN_SET,
1128 					    KEY_INFO_ENABLED, encrypt_key);
1129 
1130 	return ret;
1131 }
1132 
1133 /*
1134  * IOCTL request handler to set/get network keys.
1135  *
1136  * This is a generic key handling function which supports WEP, WPA
1137  * and WAPI.
1138  */
1139 static int
mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private * priv,struct mwifiex_ds_encrypt_key * encrypt_key)1140 mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
1141 			      struct mwifiex_ds_encrypt_key *encrypt_key)
1142 {
1143 	int status;
1144 
1145 	if (encrypt_key->is_wapi_key)
1146 		status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
1147 	else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1148 		status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
1149 	else
1150 		status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
1151 	return status;
1152 }
1153 
1154 /*
1155  * This function returns the driver version.
1156  */
1157 int
mwifiex_drv_get_driver_version(struct mwifiex_adapter * adapter,char * version,int max_len)1158 mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
1159 			       int max_len)
1160 {
1161 	union {
1162 		u32 l;
1163 		u8 c[4];
1164 	} ver;
1165 	char fw_ver[32];
1166 
1167 	ver.l = adapter->fw_release_number;
1168 	sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);
1169 
1170 	snprintf(version, max_len, driver_version, fw_ver);
1171 
1172 	dev_dbg(adapter->dev, "info: MWIFIEX VERSION: %s\n", version);
1173 
1174 	return 0;
1175 }
1176 
1177 /*
1178  * Sends IOCTL request to get signal information.
1179  *
1180  * This function allocates the IOCTL request buffer, fills it
1181  * with requisite parameters and calls the IOCTL handler.
1182  */
mwifiex_get_signal_info(struct mwifiex_private * priv,struct mwifiex_ds_get_signal * signal)1183 int mwifiex_get_signal_info(struct mwifiex_private *priv,
1184 			    struct mwifiex_ds_get_signal *signal)
1185 {
1186 	int status;
1187 
1188 	signal->selector = ALL_RSSI_INFO_MASK;
1189 
1190 	/* Signal info can be obtained only if connected */
1191 	if (!priv->media_connected) {
1192 		dev_dbg(priv->adapter->dev,
1193 			"info: Can not get signal in disconnected state\n");
1194 		return -1;
1195 	}
1196 
1197 	status = mwifiex_send_cmd_sync(priv, HostCmd_CMD_RSSI_INFO,
1198 				       HostCmd_ACT_GEN_GET, 0, signal);
1199 
1200 	if (!status) {
1201 		if (signal->selector & BCN_RSSI_AVG_MASK)
1202 			priv->qual_level = signal->bcn_rssi_avg;
1203 		if (signal->selector & BCN_NF_AVG_MASK)
1204 			priv->qual_noise = signal->bcn_nf_avg;
1205 	}
1206 
1207 	return status;
1208 }
1209 
1210 /*
1211  * Sends IOCTL request to set encoding parameters.
1212  *
1213  * This function allocates the IOCTL request buffer, fills it
1214  * with requisite parameters and calls the IOCTL handler.
1215  */
mwifiex_set_encode(struct mwifiex_private * priv,const u8 * key,int key_len,u8 key_index,int disable)1216 int mwifiex_set_encode(struct mwifiex_private *priv, const u8 *key,
1217 			int key_len, u8 key_index, int disable)
1218 {
1219 	struct mwifiex_ds_encrypt_key encrypt_key;
1220 
1221 	memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
1222 	encrypt_key.key_len = key_len;
1223 	if (!disable) {
1224 		encrypt_key.key_index = key_index;
1225 		if (key_len)
1226 			memcpy(encrypt_key.key_material, key, key_len);
1227 	} else {
1228 		encrypt_key.key_disable = true;
1229 	}
1230 
1231 	return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1232 }
1233 
1234 /*
1235  * Sends IOCTL request to get extended version.
1236  *
1237  * This function allocates the IOCTL request buffer, fills it
1238  * with requisite parameters and calls the IOCTL handler.
1239  */
1240 int
mwifiex_get_ver_ext(struct mwifiex_private * priv)1241 mwifiex_get_ver_ext(struct mwifiex_private *priv)
1242 {
1243 	struct mwifiex_ver_ext ver_ext;
1244 
1245 	memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
1246 	if (mwifiex_send_cmd_sync(priv, HostCmd_CMD_VERSION_EXT,
1247 				  HostCmd_ACT_GEN_GET, 0, &ver_ext))
1248 		return -1;
1249 
1250 	return 0;
1251 }
1252 
1253 /*
1254  * Sends IOCTL request to get statistics information.
1255  *
1256  * This function allocates the IOCTL request buffer, fills it
1257  * with requisite parameters and calls the IOCTL handler.
1258  */
1259 int
mwifiex_get_stats_info(struct mwifiex_private * priv,struct mwifiex_ds_get_stats * log)1260 mwifiex_get_stats_info(struct mwifiex_private *priv,
1261 		       struct mwifiex_ds_get_stats *log)
1262 {
1263 	return mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_GET_LOG,
1264 				     HostCmd_ACT_GEN_GET, 0, log);
1265 }
1266 
1267 /*
1268  * IOCTL request handler to read/write register.
1269  *
1270  * This function prepares the correct firmware command and
1271  * issues it.
1272  *
1273  * Access to the following registers are supported -
1274  *      - MAC
1275  *      - BBP
1276  *      - RF
1277  *      - PMIC
1278  *      - CAU
1279  */
mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private * priv,struct mwifiex_ds_reg_rw * reg_rw,u16 action)1280 static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
1281 					struct mwifiex_ds_reg_rw *reg_rw,
1282 					u16 action)
1283 {
1284 	u16 cmd_no;
1285 
1286 	switch (le32_to_cpu(reg_rw->type)) {
1287 	case MWIFIEX_REG_MAC:
1288 		cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
1289 		break;
1290 	case MWIFIEX_REG_BBP:
1291 		cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
1292 		break;
1293 	case MWIFIEX_REG_RF:
1294 		cmd_no = HostCmd_CMD_RF_REG_ACCESS;
1295 		break;
1296 	case MWIFIEX_REG_PMIC:
1297 		cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
1298 		break;
1299 	case MWIFIEX_REG_CAU:
1300 		cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
1301 		break;
1302 	default:
1303 		return -1;
1304 	}
1305 
1306 	return mwifiex_send_cmd_sync(priv, cmd_no, action, 0, reg_rw);
1307 
1308 }
1309 
1310 /*
1311  * Sends IOCTL request to write to a register.
1312  *
1313  * This function allocates the IOCTL request buffer, fills it
1314  * with requisite parameters and calls the IOCTL handler.
1315  */
1316 int
mwifiex_reg_write(struct mwifiex_private * priv,u32 reg_type,u32 reg_offset,u32 reg_value)1317 mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
1318 		  u32 reg_offset, u32 reg_value)
1319 {
1320 	struct mwifiex_ds_reg_rw reg_rw;
1321 
1322 	reg_rw.type = cpu_to_le32(reg_type);
1323 	reg_rw.offset = cpu_to_le32(reg_offset);
1324 	reg_rw.value = cpu_to_le32(reg_value);
1325 
1326 	return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1327 }
1328 
1329 /*
1330  * Sends IOCTL request to read from a register.
1331  *
1332  * This function allocates the IOCTL request buffer, fills it
1333  * with requisite parameters and calls the IOCTL handler.
1334  */
1335 int
mwifiex_reg_read(struct mwifiex_private * priv,u32 reg_type,u32 reg_offset,u32 * value)1336 mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
1337 		 u32 reg_offset, u32 *value)
1338 {
1339 	int ret;
1340 	struct mwifiex_ds_reg_rw reg_rw;
1341 
1342 	reg_rw.type = cpu_to_le32(reg_type);
1343 	reg_rw.offset = cpu_to_le32(reg_offset);
1344 	ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1345 
1346 	if (ret)
1347 		goto done;
1348 
1349 	*value = le32_to_cpu(reg_rw.value);
1350 
1351 done:
1352 	return ret;
1353 }
1354 
1355 /*
1356  * Sends IOCTL request to read from EEPROM.
1357  *
1358  * This function allocates the IOCTL request buffer, fills it
1359  * with requisite parameters and calls the IOCTL handler.
1360  */
1361 int
mwifiex_eeprom_read(struct mwifiex_private * priv,u16 offset,u16 bytes,u8 * value)1362 mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
1363 		    u8 *value)
1364 {
1365 	int ret;
1366 	struct mwifiex_ds_read_eeprom rd_eeprom;
1367 
1368 	rd_eeprom.offset = cpu_to_le16((u16) offset);
1369 	rd_eeprom.byte_count = cpu_to_le16((u16) bytes);
1370 
1371 	/* Send request to firmware */
1372 	ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
1373 				    HostCmd_ACT_GEN_GET, 0, &rd_eeprom);
1374 
1375 	if (!ret)
1376 		memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
1377 	return ret;
1378 }
1379 
1380 /*
1381  * This function sets a generic IE. In addition to generic IE, it can
1382  * also handle WPA, WPA2 and WAPI IEs.
1383  */
1384 static int
mwifiex_set_gen_ie_helper(struct mwifiex_private * priv,u8 * ie_data_ptr,u16 ie_len)1385 mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
1386 			  u16 ie_len)
1387 {
1388 	int ret = 0;
1389 	struct ieee_types_vendor_header *pvendor_ie;
1390 	const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
1391 	const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
1392 
1393 	/* If the passed length is zero, reset the buffer */
1394 	if (!ie_len) {
1395 		priv->gen_ie_buf_len = 0;
1396 		priv->wps.session_enable = false;
1397 
1398 		return 0;
1399 	} else if (!ie_data_ptr) {
1400 		return -1;
1401 	}
1402 	pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1403 	/* Test to see if it is a WPA IE, if not, then it is a gen IE */
1404 	if (((pvendor_ie->element_id == WLAN_EID_WPA) &&
1405 	     (!memcmp(pvendor_ie->oui, wpa_oui, sizeof(wpa_oui)))) ||
1406 	    (pvendor_ie->element_id == WLAN_EID_RSN)) {
1407 
1408 		/* IE is a WPA/WPA2 IE so call set_wpa function */
1409 		ret = mwifiex_set_wpa_ie_helper(priv, ie_data_ptr, ie_len);
1410 		priv->wps.session_enable = false;
1411 
1412 		return ret;
1413 	} else if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1414 		/* IE is a WAPI IE so call set_wapi function */
1415 		ret = mwifiex_set_wapi_ie(priv, ie_data_ptr, ie_len);
1416 
1417 		return ret;
1418 	}
1419 	/*
1420 	 * Verify that the passed length is not larger than the
1421 	 * available space remaining in the buffer
1422 	 */
1423 	if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {
1424 
1425 		/* Test to see if it is a WPS IE, if so, enable
1426 		 * wps session flag
1427 		 */
1428 		pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;
1429 		if ((pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) &&
1430 		    (!memcmp(pvendor_ie->oui, wps_oui, sizeof(wps_oui)))) {
1431 			priv->wps.session_enable = true;
1432 			dev_dbg(priv->adapter->dev,
1433 				"info: WPS Session Enabled.\n");
1434 		}
1435 
1436 		/* Append the passed data to the end of the
1437 		   genIeBuffer */
1438 		memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
1439 		       ie_len);
1440 		/* Increment the stored buffer length by the
1441 		   size passed */
1442 		priv->gen_ie_buf_len += ie_len;
1443 	} else {
1444 		/* Passed data does not fit in the remaining
1445 		   buffer space */
1446 		ret = -1;
1447 	}
1448 
1449 	/* Return 0, or -1 for error case */
1450 	return ret;
1451 }
1452 
1453 /*
1454  * IOCTL request handler to set/get generic IE.
1455  *
1456  * In addition to various generic IEs, this function can also be
1457  * used to set the ARP filter.
1458  */
mwifiex_misc_ioctl_gen_ie(struct mwifiex_private * priv,struct mwifiex_ds_misc_gen_ie * gen_ie,u16 action)1459 static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
1460 				     struct mwifiex_ds_misc_gen_ie *gen_ie,
1461 				     u16 action)
1462 {
1463 	struct mwifiex_adapter *adapter = priv->adapter;
1464 
1465 	switch (gen_ie->type) {
1466 	case MWIFIEX_IE_TYPE_GEN_IE:
1467 		if (action == HostCmd_ACT_GEN_GET) {
1468 			gen_ie->len = priv->wpa_ie_len;
1469 			memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
1470 		} else {
1471 			mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
1472 						  (u16) gen_ie->len);
1473 		}
1474 		break;
1475 	case MWIFIEX_IE_TYPE_ARP_FILTER:
1476 		memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
1477 		if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
1478 			adapter->arp_filter_size = 0;
1479 			dev_err(adapter->dev, "invalid ARP filter size\n");
1480 			return -1;
1481 		} else {
1482 			memcpy(adapter->arp_filter, gen_ie->ie_data,
1483 			       gen_ie->len);
1484 			adapter->arp_filter_size = gen_ie->len;
1485 		}
1486 		break;
1487 	default:
1488 		dev_err(adapter->dev, "invalid IE type\n");
1489 		return -1;
1490 	}
1491 	return 0;
1492 }
1493 
1494 /*
1495  * Sends IOCTL request to set a generic IE.
1496  *
1497  * This function allocates the IOCTL request buffer, fills it
1498  * with requisite parameters and calls the IOCTL handler.
1499  */
1500 int
mwifiex_set_gen_ie(struct mwifiex_private * priv,u8 * ie,int ie_len)1501 mwifiex_set_gen_ie(struct mwifiex_private *priv, u8 *ie, int ie_len)
1502 {
1503 	struct mwifiex_ds_misc_gen_ie gen_ie;
1504 
1505 	if (ie_len > IEEE_MAX_IE_SIZE)
1506 		return -EFAULT;
1507 
1508 	gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
1509 	gen_ie.len = ie_len;
1510 	memcpy(gen_ie.ie_data, ie, ie_len);
1511 	if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1512 		return -EFAULT;
1513 
1514 	return 0;
1515 }
1516