sta_ioctl.c 38.5 KB
Newer Older
1 2 3
/*
 * Marvell Wireless LAN device driver: functions for station ioctl
 *
X
Xinming Hu 已提交
4
 * Copyright (C) 2011-2014, Marvell International Ltd.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This software file (the "File") is distributed by Marvell International
 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
 * (the "License").  You may use, redistribute and/or modify this File in
 * accordance with the terms and conditions of the License, a copy of which
 * is available by writing to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
 *
 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
 * this warranty disclaimer.
 */

#include "decl.h"
#include "ioctl.h"
#include "util.h"
#include "fw.h"
#include "main.h"
#include "wmm.h"
#include "11n.h"
#include "cfg80211.h"

29
static int disconnect_on_suspend;
30 31
module_param(disconnect_on_suspend, int, 0644);

32 33 34 35 36 37 38
/*
 * Copies the multicast address list from device to driver.
 *
 * This function does not validate the destination memory for
 * size, and the calling function must ensure enough memory is
 * available.
 */
39 40
int mwifiex_copy_mcast_addr(struct mwifiex_multicast_list *mlist,
			    struct net_device *dev)
41 42 43 44 45 46 47 48 49 50 51 52 53
{
	int i = 0;
	struct netdev_hw_addr *ha;

	netdev_for_each_mc_addr(ha, dev)
		memcpy(&mlist->mac_list[i++], ha->addr, ETH_ALEN);

	return i;
}

/*
 * Wait queue completion handler.
 *
54 55
 * This function waits on a cmd wait queue. It also cancels the pending
 * request after waking up, in case of errors.
56
 */
57 58
int mwifiex_wait_queue_complete(struct mwifiex_adapter *adapter,
				struct cmd_ctrl_node *cmd_queued)
59
{
60
	int status;
61

62
	/* Wait for completion */
63 64 65 66
	status = wait_event_interruptible_timeout(adapter->cmd_wait_q.wait,
						  *(cmd_queued->condition),
						  (12 * HZ));
	if (status <= 0) {
67 68
		if (status == 0)
			status = -ETIMEDOUT;
69 70
		mwifiex_dbg(adapter, ERROR, "cmd_wait_q terminated: %d\n",
			    status);
71
		mwifiex_cancel_all_pending_cmd(adapter);
72
		return status;
73
	}
74 75

	status = adapter->cmd_wait_q.status;
76
	adapter->cmd_wait_q.status = 0;
77 78 79 80 81 82 83 84 85 86 87

	return status;
}

/*
 * This function prepares the correct firmware command and
 * issues it to set the multicast list.
 *
 * This function can be used to enable promiscuous mode, or enable all
 * multicast packets, or to enable selective multicast.
 */
88 89
int mwifiex_request_set_multicast_list(struct mwifiex_private *priv,
				struct mwifiex_multicast_list *mcast_list)
90 91 92 93 94 95 96
{
	int ret = 0;
	u16 old_pkt_filter;

	old_pkt_filter = priv->curr_pkt_filter;

	if (mcast_list->mode == MWIFIEX_PROMISC_MODE) {
97 98
		mwifiex_dbg(priv->adapter, INFO,
			    "info: Enable Promiscuous mode\n");
99 100 101 102 103 104
		priv->curr_pkt_filter |= HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
		priv->curr_pkt_filter &=
			~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
	} else {
		/* Multicast */
		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_PROMISCUOUS_ENABLE;
105
		if (mcast_list->mode == MWIFIEX_ALL_MULTI_MODE) {
106 107
			mwifiex_dbg(priv->adapter, INFO,
				    "info: Enabling All Multicast!\n");
108 109 110 111 112
			priv->curr_pkt_filter |=
				HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
		} else {
			priv->curr_pkt_filter &=
				~HostCmd_ACT_MAC_ALL_MULTICAST_ENABLE;
113 114 115
			mwifiex_dbg(priv->adapter, INFO,
				    "info: Set multicast list=%d\n",
				    mcast_list->num_multicast_addr);
116
			/* Send multicast addresses to firmware */
117 118 119 120
			ret = mwifiex_send_cmd(priv,
					       HostCmd_CMD_MAC_MULTICAST_ADR,
					       HostCmd_ACT_GEN_SET, 0,
					       mcast_list, false);
121 122
		}
	}
123 124 125
	mwifiex_dbg(priv->adapter, INFO,
		    "info: old_pkt_filter=%#x, curr_pkt_filter=%#x\n",
		    old_pkt_filter, priv->curr_pkt_filter);
126
	if (old_pkt_filter != priv->curr_pkt_filter) {
127 128 129
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
				       HostCmd_ACT_GEN_SET,
				       0, &priv->curr_pkt_filter, false);
130 131 132 133 134
	}

	return ret;
}

135 136 137
/*
 * This function fills bss descriptor structure using provided
 * information.
138 139
 * beacon_ie buffer is allocated in this function. It is caller's
 * responsibility to free the memory.
140 141
 */
int mwifiex_fill_new_bss_desc(struct mwifiex_private *priv,
142
			      struct cfg80211_bss *bss,
143 144
			      struct mwifiex_bssdescriptor *bss_desc)
{
145
	u8 *beacon_ie;
146
	size_t beacon_ie_len;
147
	struct mwifiex_bss_priv *bss_priv = (void *)bss->priv;
148 149 150 151 152 153
	const struct cfg80211_bss_ies *ies;

	rcu_read_lock();
	ies = rcu_dereference(bss->ies);
	beacon_ie = kmemdup(ies->data, ies->len, GFP_ATOMIC);
	beacon_ie_len = ies->len;
J
Johannes Berg 已提交
154
	bss_desc->timestamp = ies->tsf;
155
	rcu_read_unlock();
156

157
	if (!beacon_ie) {
158 159
		mwifiex_dbg(priv->adapter, ERROR,
			    " failed to alloc beacon_ie\n");
160 161 162 163 164
		return -ENOMEM;
	}

	memcpy(bss_desc->mac_address, bss->bssid, ETH_ALEN);
	bss_desc->rssi = bss->signal;
165
	/* The caller of this function will free beacon_ie */
166
	bss_desc->beacon_buf = beacon_ie;
167
	bss_desc->beacon_buf_size = beacon_ie_len;
168 169
	bss_desc->beacon_period = bss->beacon_interval;
	bss_desc->cap_info_bitmap = bss->capability;
170 171
	bss_desc->bss_band = bss_priv->band;
	bss_desc->fw_tsf = bss_priv->fw_tsf;
172
	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_PRIVACY) {
173 174
		mwifiex_dbg(priv->adapter, INFO,
			    "info: InterpretIE: AP WEP enabled\n");
175 176 177 178 179 180 181 182 183
		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
	} else {
		bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
	}
	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_IBSS)
		bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
	else
		bss_desc->bss_mode = NL80211_IFTYPE_STATION;

184 185 186 187 188
	/* Disable 11ac by default. Enable it only where there
	 * exist VHT_CAP IE in AP beacon
	 */
	bss_desc->disable_11ac = true;

189 190 191
	if (bss_desc->cap_info_bitmap & WLAN_CAPABILITY_SPECTRUM_MGMT)
		bss_desc->sensed_11h = true;

192
	return mwifiex_update_bss_desc_with_ie(priv->adapter, bss_desc);
193 194
}

195 196 197 198 199 200 201 202 203 204
void mwifiex_dnld_txpwr_table(struct mwifiex_private *priv)
{
	if (priv->adapter->dt_node) {
		char txpwr[] = {"marvell,00_txpwrlimit"};

		memcpy(&txpwr[8], priv->adapter->country_code, 2);
		mwifiex_dnld_dt_cfgdata(priv, priv->adapter->dt_node, txpwr);
	}
}

205 206 207
static int mwifiex_process_country_ie(struct mwifiex_private *priv,
				      struct cfg80211_bss *bss)
{
208 209
	const u8 *country_ie;
	u8 country_ie_len;
210 211 212
	struct mwifiex_802_11d_domain_reg *domain_info =
					&priv->adapter->domain_reg;

213 214 215 216
	rcu_read_lock();
	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
	if (!country_ie) {
		rcu_read_unlock();
217
		return 0;
218
	}
219 220

	country_ie_len = country_ie[1];
221 222
	if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) {
		rcu_read_unlock();
223
		return 0;
224
	}
225

226 227
	if (!strncmp(priv->adapter->country_code, &country_ie[2], 2)) {
		rcu_read_unlock();
228 229
		mwifiex_dbg(priv->adapter, INFO,
			    "11D: skip setting domain info in FW\n");
230 231 232 233
		return 0;
	}
	memcpy(priv->adapter->country_code, &country_ie[2], 2);

234 235 236 237 238 239 240 241 242 243 244 245
	domain_info->country_code[0] = country_ie[2];
	domain_info->country_code[1] = country_ie[3];
	domain_info->country_code[2] = ' ';

	country_ie_len -= IEEE80211_COUNTRY_STRING_LEN;

	domain_info->no_of_triplet =
		country_ie_len / sizeof(struct ieee80211_country_ie_triplet);

	memcpy((u8 *)domain_info->triplet,
	       &country_ie[2] + IEEE80211_COUNTRY_STRING_LEN, country_ie_len);

246 247
	rcu_read_unlock();

248 249
	if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11D_DOMAIN_INFO,
			     HostCmd_ACT_GEN_SET, 0, NULL, false)) {
250 251
		mwifiex_dbg(priv->adapter, ERROR,
			    "11D: setting domain info in FW fail\n");
252 253 254
		return -1;
	}

255
	mwifiex_dnld_txpwr_table(priv);
256

257 258 259
	return 0;
}

260 261 262 263 264
/*
 * In Ad-Hoc mode, the IBSS is created if not found in scan list.
 * In both Ad-Hoc and infra mode, an deauthentication is performed
 * first.
 */
265
int mwifiex_bss_start(struct mwifiex_private *priv, struct cfg80211_bss *bss,
266
		      struct cfg80211_ssid *req_ssid)
267
{
268
	int ret;
269
	struct mwifiex_adapter *adapter = priv->adapter;
270
	struct mwifiex_bssdescriptor *bss_desc = NULL;
271 272

	priv->scan_block = false;
273 274

	if (bss) {
275 276
		if (adapter->region_code == 0x00)
			mwifiex_process_country_ie(priv, bss);
277

278 279
		/* Allocate and fill new bss descriptor */
		bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
280 281
				   GFP_KERNEL);
		if (!bss_desc)
282
			return -ENOMEM;
283

284
		ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
285 286 287
		if (ret)
			goto done;
	}
288

289 290
	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
	    priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT) {
291 292 293 294
		u8 config_bands;

		if (!bss_desc)
			return -1;
295

296
		if (mwifiex_band_to_radio_type(bss_desc->bss_band) ==
297
						HostCmd_SCAN_RADIO_TYPE_BG) {
298
			config_bands = BAND_B | BAND_G | BAND_GN;
299 300 301 302 303
		} else {
			config_bands = BAND_A | BAND_AN;
			if (adapter->fw_bands & BAND_AAC)
				config_bands |= BAND_AAC;
		}
304

305 306
		if (!((config_bands | adapter->fw_bands) & ~adapter->fw_bands))
			adapter->config_bands = config_bands;
307

308 309 310
		ret = mwifiex_check_network_compatibility(priv, bss_desc);
		if (ret)
			goto done;
311

312 313
		if (mwifiex_11h_get_csa_closed_channel(priv) ==
							(u8)bss_desc->channel) {
314 315 316
			mwifiex_dbg(adapter, ERROR,
				    "Attempt to reconnect on csa closed chan(%d)\n",
				    bss_desc->channel);
317 318 319
			goto done;
		}

320 321 322
		mwifiex_dbg(adapter, INFO,
			    "info: SSID found in scan list ...\t"
			    "associating...\n");
323

A
Avinash Patil 已提交
324
		mwifiex_stop_net_dev_queue(priv->netdev, adapter);
325 326
		if (netif_carrier_ok(priv->netdev))
			netif_carrier_off(priv->netdev);
327 328 329 330

		/* Clear any past association response stored for
		 * application retrieval */
		priv->assoc_rsp_size = 0;
331
		ret = mwifiex_associate(priv, bss_desc);
332 333 334 335 336 337 338 339 340 341 342

		/* If auth type is auto and association fails using open mode,
		 * try to connect using shared mode */
		if (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
		    priv->sec_info.is_authtype_auto &&
		    priv->sec_info.wep_enabled) {
			priv->sec_info.authentication_mode =
						NL80211_AUTHTYPE_SHARED_KEY;
			ret = mwifiex_associate(priv, bss_desc);
		}

343
		if (bss)
344
			cfg80211_put_bss(priv->adapter->wiphy, bss);
345 346 347
	} else {
		/* Adhoc mode */
		/* If the requested SSID matches current SSID, return */
348
		if (bss_desc && bss_desc->ssid.ssid_len &&
349 350
		    (!mwifiex_ssid_cmp(&priv->curr_bss_params.bss_descriptor.
				       ssid, &bss_desc->ssid))) {
351 352
			ret = 0;
			goto done;
353
		}
354 355 356

		priv->adhoc_is_link_sensed = false;

357 358
		ret = mwifiex_check_network_compatibility(priv, bss_desc);

A
Avinash Patil 已提交
359
		mwifiex_stop_net_dev_queue(priv->netdev, adapter);
360 361
		if (netif_carrier_ok(priv->netdev))
			netif_carrier_off(priv->netdev);
362 363

		if (!ret) {
364 365 366
			mwifiex_dbg(adapter, INFO,
				    "info: network found in scan\t"
				    " list. Joining...\n");
367 368
			ret = mwifiex_adhoc_join(priv, bss_desc);
			if (bss)
369
				cfg80211_put_bss(priv->adapter->wiphy, bss);
370
		} else {
371 372 373 374
			mwifiex_dbg(adapter, INFO,
				    "info: Network not found in\t"
				    "the list, creating adhoc with ssid = %s\n",
				    req_ssid->ssid);
375
			ret = mwifiex_adhoc_start(priv, req_ssid);
376 377 378
		}
	}

379
done:
380 381 382 383 384
	/* beacon_ie buffer was allocated in function
	 * mwifiex_fill_new_bss_desc(). Free it now.
	 */
	if (bss_desc)
		kfree(bss_desc->beacon_buf);
385
	kfree(bss_desc);
386 387 388 389 390 391 392 393 394
	return ret;
}

/*
 * IOCTL request handler to set host sleep configuration.
 *
 * This function prepares the correct firmware command and
 * issues it.
 */
X
Xinming Hu 已提交
395 396
int mwifiex_set_hs_params(struct mwifiex_private *priv, u16 action,
			  int cmd_type, struct mwifiex_ds_hs_cfg *hs_cfg)
397

398 399 400 401 402
{
	struct mwifiex_adapter *adapter = priv->adapter;
	int status = 0;
	u32 prev_cond = 0;

403 404 405
	if (!hs_cfg)
		return -ENOMEM;

406 407 408
	switch (action) {
	case HostCmd_ACT_GEN_SET:
		if (adapter->pps_uapsd_mode) {
409 410 411
			mwifiex_dbg(adapter, INFO,
				    "info: Host Sleep IOCTL\t"
				    "is blocked in UAPSD/PPS mode\n");
412 413 414 415
			status = -1;
			break;
		}
		if (hs_cfg->is_invoke_hostcmd) {
416
			if (hs_cfg->conditions == HS_CFG_CANCEL) {
417 418 419 420 421 422 423 424 425 426 427 428 429 430
				if (!adapter->is_hs_configured)
					/* Already cancelled */
					break;
				/* Save previous condition */
				prev_cond = le32_to_cpu(adapter->hs_cfg
							.conditions);
				adapter->hs_cfg.conditions =
						cpu_to_le32(hs_cfg->conditions);
			} else if (hs_cfg->conditions) {
				adapter->hs_cfg.conditions =
						cpu_to_le32(hs_cfg->conditions);
				adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
				if (hs_cfg->gap)
					adapter->hs_cfg.gap = (u8)hs_cfg->gap;
431 432
			} else if (adapter->hs_cfg.conditions ==
				   cpu_to_le32(HS_CFG_CANCEL)) {
433 434 435 436 437
				/* Return failure if no parameters for HS
				   enable */
				status = -1;
				break;
			}
438 439 440 441 442 443 444

			status = mwifiex_send_cmd(priv,
						  HostCmd_CMD_802_11_HS_CFG_ENH,
						  HostCmd_ACT_GEN_SET, 0,
						  &adapter->hs_cfg,
						  cmd_type == MWIFIEX_SYNC_CMD);

445
			if (hs_cfg->conditions == HS_CFG_CANCEL)
446 447 448 449 450
				/* Restore previous condition */
				adapter->hs_cfg.conditions =
						cpu_to_le32(prev_cond);
		} else {
			adapter->hs_cfg.conditions =
451
						cpu_to_le32(hs_cfg->conditions);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
			adapter->hs_cfg.gpio = (u8)hs_cfg->gpio;
			adapter->hs_cfg.gap = (u8)hs_cfg->gap;
		}
		break;
	case HostCmd_ACT_GEN_GET:
		hs_cfg->conditions = le32_to_cpu(adapter->hs_cfg.conditions);
		hs_cfg->gpio = adapter->hs_cfg.gpio;
		hs_cfg->gap = adapter->hs_cfg.gap;
		break;
	default:
		status = -1;
		break;
	}

	return status;
}

/*
 * Sends IOCTL request to cancel the existing Host Sleep configuration.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
475
int mwifiex_cancel_hs(struct mwifiex_private *priv, int cmd_type)
476 477 478
{
	struct mwifiex_ds_hs_cfg hscfg;

479
	hscfg.conditions = HS_CFG_CANCEL;
480 481
	hscfg.is_invoke_hostcmd = true;

482 483
	return mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
				    cmd_type, &hscfg);
484 485 486 487 488 489 490 491 492 493 494 495
}
EXPORT_SYMBOL_GPL(mwifiex_cancel_hs);

/*
 * Sends IOCTL request to cancel the existing Host Sleep configuration.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int mwifiex_enable_hs(struct mwifiex_adapter *adapter)
{
	struct mwifiex_ds_hs_cfg hscfg;
496 497 498 499 500 501 502 503 504 505
	struct mwifiex_private *priv;
	int i;

	if (disconnect_on_suspend) {
		for (i = 0; i < adapter->priv_num; i++) {
			priv = adapter->priv[i];
			if (priv)
				mwifiex_deauthenticate(priv, NULL);
		}
	}
506 507

	if (adapter->hs_activated) {
508 509
		mwifiex_dbg(adapter, CMD,
			    "cmd: HS Already activated\n");
510 511 512 513 514
		return true;
	}

	adapter->hs_activate_wait_q_woken = false;

515
	memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
516 517
	hscfg.is_invoke_hostcmd = true;

518 519 520
	adapter->hs_enabling = true;
	mwifiex_cancel_all_pending_cmd(adapter);

521
	if (mwifiex_set_hs_params(mwifiex_get_priv(adapter,
522
						   MWIFIEX_BSS_ROLE_STA),
523 524
				  HostCmd_ACT_GEN_SET, MWIFIEX_SYNC_CMD,
				  &hscfg)) {
525 526
		mwifiex_dbg(adapter, ERROR,
			    "IOCTL request HS enable failed\n");
527 528 529
		return false;
	}

530 531 532
	if (wait_event_interruptible_timeout(adapter->hs_activate_wait_q,
					     adapter->hs_activate_wait_q_woken,
					     (10 * HZ)) <= 0) {
533 534
		mwifiex_dbg(adapter, ERROR,
			    "hs_activate_wait_q terminated\n");
535 536
		return false;
	}
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

	return true;
}
EXPORT_SYMBOL_GPL(mwifiex_enable_hs);

/*
 * IOCTL request handler to get BSS information.
 *
 * This function collates the information from different driver structures
 * to send to the user.
 */
int mwifiex_get_bss_info(struct mwifiex_private *priv,
			 struct mwifiex_bss_info *info)
{
	struct mwifiex_adapter *adapter = priv->adapter;
	struct mwifiex_bssdescriptor *bss_desc;

	if (!info)
		return -1;

	bss_desc = &priv->curr_bss_params.bss_descriptor;

	info->bss_mode = priv->bss_mode;

561
	memcpy(&info->ssid, &bss_desc->ssid, sizeof(struct cfg80211_ssid));
562 563 564 565 566

	memcpy(&info->bssid, &bss_desc->mac_address, ETH_ALEN);

	info->bss_chan = bss_desc->channel;

567
	memcpy(info->country_code, adapter->country_code,
568
	       IEEE80211_COUNTRY_STRING_LEN);
569 570 571 572 573 574 575 576 577 578

	info->media_connected = priv->media_connected;

	info->max_power_level = priv->max_tx_power_level;
	info->min_power_level = priv->min_tx_power_level;

	info->adhoc_state = priv->adhoc_state;

	info->bcn_nf_last = priv->bcn_nf_last;

579
	if (priv->sec_info.wep_enabled)
580 581 582 583 584 585 586 587 588 589
		info->wep_status = true;
	else
		info->wep_status = false;

	info->is_hs_configured = adapter->is_hs_configured;
	info->is_deep_sleep = adapter->is_deep_sleep;

	return 0;
}

590 591 592 593 594 595 596 597 598
/*
 * The function disables auto deep sleep mode.
 */
int mwifiex_disable_auto_ds(struct mwifiex_private *priv)
{
	struct mwifiex_ds_auto_ds auto_ds;

	auto_ds.auto_ds = DEEP_SLEEP_OFF;

599 600
	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
				DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds, true);
601 602 603
}
EXPORT_SYMBOL_GPL(mwifiex_disable_auto_ds);

604 605 606 607 608 609
/*
 * Sends IOCTL request to get the data rate.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
610
int mwifiex_drv_get_data_rate(struct mwifiex_private *priv, u32 *rate)
611
{
612
	int ret;
613

614 615
	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
			       HostCmd_ACT_GEN_GET, 0, NULL, true);
616 617

	if (!ret) {
618 619 620
		if (priv->is_data_rate_auto)
			*rate = mwifiex_index_to_data_rate(priv, priv->tx_rate,
							   priv->tx_htinfo);
621
		else
622
			*rate = priv->data_rate;
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	}

	return ret;
}

/*
 * IOCTL request handler to set tx power configuration.
 *
 * This function prepares the correct firmware command and
 * issues it.
 *
 * For non-auto power mode, all the following power groups are set -
 *      - Modulation class HR/DSSS
 *      - Modulation class OFDM
 *      - Modulation class HTBW20
 *      - Modulation class HTBW40
 */
640 641
int mwifiex_set_tx_power(struct mwifiex_private *priv,
			 struct mwifiex_power_cfg *power_cfg)
642
{
643 644 645 646 647
	int ret;
	struct host_cmd_ds_txpwr_cfg *txp_cfg;
	struct mwifiex_types_power_group *pg_tlv;
	struct mwifiex_power_group *pg;
	u8 *buf;
648 649 650 651 652 653
	u16 dbm = 0;

	if (!power_cfg->is_power_auto) {
		dbm = (u16) power_cfg->power_level;
		if ((dbm < priv->min_tx_power_level) ||
		    (dbm > priv->max_tx_power_level)) {
654 655 656 657 658
			mwifiex_dbg(priv->adapter, ERROR,
				    "txpower value %d dBm\t"
				    "is out of range (%d dBm-%d dBm)\n",
				    dbm, priv->min_tx_power_level,
				    priv->max_tx_power_level);
659 660 661 662
			return -1;
		}
	}
	buf = kzalloc(MWIFIEX_SIZE_OF_CMD_BUFFER, GFP_KERNEL);
663
	if (!buf)
664
		return -ENOMEM;
665 666 667 668 669

	txp_cfg = (struct host_cmd_ds_txpwr_cfg *) buf;
	txp_cfg->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
	if (!power_cfg->is_power_auto) {
		txp_cfg->mode = cpu_to_le32(1);
670 671
		pg_tlv = (struct mwifiex_types_power_group *)
			 (buf + sizeof(struct host_cmd_ds_txpwr_cfg));
672 673 674
		pg_tlv->type = cpu_to_le16(TLV_TYPE_POWER_GROUP);
		pg_tlv->length =
			cpu_to_le16(4 * sizeof(struct mwifiex_power_group));
675 676 677
		pg = (struct mwifiex_power_group *)
		     (buf + sizeof(struct host_cmd_ds_txpwr_cfg)
		      + sizeof(struct mwifiex_types_power_group));
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
		/* Power group for modulation class HR/DSSS */
		pg->first_rate_code = 0x00;
		pg->last_rate_code = 0x03;
		pg->modulation_class = MOD_CLASS_HR_DSSS;
		pg->power_step = 0;
		pg->power_min = (s8) dbm;
		pg->power_max = (s8) dbm;
		pg++;
		/* Power group for modulation class OFDM */
		pg->first_rate_code = 0x00;
		pg->last_rate_code = 0x07;
		pg->modulation_class = MOD_CLASS_OFDM;
		pg->power_step = 0;
		pg->power_min = (s8) dbm;
		pg->power_max = (s8) dbm;
		pg++;
		/* Power group for modulation class HTBW20 */
		pg->first_rate_code = 0x00;
		pg->last_rate_code = 0x20;
		pg->modulation_class = MOD_CLASS_HT;
		pg->power_step = 0;
		pg->power_min = (s8) dbm;
		pg->power_max = (s8) dbm;
		pg->ht_bandwidth = HT_BW_20;
		pg++;
		/* Power group for modulation class HTBW40 */
		pg->first_rate_code = 0x00;
		pg->last_rate_code = 0x20;
		pg->modulation_class = MOD_CLASS_HT;
		pg->power_step = 0;
		pg->power_min = (s8) dbm;
		pg->power_max = (s8) dbm;
		pg->ht_bandwidth = HT_BW_40;
	}
712 713
	ret = mwifiex_send_cmd(priv, HostCmd_CMD_TXPWR_CFG,
			       HostCmd_ACT_GEN_SET, 0, buf, true);
714

715
	kfree(buf);
716 717 718 719 720 721 722 723 724
	return ret;
}

/*
 * IOCTL request handler to get power save mode.
 *
 * This function prepares the correct firmware command and
 * issues it.
 */
725
int mwifiex_drv_set_power(struct mwifiex_private *priv, u32 *ps_mode)
726
{
727
	int ret;
728 729 730
	struct mwifiex_adapter *adapter = priv->adapter;
	u16 sub_cmd;

731 732 733 734 735
	if (*ps_mode)
		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
	else
		adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
	sub_cmd = (*ps_mode) ? EN_AUTO_PS : DIS_AUTO_PS;
736 737
	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
			       sub_cmd, BITMAP_STA_PS, NULL, true);
738
	if ((!ret) && (sub_cmd == DIS_AUTO_PS))
739 740
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
				       GET_PS, 0, NULL, false);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756

	return ret;
}

/*
 * IOCTL request handler to set/reset WPA IE.
 *
 * The supplied WPA IE is treated as a opaque buffer. Only the first field
 * is checked to determine WPA version. If buffer length is zero, the existing
 * WPA IE is reset.
 */
static int mwifiex_set_wpa_ie_helper(struct mwifiex_private *priv,
				     u8 *ie_data_ptr, u16 ie_len)
{
	if (ie_len) {
		if (ie_len > sizeof(priv->wpa_ie)) {
757 758
			mwifiex_dbg(priv->adapter, ERROR,
				    "failed to copy WPA IE, too big\n");
759 760 761
			return -1;
		}
		memcpy(priv->wpa_ie, ie_data_ptr, ie_len);
762
		priv->wpa_ie_len = ie_len;
763 764 765
		mwifiex_dbg(priv->adapter, CMD,
			    "cmd: Set Wpa_ie_len=%d IE=%#x\n",
			    priv->wpa_ie_len, priv->wpa_ie[0]);
766

767
		if (priv->wpa_ie[0] == WLAN_EID_VENDOR_SPECIFIC) {
768 769 770 771 772 773 774 775 776 777
			priv->sec_info.wpa_enabled = true;
		} else if (priv->wpa_ie[0] == WLAN_EID_RSN) {
			priv->sec_info.wpa2_enabled = true;
		} else {
			priv->sec_info.wpa_enabled = false;
			priv->sec_info.wpa2_enabled = false;
		}
	} else {
		memset(priv->wpa_ie, 0, sizeof(priv->wpa_ie));
		priv->wpa_ie_len = 0;
778 779 780
		mwifiex_dbg(priv->adapter, INFO,
			    "info: reset wpa_ie_len=%d IE=%#x\n",
			    priv->wpa_ie_len, priv->wpa_ie[0]);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
		priv->sec_info.wpa_enabled = false;
		priv->sec_info.wpa2_enabled = false;
	}

	return 0;
}

/*
 * IOCTL request handler to set/reset WAPI IE.
 *
 * The supplied WAPI IE is treated as a opaque buffer. Only the first field
 * is checked to internally enable WAPI. If buffer length is zero, the existing
 * WAPI IE is reset.
 */
static int mwifiex_set_wapi_ie(struct mwifiex_private *priv,
			       u8 *ie_data_ptr, u16 ie_len)
{
	if (ie_len) {
		if (ie_len > sizeof(priv->wapi_ie)) {
800 801
			mwifiex_dbg(priv->adapter, ERROR,
				    "info: failed to copy WAPI IE, too big\n");
802 803 804 805
			return -1;
		}
		memcpy(priv->wapi_ie, ie_data_ptr, ie_len);
		priv->wapi_ie_len = ie_len;
806 807 808
		mwifiex_dbg(priv->adapter, CMD,
			    "cmd: Set wapi_ie_len=%d IE=%#x\n",
			    priv->wapi_ie_len, priv->wapi_ie[0]);
809 810 811 812 813 814

		if (priv->wapi_ie[0] == WLAN_EID_BSS_AC_ACCESS_DELAY)
			priv->sec_info.wapi_enabled = true;
	} else {
		memset(priv->wapi_ie, 0, sizeof(priv->wapi_ie));
		priv->wapi_ie_len = ie_len;
815 816 817
		mwifiex_dbg(priv->adapter, INFO,
			    "info: Reset wapi_ie_len=%d IE=%#x\n",
			    priv->wapi_ie_len, priv->wapi_ie[0]);
818 819 820 821 822
		priv->sec_info.wapi_enabled = false;
	}
	return 0;
}

A
Avinash Patil 已提交
823 824 825 826 827 828 829 830 831 832 833
/*
 * IOCTL request handler to set/reset WPS IE.
 *
 * The supplied WPS IE is treated as a opaque buffer. Only the first field
 * is checked to internally enable WPS. If buffer length is zero, the existing
 * WPS IE is reset.
 */
static int mwifiex_set_wps_ie(struct mwifiex_private *priv,
			       u8 *ie_data_ptr, u16 ie_len)
{
	if (ie_len) {
834
		if (ie_len > MWIFIEX_MAX_VSIE_LEN) {
835 836
			mwifiex_dbg(priv->adapter, ERROR,
				    "info: failed to copy WPS IE, too big\n");
A
Avinash Patil 已提交
837 838
			return -1;
		}
839 840 841 842 843

		priv->wps_ie = kzalloc(MWIFIEX_MAX_VSIE_LEN, GFP_KERNEL);
		if (!priv->wps_ie)
			return -ENOMEM;

A
Avinash Patil 已提交
844 845
		memcpy(priv->wps_ie, ie_data_ptr, ie_len);
		priv->wps_ie_len = ie_len;
846 847 848
		mwifiex_dbg(priv->adapter, CMD,
			    "cmd: Set wps_ie_len=%d IE=%#x\n",
			    priv->wps_ie_len, priv->wps_ie[0]);
A
Avinash Patil 已提交
849 850 851
	} else {
		kfree(priv->wps_ie);
		priv->wps_ie_len = ie_len;
852 853
		mwifiex_dbg(priv->adapter, INFO,
			    "info: Reset wps_ie_len=%d\n", priv->wps_ie_len);
A
Avinash Patil 已提交
854 855 856 857
	}
	return 0;
}

858 859 860 861 862 863
/*
 * IOCTL request handler to set WAPI key.
 *
 * This function prepares the correct firmware command and
 * issues it.
 */
864
static int mwifiex_sec_ioctl_set_wapi_key(struct mwifiex_private *priv,
865 866 867
			       struct mwifiex_ds_encrypt_key *encrypt_key)
{

868 869 870
	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
				HostCmd_ACT_GEN_SET, KEY_INFO_ENABLED,
				encrypt_key, true);
871 872 873 874 875 876 877 878
}

/*
 * IOCTL request handler to set WEP network key.
 *
 * This function prepares the correct firmware command and
 * issues it, after validation checks.
 */
879
static int mwifiex_sec_ioctl_set_wep_key(struct mwifiex_private *priv,
880 881
			      struct mwifiex_ds_encrypt_key *encrypt_key)
{
882
	struct mwifiex_adapter *adapter = priv->adapter;
883 884
	int ret;
	struct mwifiex_wep_key *wep_key;
885 886 887 888 889 890 891
	int index;

	if (priv->wep_key_curr_index >= NUM_WEP_KEYS)
		priv->wep_key_curr_index = 0;
	wep_key = &priv->wep_key[priv->wep_key_curr_index];
	index = encrypt_key->key_index;
	if (encrypt_key->key_disable) {
892
		priv->sec_info.wep_enabled = 0;
893 894 895 896
	} else if (!encrypt_key->key_len) {
		/* Copy the required key as the current key */
		wep_key = &priv->wep_key[index];
		if (!wep_key->key_length) {
897 898
			mwifiex_dbg(adapter, ERROR,
				    "key not set, so cannot enable it\n");
899 900
			return -1;
		}
901

902
		if (adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2) {
903 904 905 906 907
			memcpy(encrypt_key->key_material,
			       wep_key->key_material, wep_key->key_length);
			encrypt_key->key_len = wep_key->key_length;
		}

908
		priv->wep_key_curr_index = (u16) index;
909
		priv->sec_info.wep_enabled = 1;
910 911 912 913 914 915 916 917 918
	} else {
		wep_key = &priv->wep_key[index];
		memset(wep_key, 0, sizeof(struct mwifiex_wep_key));
		/* Copy the key in the driver */
		memcpy(wep_key->key_material,
		       encrypt_key->key_material,
		       encrypt_key->key_len);
		wep_key->key_index = index;
		wep_key->key_length = encrypt_key->key_len;
919
		priv->sec_info.wep_enabled = 1;
920 921
	}
	if (wep_key->key_length) {
922 923
		void *enc_key;

924
		if (encrypt_key->key_disable) {
925 926
			memset(&priv->wep_key[index], 0,
			       sizeof(struct mwifiex_wep_key));
927 928
			goto done;
		}
929

930
		if (adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2)
931 932 933 934
			enc_key = encrypt_key;
		else
			enc_key = NULL;

935
		/* Send request to firmware */
936 937
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
				       HostCmd_ACT_GEN_SET, 0, enc_key, false);
938 939 940
		if (ret)
			return ret;
	}
941

942
done:
943
	if (priv->sec_info.wep_enabled)
944 945 946 947
		priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
	else
		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;

948 949 950
	ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
			       HostCmd_ACT_GEN_SET, 0,
			       &priv->curr_pkt_filter, true);
951 952 953 954 955 956 957 958 959 960 961 962 963 964

	return ret;
}

/*
 * IOCTL request handler to set WPA key.
 *
 * This function prepares the correct firmware command and
 * issues it, after validation checks.
 *
 * Current driver only supports key length of up to 32 bytes.
 *
 * This function can also be used to disable a currently set key.
 */
965
static int mwifiex_sec_ioctl_set_wpa_key(struct mwifiex_private *priv,
966 967
			      struct mwifiex_ds_encrypt_key *encrypt_key)
{
968
	int ret;
969 970 971 972
	u8 remove_key = false;
	struct host_cmd_ds_802_11_key_material *ibss_key;

	/* Current driver only supports key length of up to 32 bytes */
973
	if (encrypt_key->key_len > WLAN_MAX_KEY_LEN) {
974 975
		mwifiex_dbg(priv->adapter, ERROR,
			    "key length too long\n");
976 977 978
		return -1;
	}

979
	if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
980 981 982 983 984 985
		/*
		 * IBSS/WPA-None uses only one key (Group) for both receiving
		 * and sending unicast and multicast packets.
		 */
		/* Send the key as PTK to firmware */
		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;
986 987 988
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
				       HostCmd_ACT_GEN_SET,
				       KEY_INFO_ENABLED, encrypt_key, false);
989 990 991 992 993 994 995 996 997 998 999 1000 1001
		if (ret)
			return ret;

		ibss_key = &priv->aes_key;
		memset(ibss_key, 0,
		       sizeof(struct host_cmd_ds_802_11_key_material));
		/* Copy the key in the driver */
		memcpy(ibss_key->key_param_set.key, encrypt_key->key_material,
		       encrypt_key->key_len);
		memcpy(&ibss_key->key_param_set.key_len, &encrypt_key->key_len,
		       sizeof(ibss_key->key_param_set.key_len));
		ibss_key->key_param_set.key_type_id
			= cpu_to_le16(KEY_TYPE_ID_TKIP);
1002
		ibss_key->key_param_set.key_info = cpu_to_le16(KEY_ENABLED);
1003 1004 1005 1006 1007 1008 1009 1010 1011

		/* Send the key as GTK to firmware */
		encrypt_key->key_index = ~MWIFIEX_KEY_INDEX_UNICAST;
	}

	if (!encrypt_key->key_index)
		encrypt_key->key_index = MWIFIEX_KEY_INDEX_UNICAST;

	if (remove_key)
1012 1013 1014
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
				       HostCmd_ACT_GEN_SET,
				       !KEY_INFO_ENABLED, encrypt_key, true);
1015
	else
1016 1017 1018
		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_KEY_MATERIAL,
				       HostCmd_ACT_GEN_SET,
				       KEY_INFO_ENABLED, encrypt_key, true);
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

	return ret;
}

/*
 * IOCTL request handler to set/get network keys.
 *
 * This is a generic key handling function which supports WEP, WPA
 * and WAPI.
 */
static int
mwifiex_sec_ioctl_encrypt_key(struct mwifiex_private *priv,
			      struct mwifiex_ds_encrypt_key *encrypt_key)
{
1033
	int status;
1034 1035

	if (encrypt_key->is_wapi_key)
1036
		status = mwifiex_sec_ioctl_set_wapi_key(priv, encrypt_key);
1037
	else if (encrypt_key->key_len > WLAN_KEY_LEN_WEP104)
1038
		status = mwifiex_sec_ioctl_set_wpa_key(priv, encrypt_key);
1039
	else
1040
		status = mwifiex_sec_ioctl_set_wep_key(priv, encrypt_key);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	return status;
}

/*
 * This function returns the driver version.
 */
int
mwifiex_drv_get_driver_version(struct mwifiex_adapter *adapter, char *version,
			       int max_len)
{
	union {
1052
		__le32 l;
1053 1054 1055 1056
		u8 c[4];
	} ver;
	char fw_ver[32];

1057
	ver.l = cpu_to_le32(adapter->fw_release_number);
1058 1059 1060 1061
	sprintf(fw_ver, "%u.%u.%u.p%u", ver.c[2], ver.c[1], ver.c[0], ver.c[3]);

	snprintf(version, max_len, driver_version, fw_ver);

1062
	mwifiex_dbg(adapter, MSG, "info: MWIFIEX VERSION: %s\n", version);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

	return 0;
}

/*
 * Sends IOCTL request to set encoding parameters.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
1073 1074 1075
int mwifiex_set_encode(struct mwifiex_private *priv, struct key_params *kp,
		       const u8 *key, int key_len, u8 key_index,
		       const u8 *mac_addr, int disable)
1076 1077 1078 1079 1080
{
	struct mwifiex_ds_encrypt_key encrypt_key;

	memset(&encrypt_key, 0, sizeof(struct mwifiex_ds_encrypt_key));
	encrypt_key.key_len = key_len;
1081
	encrypt_key.key_index = key_index;
1082 1083 1084 1085

	if (kp && kp->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
		encrypt_key.is_igtk_key = true;

1086 1087 1088
	if (!disable) {
		if (key_len)
			memcpy(encrypt_key.key_material, key, key_len);
1089 1090 1091
		else
			encrypt_key.is_current_wep_key = true;

1092 1093
		if (mac_addr)
			memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1094
		if (kp && kp->seq && kp->seq_len) {
1095
			memcpy(encrypt_key.pn, kp->seq, kp->seq_len);
1096 1097 1098
			encrypt_key.pn_len = kp->seq_len;
			encrypt_key.is_rx_seq_valid = true;
		}
1099
	} else {
1100 1101
		if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP)
			return 0;
1102
		encrypt_key.key_disable = true;
1103 1104
		if (mac_addr)
			memcpy(encrypt_key.mac_addr, mac_addr, ETH_ALEN);
1105 1106
	}

1107
	return mwifiex_sec_ioctl_encrypt_key(priv, &encrypt_key);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
}

/*
 * Sends IOCTL request to get extended version.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
mwifiex_get_ver_ext(struct mwifiex_private *priv)
{
	struct mwifiex_ver_ext ver_ext;

	memset(&ver_ext, 0, sizeof(struct host_cmd_ds_version_ext));
1122 1123
	if (mwifiex_send_cmd(priv, HostCmd_CMD_VERSION_EXT,
			     HostCmd_ACT_GEN_GET, 0, &ver_ext, true))
1124
		return -1;
1125

1126
	return 0;
1127 1128
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
int
mwifiex_remain_on_chan_cfg(struct mwifiex_private *priv, u16 action,
			   struct ieee80211_channel *chan,
			   unsigned int duration)
{
	struct host_cmd_ds_remain_on_chan roc_cfg;
	u8 sc;

	memset(&roc_cfg, 0, sizeof(roc_cfg));
	roc_cfg.action = cpu_to_le16(action);
	if (action == HostCmd_ACT_GEN_SET) {
		roc_cfg.band_cfg = chan->band;
1141
		sc = mwifiex_chan_type_to_sec_chan_offset(NL80211_CHAN_NO_HT);
1142 1143 1144 1145 1146 1147
		roc_cfg.band_cfg |= (sc << 2);

		roc_cfg.channel =
			ieee80211_frequency_to_channel(chan->center_freq);
		roc_cfg.duration = cpu_to_le32(duration);
	}
1148 1149
	if (mwifiex_send_cmd(priv, HostCmd_CMD_REMAIN_ON_CHAN,
			     action, 0, &roc_cfg, true)) {
1150 1151
		mwifiex_dbg(priv->adapter, ERROR,
			    "failed to remain on channel\n");
1152 1153 1154 1155 1156 1157
		return -1;
	}

	return roc_cfg.status;
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
/*
 * Sends IOCTL request to get statistics information.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
mwifiex_get_stats_info(struct mwifiex_private *priv,
		       struct mwifiex_ds_get_stats *log)
{
1168 1169
	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_GET_LOG,
				HostCmd_ACT_GEN_GET, 0, log, true);
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
}

/*
 * IOCTL request handler to read/write register.
 *
 * This function prepares the correct firmware command and
 * issues it.
 *
 * Access to the following registers are supported -
 *      - MAC
 *      - BBP
 *      - RF
 *      - PMIC
 *      - CAU
 */
static int mwifiex_reg_mem_ioctl_reg_rw(struct mwifiex_private *priv,
					struct mwifiex_ds_reg_rw *reg_rw,
					u16 action)
{
	u16 cmd_no;

	switch (le32_to_cpu(reg_rw->type)) {
	case MWIFIEX_REG_MAC:
		cmd_no = HostCmd_CMD_MAC_REG_ACCESS;
		break;
	case MWIFIEX_REG_BBP:
		cmd_no = HostCmd_CMD_BBP_REG_ACCESS;
		break;
	case MWIFIEX_REG_RF:
		cmd_no = HostCmd_CMD_RF_REG_ACCESS;
		break;
	case MWIFIEX_REG_PMIC:
		cmd_no = HostCmd_CMD_PMIC_REG_ACCESS;
		break;
	case MWIFIEX_REG_CAU:
		cmd_no = HostCmd_CMD_CAU_REG_ACCESS;
		break;
	default:
		return -1;
	}

1211
	return mwifiex_send_cmd(priv, cmd_no, action, 0, reg_rw, true);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
}

/*
 * Sends IOCTL request to write to a register.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
mwifiex_reg_write(struct mwifiex_private *priv, u32 reg_type,
		  u32 reg_offset, u32 reg_value)
{
	struct mwifiex_ds_reg_rw reg_rw;

	reg_rw.type = cpu_to_le32(reg_type);
	reg_rw.offset = cpu_to_le32(reg_offset);
	reg_rw.value = cpu_to_le32(reg_value);

1230
	return mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_SET);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
}

/*
 * Sends IOCTL request to read from a register.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
mwifiex_reg_read(struct mwifiex_private *priv, u32 reg_type,
		 u32 reg_offset, u32 *value)
{
1243
	int ret;
1244 1245 1246 1247
	struct mwifiex_ds_reg_rw reg_rw;

	reg_rw.type = cpu_to_le32(reg_type);
	reg_rw.offset = cpu_to_le32(reg_offset);
1248
	ret = mwifiex_reg_mem_ioctl_reg_rw(priv, &reg_rw, HostCmd_ACT_GEN_GET);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

	if (ret)
		goto done;

	*value = le32_to_cpu(reg_rw.value);

done:
	return ret;
}

/*
 * Sends IOCTL request to read from EEPROM.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
mwifiex_eeprom_read(struct mwifiex_private *priv, u16 offset, u16 bytes,
		    u8 *value)
{
1269
	int ret;
1270 1271 1272 1273 1274
	struct mwifiex_ds_read_eeprom rd_eeprom;

	rd_eeprom.offset = cpu_to_le16((u16) offset);
	rd_eeprom.byte_count = cpu_to_le16((u16) bytes);

1275
	/* Send request to firmware */
1276 1277
	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_EEPROM_ACCESS,
			       HostCmd_ACT_GEN_GET, 0, &rd_eeprom, true);
1278

1279 1280
	if (!ret)
		memcpy(value, rd_eeprom.value, MAX_EEPROM_DATA);
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	return ret;
}

/*
 * This function sets a generic IE. In addition to generic IE, it can
 * also handle WPA, WPA2 and WAPI IEs.
 */
static int
mwifiex_set_gen_ie_helper(struct mwifiex_private *priv, u8 *ie_data_ptr,
			  u16 ie_len)
{
	int ret = 0;
	struct ieee_types_vendor_header *pvendor_ie;
	const u8 wpa_oui[] = { 0x00, 0x50, 0xf2, 0x01 };
	const u8 wps_oui[] = { 0x00, 0x50, 0xf2, 0x04 };
C
chunfan chen 已提交
1296 1297
	u16 unparsed_len = ie_len;
	int find_wpa_ie = 0;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

	/* If the passed length is zero, reset the buffer */
	if (!ie_len) {
		priv->gen_ie_buf_len = 0;
		priv->wps.session_enable = false;

		return 0;
	} else if (!ie_data_ptr) {
		return -1;
	}
	pvendor_ie = (struct ieee_types_vendor_header *) ie_data_ptr;

C
chunfan chen 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	while (pvendor_ie) {
		if (pvendor_ie->element_id == WLAN_EID_VENDOR_SPECIFIC) {
			/* Test to see if it is a WPA IE, if not, then it is a
			 * gen IE
			 */
			if (!memcmp(pvendor_ie->oui, wpa_oui,
				    sizeof(wpa_oui))) {
				find_wpa_ie = 1;
				break;
			}
1320

C
chunfan chen 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
			/* Test to see if it is a WPS IE, if so, enable
			 * wps session flag
			 */
			if (!memcmp(pvendor_ie->oui, wps_oui,
				    sizeof(wps_oui))) {
				priv->wps.session_enable = true;
				mwifiex_dbg(priv->adapter, MSG,
					    "info: WPS Session Enabled.\n");
				ret = mwifiex_set_wps_ie(priv,
							 (u8 *)pvendor_ie,
							 unparsed_len);
			}
		}

		if (pvendor_ie->element_id == WLAN_EID_RSN) {
			find_wpa_ie = 1;
			break;
		}

		if (pvendor_ie->element_id == WLAN_EID_BSS_AC_ACCESS_DELAY) {
1341
		/* IE is a WAPI IE so call set_wapi function */
C
chunfan chen 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
			ret = mwifiex_set_wapi_ie(priv, (u8 *)pvendor_ie,
						  unparsed_len);
			return ret;
		}

		unparsed_len -= (pvendor_ie->len +
				 sizeof(struct ieee_types_header));

		if (unparsed_len <= sizeof(struct ieee_types_header))
			pvendor_ie = NULL;
		else
			pvendor_ie = (struct ieee_types_vendor_header *)
				(((u8 *)pvendor_ie) + pvendor_ie->len +
				 sizeof(struct ieee_types_header));
	}
1357

C
chunfan chen 已提交
1358 1359 1360 1361 1362
	if (find_wpa_ie) {
		/* IE is a WPA/WPA2 IE so call set_wpa function */
		ret = mwifiex_set_wpa_ie_helper(priv, (u8 *)pvendor_ie,
						unparsed_len);
		priv->wps.session_enable = false;
1363 1364
		return ret;
	}
C
chunfan chen 已提交
1365

1366 1367 1368 1369 1370 1371 1372 1373 1374
	/*
	 * Verify that the passed length is not larger than the
	 * available space remaining in the buffer
	 */
	if (ie_len < (sizeof(priv->gen_ie_buf) - priv->gen_ie_buf_len)) {

		/* Append the passed data to the end of the
		   genIeBuffer */
		memcpy(priv->gen_ie_buf + priv->gen_ie_buf_len, ie_data_ptr,
1375
		       ie_len);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
		/* Increment the stored buffer length by the
		   size passed */
		priv->gen_ie_buf_len += ie_len;
	} else {
		/* Passed data does not fit in the remaining
		   buffer space */
		ret = -1;
	}

	/* Return 0, or -1 for error case */
	return ret;
}

/*
 * IOCTL request handler to set/get generic IE.
 *
 * In addition to various generic IEs, this function can also be
 * used to set the ARP filter.
 */
static int mwifiex_misc_ioctl_gen_ie(struct mwifiex_private *priv,
				     struct mwifiex_ds_misc_gen_ie *gen_ie,
				     u16 action)
{
	struct mwifiex_adapter *adapter = priv->adapter;

	switch (gen_ie->type) {
	case MWIFIEX_IE_TYPE_GEN_IE:
		if (action == HostCmd_ACT_GEN_GET) {
			gen_ie->len = priv->wpa_ie_len;
			memcpy(gen_ie->ie_data, priv->wpa_ie, gen_ie->len);
		} else {
			mwifiex_set_gen_ie_helper(priv, gen_ie->ie_data,
						  (u16) gen_ie->len);
		}
		break;
	case MWIFIEX_IE_TYPE_ARP_FILTER:
		memset(adapter->arp_filter, 0, sizeof(adapter->arp_filter));
		if (gen_ie->len > ARP_FILTER_MAX_BUF_SIZE) {
			adapter->arp_filter_size = 0;
1415 1416
			mwifiex_dbg(adapter, ERROR,
				    "invalid ARP filter size\n");
1417 1418 1419
			return -1;
		} else {
			memcpy(adapter->arp_filter, gen_ie->ie_data,
1420
			       gen_ie->len);
1421 1422 1423 1424
			adapter->arp_filter_size = gen_ie->len;
		}
		break;
	default:
1425
		mwifiex_dbg(adapter, ERROR, "invalid IE type\n");
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
		return -1;
	}
	return 0;
}

/*
 * Sends IOCTL request to set a generic IE.
 *
 * This function allocates the IOCTL request buffer, fills it
 * with requisite parameters and calls the IOCTL handler.
 */
int
1438
mwifiex_set_gen_ie(struct mwifiex_private *priv, const u8 *ie, int ie_len)
1439 1440 1441
{
	struct mwifiex_ds_misc_gen_ie gen_ie;

1442
	if (ie_len > IEEE_MAX_IE_SIZE)
1443 1444 1445 1446 1447
		return -EFAULT;

	gen_ie.type = MWIFIEX_IE_TYPE_GEN_IE;
	gen_ie.len = ie_len;
	memcpy(gen_ie.ie_data, ie, ie_len);
1448
	if (mwifiex_misc_ioctl_gen_ie(priv, &gen_ie, HostCmd_ACT_GEN_SET))
1449 1450 1451 1452
		return -EFAULT;

	return 0;
}