cmd.c 39.8 KB
Newer Older
1 2 3 4 5
/**
  * This file contains the handling of command.
  * It prepares command and sends it to firmware when it is ready.
  */

6
#include <linux/kfifo.h>
7
#include <linux/sched.h>
8
#include <linux/slab.h>
9

10 11 12 13
#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
14
#include "assoc.h"
15
#include "wext.h"
16
#include "scan.h"
17
#include "cmd.h"
18

19

20
static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/**
 *  @brief Simple callback that copies response back into command
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param extra  	A pointer to the original command structure for which
 *                      'resp' is a response
 *  @param resp         A pointer to the command response
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
		     struct cmd_header *resp)
{
	struct cmd_header *buf = (void *)extra;
	uint16_t copy_len;

	copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
	memcpy(buf, resp, copy_len);
	return 0;
}
EXPORT_SYMBOL_GPL(lbs_cmd_copyback);

/**
 *  @brief Simple callback that ignores the result. Use this if
 *  you just want to send a command to the hardware, but don't
 *  care for the result.
 *
 *  @param priv         ignored
 *  @param extra        ignored
 *  @param resp         ignored
 *
 *  @return 	   	0 for success
 */
static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
		     struct cmd_header *resp)
{
	return 0;
}


62
/**
63
 *  @brief Checks whether a command is allowed in Power Save mode
64 65
 *
 *  @param command the command ID
66
 *  @return 	   1 if allowed, 0 if not allowed
67
 */
68
static u8 is_command_allowed_in_ps(u16 cmd)
69
{
70 71 72
	switch (cmd) {
	case CMD_802_11_RSSI:
		return 1;
73 74
	case CMD_802_11_HOST_SLEEP_CFG:
		return 1;
75 76
	default:
		break;
77 78 79 80
	}
	return 0;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 *  @brief This function checks if the command is allowed.
 *
 *  @param priv         A pointer to lbs_private structure
 *  @return             allowed or not allowed.
 */

static int lbs_is_cmd_allowed(struct lbs_private *priv)
{
	int ret = 1;

	lbs_deb_enter(LBS_DEB_CMD);

	if (!priv->is_auto_deep_sleep_enabled) {
		if (priv->is_deep_sleep) {
			lbs_deb_cmd("command not allowed in deep sleep\n");
			ret = 0;
		}
	}

	lbs_deb_leave(LBS_DEB_CMD);
	return ret;
}

105 106 107 108 109 110 111 112
/**
 *  @brief Updates the hardware details like MAC address and regulatory region
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_update_hw_spec(struct lbs_private *priv)
113
{
114 115 116
	struct cmd_ds_get_hw_spec cmd;
	int ret = -1;
	u32 i;
117

118
	lbs_deb_enter(LBS_DEB_CMD);
119

120 121 122
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
123
	ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
124 125 126 127 128
	if (ret)
		goto out;

	priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);

129 130 131 132 133 134 135 136 137 138
	/* The firmware release is in an interesting format: the patch
	 * level is in the most significant nibble ... so fix that: */
	priv->fwrelease = le32_to_cpu(cmd.fwrelease);
	priv->fwrelease = (priv->fwrelease << 8) |
		(priv->fwrelease >> 24 & 0xff);

	/* Some firmware capabilities:
	 * CF card    firmware 5.0.16p0:   cap 0x00000303
	 * USB dongle firmware 5.110.17p2: cap 0x00000303
	 */
J
Johannes Berg 已提交
139 140
	lbs_pr_info("%pM, fw %u.%u.%up%u, cap 0x%08x\n",
		cmd.permanentaddr,
141 142 143 144 145
		priv->fwrelease >> 24 & 0xff,
		priv->fwrelease >> 16 & 0xff,
		priv->fwrelease >>  8 & 0xff,
		priv->fwrelease       & 0xff,
		priv->fwcapinfo);
146 147 148 149 150 151
	lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
		    cmd.hwifversion, cmd.version);

	/* Clamp region code to 8-bit since FW spec indicates that it should
	 * only ever be 8-bit, even though the field size is 16-bit.  Some firmware
	 * returns non-zero high 8 bits here.
152 153 154
	 *
	 * Firmware version 4.0.102 used in CF8381 has region code shifted.  We
	 * need to check for this problem and handle it properly.
155
	 */
156 157 158 159
	if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
		priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
	else
		priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
		/* use the region code to search for the index */
		if (priv->regioncode == lbs_region_code_to_index[i])
			break;
	}

	/* if it's unidentified region code, use the default (USA) */
	if (i >= MRVDRV_MAX_REGION_CODE) {
		priv->regioncode = 0x10;
		lbs_pr_info("unidentified region code; using the default (USA)\n");
	}

	if (priv->current_addr[0] == 0xff)
		memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
175

176 177 178 179 180 181 182 183 184 185
	memcpy(priv->dev->dev_addr, priv->current_addr, ETH_ALEN);
	if (priv->mesh_dev)
		memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);

	if (lbs_set_regiontable(priv, priv->regioncode, 0)) {
		ret = -1;
		goto out;
	}

out:
186
	lbs_deb_leave(LBS_DEB_CMD);
187
	return ret;
188 189
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
			struct cmd_header *resp)
{
	lbs_deb_enter(LBS_DEB_CMD);
	if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
		priv->is_host_sleep_configured = 0;
		if (priv->psstate == PS_STATE_FULL_POWER) {
			priv->is_host_sleep_activated = 0;
			wake_up_interruptible(&priv->host_sleep_q);
		}
	} else {
		priv->is_host_sleep_configured = 1;
	}
	lbs_deb_leave(LBS_DEB_CMD);
	return 0;
}

207 208
int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
		struct wol_config *p_wol_config)
209 210 211 212
{
	struct cmd_ds_host_sleep cmd_config;
	int ret;

213
	cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
214
	cmd_config.criteria = cpu_to_le32(criteria);
215 216
	cmd_config.gpio = priv->wol_gpio;
	cmd_config.gap = priv->wol_gap;
217

218 219 220 221 222 223
	if (p_wol_config != NULL)
		memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
				sizeof(struct wol_config));
	else
		cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;

224 225 226
	ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
			le16_to_cpu(cmd_config.hdr.size),
			lbs_ret_host_sleep_cfg, 0);
227
	if (!ret) {
228
		if (p_wol_config)
229 230 231
			memcpy((uint8_t *) p_wol_config,
					(uint8_t *)&cmd_config.wol_conf,
					sizeof(struct wol_config));
232
	} else {
233 234
		lbs_pr_info("HOST_SLEEP_CFG failed %d\n", ret);
	}
235

236 237 238 239
	return ret;
}
EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);

240
static int lbs_cmd_802_11_ps_mode(struct cmd_ds_command *cmd,
241 242 243 244
				   u16 cmd_action)
{
	struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;

245
	lbs_deb_enter(LBS_DEB_CMD);
246

247
	cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
248
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
249
				sizeof(struct cmd_header));
250 251
	psm->action = cpu_to_le16(cmd_action);
	psm->multipledtim = 0;
252
	switch (cmd_action) {
253
	case CMD_SUBCMD_ENTER_PS:
254
		lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
255

256
		psm->locallisteninterval = 0;
257
		psm->nullpktinterval = 0;
258
		psm->multipledtim =
259
		    cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
260 261
		break;

262
	case CMD_SUBCMD_EXIT_PS:
263
		lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
264 265
		break;

266
	case CMD_SUBCMD_SLEEP_CONFIRMED:
267
		lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
268 269 270 271 272 273
		break;

	default:
		break;
	}

274
	lbs_deb_leave(LBS_DEB_CMD);
275 276 277
	return 0;
}

278 279
int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
				struct sleep_params *sp)
280
{
281 282
	struct cmd_ds_802_11_sleep_params cmd;
	int ret;
283

284
	lbs_deb_enter(LBS_DEB_CMD);
285

286
	if (cmd_action == CMD_ACT_GET) {
287 288 289 290 291 292 293 294
		memset(&cmd, 0, sizeof(cmd));
	} else {
		cmd.error = cpu_to_le16(sp->sp_error);
		cmd.offset = cpu_to_le16(sp->sp_offset);
		cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
		cmd.calcontrol = sp->sp_calcontrol;
		cmd.externalsleepclk = sp->sp_extsleepclk;
		cmd.reserved = cpu_to_le16(sp->sp_reserved);
295
	}
296 297
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(cmd_action);
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);

	if (!ret) {
		lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
			    "calcontrol 0x%x extsleepclk 0x%x\n",
			    le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
			    le16_to_cpu(cmd.stabletime), cmd.calcontrol,
			    cmd.externalsleepclk);

		sp->sp_error = le16_to_cpu(cmd.error);
		sp->sp_offset = le16_to_cpu(cmd.offset);
		sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
		sp->sp_calcontrol = cmd.calcontrol;
		sp->sp_extsleepclk = cmd.externalsleepclk;
		sp->sp_reserved = le16_to_cpu(cmd.reserved);
	}

	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
317 318 319
	return 0;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static int lbs_wait_for_ds_awake(struct lbs_private *priv)
{
	int ret = 0;

	lbs_deb_enter(LBS_DEB_CMD);

	if (priv->is_deep_sleep) {
		if (!wait_event_interruptible_timeout(priv->ds_awake_q,
					!priv->is_deep_sleep, (10 * HZ))) {
			lbs_pr_err("ds_awake_q: timer expired\n");
			ret = -1;
		}
	}

	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
}

int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
{
	int ret =  0;

	lbs_deb_enter(LBS_DEB_CMD);

	if (deep_sleep) {
		if (priv->is_deep_sleep != 1) {
			lbs_deb_cmd("deep sleep: sleep\n");
			BUG_ON(!priv->enter_deep_sleep);
			ret = priv->enter_deep_sleep(priv);
			if (!ret) {
				netif_stop_queue(priv->dev);
				netif_carrier_off(priv->dev);
			}
		} else {
			lbs_pr_err("deep sleep: already enabled\n");
		}
	} else {
		if (priv->is_deep_sleep) {
			lbs_deb_cmd("deep sleep: wakeup\n");
			BUG_ON(!priv->exit_deep_sleep);
			ret = priv->exit_deep_sleep(priv);
			if (!ret) {
				ret = lbs_wait_for_ds_awake(priv);
				if (ret)
					lbs_pr_err("deep sleep: wakeup"
							"failed\n");
			}
		}
	}

	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
}

374 375 376 377 378 379 380 381 382 383
/**
 *  @brief Set an SNMP MIB value
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param oid  	The OID to set in the firmware
 *  @param val  	Value to set the OID to
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
384
{
385 386
	struct cmd_ds_802_11_snmp_mib cmd;
	int ret;
387

388
	lbs_deb_enter(LBS_DEB_CMD);
389

390 391 392 393
	memset(&cmd, 0, sizeof (cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_SET);
	cmd.oid = cpu_to_le16((u16) oid);
394

395 396 397
	switch (oid) {
	case SNMP_MIB_OID_BSS_TYPE:
		cmd.bufsize = cpu_to_le16(sizeof(u8));
398
		cmd.value[0] = val;
399 400 401 402 403 404 405 406
		break;
	case SNMP_MIB_OID_11D_ENABLE:
	case SNMP_MIB_OID_FRAG_THRESHOLD:
	case SNMP_MIB_OID_RTS_THRESHOLD:
	case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
	case SNMP_MIB_OID_LONG_RETRY_LIMIT:
		cmd.bufsize = cpu_to_le16(sizeof(u16));
		*((__le16 *)(&cmd.value)) = cpu_to_le16(val);
407
		break;
408 409 410 411
	default:
		lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
		ret = -EINVAL;
		goto out;
412 413
	}

414 415
	lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
		    le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
416

417
	ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
418

419 420 421 422
out:
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
}
423

424 425 426 427 428 429 430 431 432 433 434 435 436
/**
 *  @brief Get an SNMP MIB value
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param oid  	The OID to retrieve from the firmware
 *  @param out_val  	Location for the returned value
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_get_snmp_mib(struct lbs_private *priv, u32 oid, u16 *out_val)
{
	struct cmd_ds_802_11_snmp_mib cmd;
	int ret;
437

438
	lbs_deb_enter(LBS_DEB_CMD);
439

440 441 442 443
	memset(&cmd, 0, sizeof (cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_GET);
	cmd.oid = cpu_to_le16(oid);
444

445 446 447
	ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
	if (ret)
		goto out;
448

449 450
	switch (le16_to_cpu(cmd.bufsize)) {
	case sizeof(u8):
451
		*out_val = cmd.value[0];
452 453 454
		break;
	case sizeof(u16):
		*out_val = le16_to_cpu(*((__le16 *)(&cmd.value)));
455 456
		break;
	default:
457 458
		lbs_deb_cmd("SNMP_CMD: (get) unhandled OID 0x%x size %d\n",
		            oid, le16_to_cpu(cmd.bufsize));
459 460 461
		break;
	}

462 463 464
out:
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
465 466
}

467 468 469 470 471 472 473 474 475 476 477 478
/**
 *  @brief Get the min, max, and current TX power
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param curlevel  	Current power level in dBm
 *  @param minlevel  	Minimum supported power level in dBm (optional)
 *  @param maxlevel  	Maximum supported power level in dBm (optional)
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
		     s16 *maxlevel)
479
{
480 481
	struct cmd_ds_802_11_rf_tx_power cmd;
	int ret;
482

483
	lbs_deb_enter(LBS_DEB_CMD);
484

485 486 487 488 489 490 491 492
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_GET);

	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
	if (ret == 0) {
		*curlevel = le16_to_cpu(cmd.curlevel);
		if (minlevel)
493
			*minlevel = cmd.minlevel;
494
		if (maxlevel)
495
			*maxlevel = cmd.maxlevel;
496
	}
497

498 499 500
	lbs_deb_leave(LBS_DEB_CMD);
	return ret;
}
501

502 503 504 505 506 507 508 509 510 511 512 513
/**
 *  @brief Set the TX power
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param dbm  	The desired power level in dBm
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_set_tx_power(struct lbs_private *priv, s16 dbm)
{
	struct cmd_ds_802_11_rf_tx_power cmd;
	int ret;
514

515
	lbs_deb_enter(LBS_DEB_CMD);
516

517 518 519 520
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_SET);
	cmd.curlevel = cpu_to_le16(dbm);
521

522 523 524
	lbs_deb_cmd("SET_RF_TX_POWER: %d dBm\n", dbm);

	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
525 526

	lbs_deb_leave(LBS_DEB_CMD);
527
	return ret;
528 529
}

530
static int lbs_cmd_802_11_monitor_mode(struct cmd_ds_command *cmd,
531 532 533 534 535 536 537
				      u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_802_11_monitor_mode *monitor = &cmd->params.monitor;

	cmd->command = cpu_to_le16(CMD_802_11_MONITOR_MODE);
	cmd->size =
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_monitor_mode) +
538
			     sizeof(struct cmd_header));
539 540 541 542 543 544 545 546 547 548

	monitor->action = cpu_to_le16(cmd_action);
	if (cmd_action == CMD_ACT_SET) {
		monitor->mode =
		    cpu_to_le16((u16) (*(u32 *) pdata_buf));
	}

	return 0;
}

549 550 551 552 553 554 555
/**
 *  @brief Get the radio channel
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *
 *  @return 	   	The channel on success, error on failure
 */
556
static int lbs_get_channel(struct lbs_private *priv)
557
{
558 559
	struct cmd_ds_802_11_rf_channel cmd;
	int ret = 0;
560

561
	lbs_deb_enter(LBS_DEB_CMD);
562

563
	memset(&cmd, 0, sizeof(cmd));
564 565
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
566

567
	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
568 569
	if (ret)
		goto out;
570

571 572
	ret = le16_to_cpu(cmd.channel);
	lbs_deb_cmd("current radio channel is %d\n", ret);
573 574 575 576 577 578

out:
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
}

579 580 581 582 583 584 585 586 587
int lbs_update_channel(struct lbs_private *priv)
{
	int ret;

	/* the channel in f/w could be out of sync; get the current channel */
	lbs_deb_enter(LBS_DEB_ASSOC);

	ret = lbs_get_channel(priv);
	if (ret > 0) {
588
		priv->channel = ret;
589 590 591 592 593 594
		ret = 0;
	}
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}

595 596 597 598 599 600 601 602 603 604 605
/**
 *  @brief Set the radio channel
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param channel  	The desired channel, or 0 to clear a locked channel
 *
 *  @return 	   	0 on success, error on failure
 */
int lbs_set_channel(struct lbs_private *priv, u8 channel)
{
	struct cmd_ds_802_11_rf_channel cmd;
606
#ifdef DEBUG
607
	u8 old_channel = priv->channel;
608
#endif
609 610 611 612
	int ret = 0;

	lbs_deb_enter(LBS_DEB_CMD);

613
	memset(&cmd, 0, sizeof(cmd));
614 615 616 617
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
	cmd.channel = cpu_to_le16(channel);

618
	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
619 620 621
	if (ret)
		goto out;

622
	priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
623
	lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
624
		priv->channel);
625 626 627 628

out:
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
629 630
}

631
static int lbs_cmd_reg_access(struct cmd_ds_command *cmdptr,
632 633
			       u8 cmd_action, void *pdata_buf)
{
634
	struct lbs_offset_value *offval;
635

636
	lbs_deb_enter(LBS_DEB_CMD);
637

638
	offval = (struct lbs_offset_value *)pdata_buf;
639

H
Holger Schurig 已提交
640
	switch (le16_to_cpu(cmdptr->command)) {
641
	case CMD_MAC_REG_ACCESS:
642 643 644 645
		{
			struct cmd_ds_mac_reg_access *macreg;

			cmdptr->size =
646
			    cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
647
					+ sizeof(struct cmd_header));
648 649 650 651 652 653 654 655 656 657 658
			macreg =
			    (struct cmd_ds_mac_reg_access *)&cmdptr->params.
			    macreg;

			macreg->action = cpu_to_le16(cmd_action);
			macreg->offset = cpu_to_le16((u16) offval->offset);
			macreg->value = cpu_to_le32(offval->value);

			break;
		}

659
	case CMD_BBP_REG_ACCESS:
660 661 662 663 664 665
		{
			struct cmd_ds_bbp_reg_access *bbpreg;

			cmdptr->size =
			    cpu_to_le16(sizeof
					     (struct cmd_ds_bbp_reg_access)
666
					     + sizeof(struct cmd_header));
667 668 669 670 671 672 673 674 675 676 677
			bbpreg =
			    (struct cmd_ds_bbp_reg_access *)&cmdptr->params.
			    bbpreg;

			bbpreg->action = cpu_to_le16(cmd_action);
			bbpreg->offset = cpu_to_le16((u16) offval->offset);
			bbpreg->value = (u8) offval->value;

			break;
		}

678
	case CMD_RF_REG_ACCESS:
679 680 681 682 683 684
		{
			struct cmd_ds_rf_reg_access *rfreg;

			cmdptr->size =
			    cpu_to_le16(sizeof
					     (struct cmd_ds_rf_reg_access) +
685
					     sizeof(struct cmd_header));
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
			rfreg =
			    (struct cmd_ds_rf_reg_access *)&cmdptr->params.
			    rfreg;

			rfreg->action = cpu_to_le16(cmd_action);
			rfreg->offset = cpu_to_le16((u16) offval->offset);
			rfreg->value = (u8) offval->value;

			break;
		}

	default:
		break;
	}

701
	lbs_deb_leave(LBS_DEB_CMD);
702 703 704
	return 0;
}

705 706
static void lbs_queue_cmd(struct lbs_private *priv,
			  struct cmd_ctrl_node *cmdnode)
707 708
{
	unsigned long flags;
709
	int addtail = 1;
710

711
	lbs_deb_enter(LBS_DEB_HOST);
712

713 714
	if (!cmdnode) {
		lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
715 716
		goto done;
	}
717 718 719 720
	if (!cmdnode->cmdbuf->size) {
		lbs_deb_host("DNLD_CMD: cmd size is zero\n");
		goto done;
	}
721
	cmdnode->result = 0;
722 723

	/* Exit_PS command needs to be queued in the header always. */
724
	if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
725
		struct cmd_ds_802_11_ps_mode *psm = (void *) &cmdnode->cmdbuf[1];
726

727
		if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
728
			if (priv->psstate != PS_STATE_FULL_POWER)
729 730 731 732
				addtail = 0;
		}
	}

733 734 735 736
	if (le16_to_cpu(cmdnode->cmdbuf->command) ==
			CMD_802_11_WAKEUP_CONFIRM)
		addtail = 0;

737
	spin_lock_irqsave(&priv->driver_lock, flags);
738

739
	if (addtail)
740
		list_add_tail(&cmdnode->list, &priv->cmdpendingq);
741
	else
742
		list_add(&cmdnode->list, &priv->cmdpendingq);
743

744
	spin_unlock_irqrestore(&priv->driver_lock, flags);
745

746
	lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
747
		     le16_to_cpu(cmdnode->cmdbuf->command));
748 749

done:
750
	lbs_deb_leave(LBS_DEB_HOST);
751 752
}

753 754
static void lbs_submit_command(struct lbs_private *priv,
			       struct cmd_ctrl_node *cmdnode)
755 756
{
	unsigned long flags;
757
	struct cmd_header *cmd;
758 759
	uint16_t cmdsize;
	uint16_t command;
760
	int timeo = 3 * HZ;
761
	int ret;
762

763
	lbs_deb_enter(LBS_DEB_HOST);
764

765
	cmd = cmdnode->cmdbuf;
766

767 768 769 770
	spin_lock_irqsave(&priv->driver_lock, flags);
	priv->cur_cmd = cmdnode;
	priv->cur_cmd_retcode = 0;
	spin_unlock_irqrestore(&priv->driver_lock, flags);
771

772 773
	cmdsize = le16_to_cpu(cmd->size);
	command = le16_to_cpu(cmd->command);
774

775
	/* These commands take longer */
776
	if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
777
		timeo = 5 * HZ;
778

H
Holger Schurig 已提交
779 780
	lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
		     command, le16_to_cpu(cmd->seqnum), cmdsize);
781
	lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
782

783
	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
784

785 786
	if (ret) {
		lbs_pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
787 788
		/* Let the timer kick in and retry, and potentially reset
		   the whole thing if the condition persists */
789
		timeo = HZ/4;
790
	}
791

792 793 794 795 796 797 798 799 800 801 802
	if (command == CMD_802_11_DEEP_SLEEP) {
		if (priv->is_auto_deep_sleep_enabled) {
			priv->wakeup_dev_required = 1;
			priv->dnld_sent = 0;
		}
		priv->is_deep_sleep = 1;
		lbs_complete_command(priv, cmdnode, 0);
	} else {
		/* Setup the timer after transmit command */
		mod_timer(&priv->command_timer, jiffies + timeo);
	}
803

804
	lbs_deb_leave(LBS_DEB_HOST);
805 806 807 808
}

/**
 *  This function inserts command node to cmdfreeq
809
 *  after cleans it. Requires priv->driver_lock held.
810
 */
811
static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
812
					 struct cmd_ctrl_node *cmdnode)
813
{
814 815 816 817 818 819 820
	lbs_deb_enter(LBS_DEB_HOST);

	if (!cmdnode)
		goto out;

	cmdnode->callback = NULL;
	cmdnode->callback_arg = 0;
821

822
	memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
823

824 825 826
	list_add_tail(&cmdnode->list, &priv->cmdfreeq);
 out:
	lbs_deb_leave(LBS_DEB_HOST);
827 828
}

829 830
static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
	struct cmd_ctrl_node *ptempcmd)
831 832 833
{
	unsigned long flags;

834
	spin_lock_irqsave(&priv->driver_lock, flags);
835
	__lbs_cleanup_and_insert_cmd(priv, ptempcmd);
836
	spin_unlock_irqrestore(&priv->driver_lock, flags);
837 838
}

839 840 841 842 843
void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
			  int result)
{
	if (cmd == priv->cur_cmd)
		priv->cur_cmd_retcode = result;
844

845
	cmd->result = result;
846 847 848
	cmd->cmdwaitqwoken = 1;
	wake_up_interruptible(&cmd->cmdwait_q);

849
	if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
850
		__lbs_cleanup_and_insert_cmd(priv, cmd);
851 852 853
	priv->cur_cmd = NULL;
}

854
int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
855
{
856
	struct cmd_ds_802_11_radio_control cmd;
857
	int ret = -EINVAL;
858

859
	lbs_deb_enter(LBS_DEB_CMD);
860

861 862 863
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_SET);

864 865 866 867 868 869 870 871 872 873 874 875
	/* Only v8 and below support setting the preamble */
	if (priv->fwrelease < 0x09000000) {
		switch (preamble) {
		case RADIO_PREAMBLE_SHORT:
		case RADIO_PREAMBLE_AUTO:
		case RADIO_PREAMBLE_LONG:
			cmd.control = cpu_to_le16(preamble);
			break;
		default:
			goto out;
		}
	}
876

877 878 879 880 881
	if (radio_on)
		cmd.control |= cpu_to_le16(0x1);
	else {
		cmd.control &= cpu_to_le16(~0x1);
		priv->txpower_cur = 0;
882
	}
883

884 885
	lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
		    radio_on ? "ON" : "OFF", preamble);
886

887
	priv->radio_on = radio_on;
888 889

	ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
890

891
out:
892
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
893 894 895
	return ret;
}

896
void lbs_set_mac_control(struct lbs_private *priv)
897
{
898
	struct cmd_ds_mac_control cmd;
899

900
	lbs_deb_enter(LBS_DEB_CMD);
901

902
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
903
	cmd.action = cpu_to_le16(priv->mac_control);
904 905
	cmd.reserved = 0;

906
	lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
907

908
	lbs_deb_leave(LBS_DEB_CMD);
909 910 911 912 913
}

/**
 *  @brief This function prepare the command before send to firmware.
 *
914
 *  @param priv		A pointer to struct lbs_private structure
915 916 917 918 919 920 921
 *  @param cmd_no	command number
 *  @param cmd_action	command action: GET or SET
 *  @param wait_option	wait option: wait response or not
 *  @param cmd_oid	cmd oid: treated as sub command
 *  @param pdata_buf	A pointer to informaion buffer
 *  @return 		0 or -1
 */
922
int lbs_prepare_and_send_command(struct lbs_private *priv,
923 924 925 926 927 928 929 930 931
			  u16 cmd_no,
			  u16 cmd_action,
			  u16 wait_option, u32 cmd_oid, void *pdata_buf)
{
	int ret = 0;
	struct cmd_ctrl_node *cmdnode;
	struct cmd_ds_command *cmdptr;
	unsigned long flags;

932
	lbs_deb_enter(LBS_DEB_HOST);
933

934 935
	if (!priv) {
		lbs_deb_host("PREP_CMD: priv is NULL\n");
936 937 938 939
		ret = -1;
		goto done;
	}

940
	if (priv->surpriseremoved) {
941
		lbs_deb_host("PREP_CMD: card removed\n");
942 943 944 945
		ret = -1;
		goto done;
	}

946 947 948 949 950
	if (!lbs_is_cmd_allowed(priv)) {
		ret = -EBUSY;
		goto done;
	}

951
	cmdnode = lbs_get_cmd_ctrl_node(priv);
952 953

	if (cmdnode == NULL) {
954
		lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
955 956

		/* Wake up main thread to execute next command */
957
		wake_up_interruptible(&priv->waitq);
958 959 960 961
		ret = -1;
		goto done;
	}

962 963
	cmdnode->callback = NULL;
	cmdnode->callback_arg = (unsigned long)pdata_buf;
964

965
	cmdptr = (struct cmd_ds_command *)cmdnode->cmdbuf;
966

967
	lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
968 969

	/* Set sequence number, command and INT option */
970 971
	priv->seqnum++;
	cmdptr->seqnum = cpu_to_le16(priv->seqnum);
972

973
	cmdptr->command = cpu_to_le16(cmd_no);
974 975 976
	cmdptr->result = 0;

	switch (cmd_no) {
977
	case CMD_802_11_PS_MODE:
978
		ret = lbs_cmd_802_11_ps_mode(cmdptr, cmd_action);
979 980
		break;

981 982 983
	case CMD_MAC_REG_ACCESS:
	case CMD_BBP_REG_ACCESS:
	case CMD_RF_REG_ACCESS:
984
		ret = lbs_cmd_reg_access(cmdptr, cmd_action, pdata_buf);
985 986
		break;

987
	case CMD_802_11_MONITOR_MODE:
988
		ret = lbs_cmd_802_11_monitor_mode(cmdptr,
989 990 991
				          cmd_action, pdata_buf);
		break;

992
	case CMD_802_11_RSSI:
993
		ret = lbs_cmd_802_11_rssi(priv, cmdptr);
994 995
		break;

996 997
	case CMD_802_11_SET_AFC:
	case CMD_802_11_GET_AFC:
998 999

		cmdptr->command = cpu_to_le16(cmd_no);
1000
		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
1001
					   sizeof(struct cmd_header));
1002 1003 1004 1005 1006 1007 1008

		memmove(&cmdptr->params.afc,
			pdata_buf, sizeof(struct cmd_ds_802_11_afc));

		ret = 0;
		goto done;

1009 1010
	case CMD_802_11_TPC_CFG:
		cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1011 1012
		cmdptr->size =
		    cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
1013
				     sizeof(struct cmd_header));
1014 1015 1016 1017 1018 1019

		memmove(&cmdptr->params.tpccfg,
			pdata_buf, sizeof(struct cmd_ds_802_11_tpc_cfg));

		ret = 0;
		break;
1020

H
Holger Schurig 已提交
1021 1022
#ifdef CONFIG_LIBERTAS_MESH

1023
	case CMD_BT_ACCESS:
1024
		ret = lbs_cmd_bt_access(cmdptr, cmd_action, pdata_buf);
1025 1026
		break;

1027
	case CMD_FWT_ACCESS:
1028
		ret = lbs_cmd_fwt_access(cmdptr, cmd_action, pdata_buf);
1029 1030
		break;

H
Holger Schurig 已提交
1031 1032
#endif

1033 1034 1035
	case CMD_802_11_BEACON_CTRL:
		ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
		break;
1036 1037
	case CMD_802_11_DEEP_SLEEP:
		cmdptr->command = cpu_to_le16(CMD_802_11_DEEP_SLEEP);
1038
		cmdptr->size = cpu_to_le16(sizeof(struct cmd_header));
1039
		break;
1040
	default:
1041
		lbs_pr_err("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1042 1043 1044 1045 1046 1047
		ret = -1;
		break;
	}

	/* return error, since the command preparation failed */
	if (ret != 0) {
1048
		lbs_deb_host("PREP_CMD: command preparation failed\n");
1049
		lbs_cleanup_and_insert_cmd(priv, cmdnode);
1050 1051 1052 1053 1054 1055
		ret = -1;
		goto done;
	}

	cmdnode->cmdwaitqwoken = 0;

1056
	lbs_queue_cmd(priv, cmdnode);
1057
	wake_up_interruptible(&priv->waitq);
1058

1059
	if (wait_option & CMD_OPTION_WAITFORRSP) {
1060
		lbs_deb_host("PREP_CMD: wait for response\n");
1061 1062 1063 1064 1065
		might_sleep();
		wait_event_interruptible(cmdnode->cmdwait_q,
					 cmdnode->cmdwaitqwoken);
	}

1066 1067
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd_retcode) {
1068
		lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1069 1070
		       priv->cur_cmd_retcode);
		priv->cur_cmd_retcode = 0;
1071 1072
		ret = -1;
	}
1073
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1074 1075

done:
1076
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1077 1078 1079 1080 1081 1082 1083
	return ret;
}

/**
 *  @brief This function allocates the command buffer and link
 *  it to command free queue.
 *
1084
 *  @param priv		A pointer to struct lbs_private structure
1085 1086
 *  @return 		0 or -1
 */
1087
int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1088 1089
{
	int ret = 0;
1090
	u32 bufsize;
1091
	u32 i;
1092
	struct cmd_ctrl_node *cmdarray;
1093

1094
	lbs_deb_enter(LBS_DEB_HOST);
1095

1096 1097 1098
	/* Allocate and initialize the command array */
	bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
	if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1099
		lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1100 1101 1102
		ret = -1;
		goto done;
	}
1103
	priv->cmd_array = cmdarray;
1104

1105 1106 1107 1108
	/* Allocate and initialize each command buffer in the command array */
	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
		cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
		if (!cmdarray[i].cmdbuf) {
1109
			lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1110 1111 1112 1113 1114
			ret = -1;
			goto done;
		}
	}

1115 1116 1117
	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
		init_waitqueue_head(&cmdarray[i].cmdwait_q);
		lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1118 1119
	}
	ret = 0;
1120 1121

done:
1122
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1123 1124 1125 1126 1127 1128
	return ret;
}

/**
 *  @brief This function frees the command buffer.
 *
1129
 *  @param priv		A pointer to struct lbs_private structure
1130 1131
 *  @return 		0 or -1
 */
1132
int lbs_free_cmd_buffer(struct lbs_private *priv)
1133
{
1134
	struct cmd_ctrl_node *cmdarray;
1135 1136
	unsigned int i;

1137
	lbs_deb_enter(LBS_DEB_HOST);
1138 1139

	/* need to check if cmd array is allocated or not */
1140
	if (priv->cmd_array == NULL) {
1141
		lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1142 1143 1144
		goto done;
	}

1145
	cmdarray = priv->cmd_array;
1146 1147

	/* Release shared memory buffers */
1148 1149 1150 1151
	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
		if (cmdarray[i].cmdbuf) {
			kfree(cmdarray[i].cmdbuf);
			cmdarray[i].cmdbuf = NULL;
1152 1153 1154 1155
		}
	}

	/* Release cmd_ctrl_node */
1156 1157 1158
	if (priv->cmd_array) {
		kfree(priv->cmd_array);
		priv->cmd_array = NULL;
1159 1160 1161
	}

done:
1162
	lbs_deb_leave(LBS_DEB_HOST);
1163 1164 1165 1166 1167 1168 1169
	return 0;
}

/**
 *  @brief This function gets a free command node if available in
 *  command free queue.
 *
1170
 *  @param priv		A pointer to struct lbs_private structure
1171 1172
 *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
 */
1173
static struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1174 1175 1176 1177
{
	struct cmd_ctrl_node *tempnode;
	unsigned long flags;

1178 1179
	lbs_deb_enter(LBS_DEB_HOST);

1180
	if (!priv)
1181 1182
		return NULL;

1183
	spin_lock_irqsave(&priv->driver_lock, flags);
1184

1185 1186
	if (!list_empty(&priv->cmdfreeq)) {
		tempnode = list_first_entry(&priv->cmdfreeq,
1187 1188
					    struct cmd_ctrl_node, list);
		list_del(&tempnode->list);
1189
	} else {
1190
		lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1191 1192 1193
		tempnode = NULL;
	}

1194
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1195

1196
	lbs_deb_leave(LBS_DEB_HOST);
1197 1198 1199 1200 1201
	return tempnode;
}

/**
 *  @brief This function executes next command in command
1202
 *  pending queue. It will put firmware back to PS mode
1203 1204
 *  if applicable.
 *
1205
 *  @param priv     A pointer to struct lbs_private structure
1206 1207
 *  @return 	   0 or -1
 */
1208
int lbs_execute_next_command(struct lbs_private *priv)
1209 1210
{
	struct cmd_ctrl_node *cmdnode = NULL;
1211
	struct cmd_header *cmd;
1212 1213 1214
	unsigned long flags;
	int ret = 0;

1215 1216 1217
	/* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
	 * only caller to us is lbs_thread() and we get even when a
	 * data packet is received */
1218
	lbs_deb_enter(LBS_DEB_THREAD);
1219

1220
	spin_lock_irqsave(&priv->driver_lock, flags);
1221

1222
	if (priv->cur_cmd) {
1223
		lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1224
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1225 1226 1227 1228
		ret = -1;
		goto done;
	}

1229 1230
	if (!list_empty(&priv->cmdpendingq)) {
		cmdnode = list_first_entry(&priv->cmdpendingq,
1231
					   struct cmd_ctrl_node, list);
1232 1233
	}

1234
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1235 1236

	if (cmdnode) {
1237
		cmd = cmdnode->cmdbuf;
1238

1239
		if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1240 1241
			if ((priv->psstate == PS_STATE_SLEEP) ||
			    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1242 1243
				lbs_deb_host(
				       "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1244
				       le16_to_cpu(cmd->command),
1245
				       priv->psstate);
1246 1247 1248
				ret = -1;
				goto done;
			}
1249
			lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1250 1251
				     "0x%04x in psstate %d\n",
				     le16_to_cpu(cmd->command), priv->psstate);
1252
		} else if (priv->psstate != PS_STATE_FULL_POWER) {
1253 1254 1255
			/*
			 * 1. Non-PS command:
			 * Queue it. set needtowakeup to TRUE if current state
1256
			 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1257 1258 1259 1260 1261 1262 1263
			 * 2. PS command but not Exit_PS:
			 * Ignore it.
			 * 3. PS command Exit_PS:
			 * Set needtowakeup to TRUE if current state is SLEEP,
			 * otherwise send this command down to firmware
			 * immediately.
			 */
1264
			if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1265 1266
				/*  Prepare to send Exit PS,
				 *  this non PS command will be sent later */
1267 1268
				if ((priv->psstate == PS_STATE_SLEEP)
				    || (priv->psstate == PS_STATE_PRE_SLEEP)
1269 1270 1271
				    ) {
					/* w/ new scheme, it will not reach here.
					   since it is blocked in main_thread. */
1272
					priv->needtowakeup = 1;
1273
				} else
1274
					lbs_ps_wakeup(priv, 0);
1275 1276 1277 1278 1279 1280 1281 1282

				ret = 0;
				goto done;
			} else {
				/*
				 * PS command. Ignore it if it is not Exit_PS.
				 * otherwise send it down immediately.
				 */
1283
				struct cmd_ds_802_11_ps_mode *psm = (void *)&cmd[1];
1284

1285 1286
				lbs_deb_host(
				       "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1287 1288
				       psm->action);
				if (psm->action !=
1289
				    cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1290 1291
					lbs_deb_host(
					       "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1292
					list_del(&cmdnode->list);
1293 1294 1295
					spin_lock_irqsave(&priv->driver_lock, flags);
					lbs_complete_command(priv, cmdnode, 0);
					spin_unlock_irqrestore(&priv->driver_lock, flags);
1296 1297 1298 1299 1300

					ret = 0;
					goto done;
				}

1301 1302
				if ((priv->psstate == PS_STATE_SLEEP) ||
				    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1303 1304
					lbs_deb_host(
					       "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1305
					list_del(&cmdnode->list);
1306 1307 1308
					spin_lock_irqsave(&priv->driver_lock, flags);
					lbs_complete_command(priv, cmdnode, 0);
					spin_unlock_irqrestore(&priv->driver_lock, flags);
1309
					priv->needtowakeup = 1;
1310 1311 1312 1313 1314

					ret = 0;
					goto done;
				}

1315 1316
				lbs_deb_host(
				       "EXEC_NEXT_CMD: sending EXIT_PS\n");
1317 1318
			}
		}
1319
		list_del(&cmdnode->list);
1320
		lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1321
			    le16_to_cpu(cmd->command));
1322
		lbs_submit_command(priv, cmdnode);
1323 1324 1325 1326 1327
	} else {
		/*
		 * check if in power save mode, if yes, put the device back
		 * to PS mode
		 */
1328 1329 1330
		if ((priv->psmode != LBS802_11POWERMODECAM) &&
		    (priv->psstate == PS_STATE_FULL_POWER) &&
		    ((priv->connect_status == LBS_CONNECTED) ||
1331
		    lbs_mesh_connected(priv))) {
1332 1333
			if (priv->secinfo.WPAenabled ||
			    priv->secinfo.WPA2enabled) {
1334
				/* check for valid WPA group keys */
1335 1336
				if (priv->wpa_mcast_key.len ||
				    priv->wpa_unicast_key.len) {
1337
					lbs_deb_host(
1338 1339
					       "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
					       " go back to PS_SLEEP");
1340
					lbs_ps_sleep(priv, 0);
1341 1342
				}
			} else {
1343 1344 1345
				lbs_deb_host(
				       "EXEC_NEXT_CMD: cmdpendingq empty, "
				       "go back to PS_SLEEP");
1346
				lbs_ps_sleep(priv, 0);
1347 1348 1349 1350 1351 1352
			}
		}
	}

	ret = 0;
done:
1353
	lbs_deb_leave(LBS_DEB_THREAD);
1354 1355 1356
	return ret;
}

1357
static void lbs_send_confirmsleep(struct lbs_private *priv)
1358 1359
{
	unsigned long flags;
1360
	int ret;
1361

1362
	lbs_deb_enter(LBS_DEB_HOST);
1363 1364
	lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
		sizeof(confirm_sleep));
1365

1366 1367
	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
		sizeof(confirm_sleep));
1368
	if (ret) {
1369
		lbs_pr_alert("confirm_sleep failed\n");
1370
		goto out;
1371
	}
1372 1373 1374

	spin_lock_irqsave(&priv->driver_lock, flags);

1375 1376 1377
	/* We don't get a response on the sleep-confirmation */
	priv->dnld_sent = DNLD_RES_RECEIVED;

1378 1379 1380 1381 1382
	if (priv->is_host_sleep_configured) {
		priv->is_host_sleep_activated = 1;
		wake_up_interruptible(&priv->host_sleep_q);
	}

1383
	/* If nothing to do, go back to sleep (?) */
S
Stefani Seibold 已提交
1384
	if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1385 1386 1387 1388 1389
		priv->psstate = PS_STATE_SLEEP;

	spin_unlock_irqrestore(&priv->driver_lock, flags);

out:
1390
	lbs_deb_leave(LBS_DEB_HOST);
1391 1392
}

1393
void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1394
{
1395
	lbs_deb_enter(LBS_DEB_HOST);
1396 1397 1398 1399 1400 1401

	/*
	 * PS is currently supported only in Infrastructure mode
	 * Remove this check if it is to be supported in IBSS mode also
	 */

1402
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1403
			      CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1404

1405
	lbs_deb_leave(LBS_DEB_HOST);
1406 1407 1408
}

/**
1409
 *  @brief This function sends Exit_PS command to firmware.
1410
 *
1411
 *  @param priv    	A pointer to struct lbs_private structure
1412 1413 1414
 *  @param wait_option	wait response or not
 *  @return 	   	n/a
 */
1415
void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1416
{
1417
	__le32 Localpsmode;
1418

1419
	lbs_deb_enter(LBS_DEB_HOST);
1420

1421
	Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1422

1423
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1424
			      CMD_SUBCMD_EXIT_PS,
1425 1426
			      wait_option, 0, &Localpsmode);

1427
	lbs_deb_leave(LBS_DEB_HOST);
1428 1429 1430 1431 1432 1433
}

/**
 *  @brief This function checks condition and prepares to
 *  send sleep confirm command to firmware if ok.
 *
1434
 *  @param priv    	A pointer to struct lbs_private structure
1435 1436 1437
 *  @param psmode  	Power Saving mode
 *  @return 	   	n/a
 */
1438
void lbs_ps_confirm_sleep(struct lbs_private *priv)
1439 1440
{
	unsigned long flags =0;
1441
	int allowed = 1;
1442

1443
	lbs_deb_enter(LBS_DEB_HOST);
1444

1445
	spin_lock_irqsave(&priv->driver_lock, flags);
1446
	if (priv->dnld_sent) {
1447
		allowed = 0;
1448
		lbs_deb_host("dnld_sent was set\n");
1449 1450
	}

1451
	/* In-progress command? */
1452
	if (priv->cur_cmd) {
1453
		allowed = 0;
1454
		lbs_deb_host("cur_cmd was set\n");
1455
	}
1456 1457

	/* Pending events or command responses? */
S
Stefani Seibold 已提交
1458
	if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1459
		allowed = 0;
1460
		lbs_deb_host("pending events or command responses\n");
1461
	}
1462
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1463 1464

	if (allowed) {
1465
		lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1466
		lbs_send_confirmsleep(priv);
1467
	} else {
1468
		lbs_deb_host("sleep confirm has been delayed\n");
1469 1470
	}

1471
	lbs_deb_leave(LBS_DEB_HOST);
1472
}
1473 1474


1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
/**
 * @brief Configures the transmission power control functionality.
 *
 * @param priv		A pointer to struct lbs_private structure
 * @param enable	Transmission power control enable
 * @param p0		Power level when link quality is good (dBm).
 * @param p1		Power level when link quality is fair (dBm).
 * @param p2		Power level when link quality is poor (dBm).
 * @param usesnr	Use Signal to Noise Ratio in TPC
 *
 * @return 0 on success
 */
int lbs_set_tpc_cfg(struct lbs_private *priv, int enable, int8_t p0, int8_t p1,
		int8_t p2, int usesnr)
{
	struct cmd_ds_802_11_tpc_cfg cmd;
	int ret;

	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_SET);
	cmd.enable = !!enable;
1497
	cmd.usesnr = !!usesnr;
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	cmd.P0 = p0;
	cmd.P1 = p1;
	cmd.P2 = p2;

	ret = lbs_cmd_with_response(priv, CMD_802_11_TPC_CFG, &cmd);

	return ret;
}

/**
 * @brief Configures the power adaptation settings.
 *
 * @param priv		A pointer to struct lbs_private structure
 * @param enable	Power adaptation enable
 * @param p0		Power level for 1, 2, 5.5 and 11 Mbps (dBm).
 * @param p1		Power level for 6, 9, 12, 18, 22, 24 and 36 Mbps (dBm).
 * @param p2		Power level for 48 and 54 Mbps (dBm).
 *
 * @return 0 on Success
 */

int lbs_set_power_adapt_cfg(struct lbs_private *priv, int enable, int8_t p0,
		int8_t p1, int8_t p2)
{
	struct cmd_ds_802_11_pa_cfg cmd;
	int ret;

	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	cmd.action = cpu_to_le16(CMD_ACT_SET);
	cmd.enable = !!enable;
	cmd.P0 = p0;
	cmd.P1 = p1;
	cmd.P2 = p2;

	ret = lbs_cmd_with_response(priv, CMD_802_11_PA_CFG , &cmd);

	return ret;
}


1539
struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1540 1541 1542
	uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
	int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
	unsigned long callback_arg)
1543 1544 1545 1546 1547
{
	struct cmd_ctrl_node *cmdnode;

	lbs_deb_enter(LBS_DEB_HOST);

1548
	if (priv->surpriseremoved) {
1549
		lbs_deb_host("PREP_CMD: card removed\n");
1550
		cmdnode = ERR_PTR(-ENOENT);
1551 1552 1553
		goto done;
	}

1554 1555 1556 1557 1558
	if (!lbs_is_cmd_allowed(priv)) {
		cmdnode = ERR_PTR(-EBUSY);
		goto done;
	}

1559 1560 1561 1562 1563 1564
	cmdnode = lbs_get_cmd_ctrl_node(priv);
	if (cmdnode == NULL) {
		lbs_deb_host("PREP_CMD: cmdnode is NULL\n");

		/* Wake up main thread to execute next command */
		wake_up_interruptible(&priv->waitq);
1565
		cmdnode = ERR_PTR(-ENOBUFS);
1566 1567 1568
		goto done;
	}

1569
	cmdnode->callback = callback;
1570
	cmdnode->callback_arg = callback_arg;
1571

1572
	/* Copy the incoming command to the buffer */
1573
	memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1574

1575
	/* Set sequence number, clean result, move to buffer */
1576
	priv->seqnum++;
1577 1578 1579 1580
	cmdnode->cmdbuf->command = cpu_to_le16(command);
	cmdnode->cmdbuf->size    = cpu_to_le16(in_cmd_size);
	cmdnode->cmdbuf->seqnum  = cpu_to_le16(priv->seqnum);
	cmdnode->cmdbuf->result  = 0;
1581 1582 1583 1584

	lbs_deb_host("PREP_CMD: command 0x%04x\n", command);

	cmdnode->cmdwaitqwoken = 0;
1585
	lbs_queue_cmd(priv, cmdnode);
1586 1587
	wake_up_interruptible(&priv->waitq);

1588 1589 1590 1591 1592
 done:
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %p", cmdnode);
	return cmdnode;
}

1593 1594 1595 1596 1597 1598 1599 1600 1601
void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
	struct cmd_header *in_cmd, int in_cmd_size)
{
	lbs_deb_enter(LBS_DEB_CMD);
	__lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
		lbs_cmd_async_callback, 0);
	lbs_deb_leave(LBS_DEB_CMD);
}

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
int __lbs_cmd(struct lbs_private *priv, uint16_t command,
	      struct cmd_header *in_cmd, int in_cmd_size,
	      int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
	      unsigned long callback_arg)
{
	struct cmd_ctrl_node *cmdnode;
	unsigned long flags;
	int ret = 0;

	lbs_deb_enter(LBS_DEB_HOST);

	cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
				  callback, callback_arg);
	if (IS_ERR(cmdnode)) {
		ret = PTR_ERR(cmdnode);
		goto done;
	}

1620 1621 1622
	might_sleep();
	wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);

1623
	spin_lock_irqsave(&priv->driver_lock, flags);
1624 1625 1626 1627
	ret = cmdnode->result;
	if (ret)
		lbs_pr_info("PREP_CMD: command 0x%04x failed: %d\n",
			    command, ret);
1628

1629
	__lbs_cleanup_and_insert_cmd(priv, cmdnode);
1630
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1631 1632 1633 1634 1635

done:
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
	return ret;
}
1636
EXPORT_SYMBOL_GPL(__lbs_cmd);