File Index
Files and directories
Sign in
FILE

cmd.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/marvell/libertas/cmd.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This file contains the handling of command.
   4 * It prepares command and sends it to firmware when it is ready.
   5 */
   6
   7#include <linux/hardirq.h>
   8#include <linux/kfifo.h>
   9#include <linux/sched.h>
  10#include <linux/slab.h>
  11#include <linux/if_arp.h>
  12#include <linux/export.h>
  13
  14#include "decl.h"
  15#include "cfg.h"
  16#include "cmd.h"
  17
  18#define CAL_NF(nf)		((s32)(-(s32)(nf)))
  19#define CAL_RSSI(snr, nf)	((s32)((s32)(snr) + CAL_NF(nf)))
  20
  21/**
  22 * lbs_cmd_copyback - Simple callback that copies response back into command
  23 *
  24 * @priv:	A pointer to &struct lbs_private structure
  25 * @extra:	A pointer to the original command structure for which
  26 *		'resp' is a response
  27 * @resp:	A pointer to the command response
  28 *
  29 * returns:	0 on success, error on failure
  30 */
  31int lbs_cmd_copyback(struct lbs_private *priv, unsigned long extra,
  32		     struct cmd_header *resp)
  33{
  34	struct cmd_header *buf = (void *)extra;
  35	uint16_t copy_len;
  36
  37	copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  38	memcpy(buf, resp, copy_len);
  39	return 0;
  40}
  41EXPORT_SYMBOL_GPL(lbs_cmd_copyback);
  42
  43/**
  44 *  lbs_cmd_async_callback - Simple callback that ignores the result.
  45 *  Use this if you just want to send a command to the hardware, but don't
  46 *  care for the result.
  47 *
  48 *  @priv:	ignored
  49 *  @extra:	ignored
  50 *  @resp:	ignored
  51 *
  52 *  returns:	0 for success
  53 */
  54static int lbs_cmd_async_callback(struct lbs_private *priv, unsigned long extra,
  55		     struct cmd_header *resp)
  56{
  57	return 0;
  58}
  59
  60
  61/**
  62 *  is_command_allowed_in_ps - tests if a command is allowed in Power Save mode
  63 *
  64 *  @cmd:	the command ID
  65 *
  66 *  returns:	1 if allowed, 0 if not allowed
  67 */
  68static u8 is_command_allowed_in_ps(u16 cmd)
  69{
  70	switch (cmd) {
  71	case CMD_802_11_RSSI:
  72		return 1;
  73	case CMD_802_11_HOST_SLEEP_CFG:
  74		return 1;
  75	default:
  76		break;
  77	}
  78	return 0;
  79}
  80
  81/**
  82 *  lbs_update_hw_spec - Updates the hardware details like MAC address
  83 *  and regulatory region
  84 *
  85 *  @priv:	A pointer to &struct lbs_private structure
  86 *
  87 *  returns:	0 on success, error on failure
  88 */
  89int lbs_update_hw_spec(struct lbs_private *priv)
  90{
  91	struct cmd_ds_get_hw_spec cmd;
  92	int ret = -1;
  93	u32 i;
  94
  95	memset(&cmd, 0, sizeof(cmd));
  96	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  97	memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  98	ret = lbs_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  99	if (ret)
 100		goto out;
 101
 102	priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
 103
 104	/* The firmware release is in an interesting format: the patch
 105	 * level is in the most significant nibble ... so fix that: */
 106	priv->fwrelease = le32_to_cpu(cmd.fwrelease);
 107	priv->fwrelease = (priv->fwrelease << 8) |
 108		(priv->fwrelease >> 24 & 0xff);
 109
 110	/* Some firmware capabilities:
 111	 * CF card    firmware 5.0.16p0:   cap 0x00000303
 112	 * USB dongle firmware 5.110.17p2: cap 0x00000303
 113	 */
 114	netdev_info(priv->dev, "%pM, fw %u.%u.%up%u, cap 0x%08x\n",
 115		cmd.permanentaddr,
 116		priv->fwrelease >> 24 & 0xff,
 117		priv->fwrelease >> 16 & 0xff,
 118		priv->fwrelease >>  8 & 0xff,
 119		priv->fwrelease       & 0xff,
 120		priv->fwcapinfo);
 121	lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
 122		    cmd.hwifversion, cmd.version);
 123
 124	/* Clamp region code to 8-bit since FW spec indicates that it should
 125	 * only ever be 8-bit, even though the field size is 16-bit.  Some firmware
 126	 * returns non-zero high 8 bits here.
 127	 *
 128	 * Firmware version 4.0.102 used in CF8381 has region code shifted.  We
 129	 * need to check for this problem and handle it properly.
 130	 */
 131	if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V4)
 132		priv->regioncode = (le16_to_cpu(cmd.regioncode) >> 8) & 0xFF;
 133	else
 134		priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
 135
 136	for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
 137		/* use the region code to search for the index */
 138		if (priv->regioncode == lbs_region_code_to_index[i])
 139			break;
 140	}
 141
 142	/* if it's unidentified region code, use the default (USA) */
 143	if (i >= MRVDRV_MAX_REGION_CODE) {
 144		priv->regioncode = 0x10;
 145		netdev_info(priv->dev,
 146			    "unidentified region code; using the default (USA)\n");
 147	}
 148
 149	if (priv->current_addr[0] == 0xff)
 150		memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
 151
 152	if (!priv->copied_hwaddr) {
 153		eth_hw_addr_set(priv->dev, priv->current_addr);
 154		if (priv->mesh_dev)
 155			eth_hw_addr_set(priv->mesh_dev, priv->current_addr);
 156		priv->copied_hwaddr = 1;
 157	}
 158
 159out:
 160	return ret;
 161}
 162
 163static int lbs_ret_host_sleep_cfg(struct lbs_private *priv, unsigned long dummy,
 164			struct cmd_header *resp)
 165{
 166	if (priv->is_host_sleep_activated) {
 167		priv->is_host_sleep_configured = 0;
 168		if (priv->psstate == PS_STATE_FULL_POWER) {
 169			priv->is_host_sleep_activated = 0;
 170			wake_up_interruptible(&priv->host_sleep_q);
 171		}
 172	} else {
 173		priv->is_host_sleep_configured = 1;
 174	}
 175
 176	return 0;
 177}
 178
 179int lbs_host_sleep_cfg(struct lbs_private *priv, uint32_t criteria,
 180		struct wol_config *p_wol_config)
 181{
 182	struct cmd_ds_host_sleep cmd_config;
 183	int ret;
 184
 185	/*
 186	 * Certain firmware versions do not support EHS_REMOVE_WAKEUP command
 187	 * and the card will return a failure.  Since we need to be
 188	 * able to reset the mask, in those cases we set a 0 mask instead.
 189	 */
 190	if (criteria == EHS_REMOVE_WAKEUP && !priv->ehs_remove_supported)
 191		criteria = 0;
 192
 193	cmd_config.hdr.size = cpu_to_le16(sizeof(cmd_config));
 194	cmd_config.criteria = cpu_to_le32(criteria);
 195	cmd_config.gpio = priv->wol_gpio;
 196	cmd_config.gap = priv->wol_gap;
 197
 198	if (p_wol_config != NULL)
 199		memcpy((uint8_t *)&cmd_config.wol_conf, (uint8_t *)p_wol_config,
 200				sizeof(struct wol_config));
 201	else
 202		cmd_config.wol_conf.action = CMD_ACT_ACTION_NONE;
 203
 204	ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_CFG, &cmd_config.hdr,
 205			le16_to_cpu(cmd_config.hdr.size),
 206			lbs_ret_host_sleep_cfg, 0);
 207	if (!ret) {
 208		if (p_wol_config)
 209			memcpy((uint8_t *) p_wol_config,
 210					(uint8_t *)&cmd_config.wol_conf,
 211					sizeof(struct wol_config));
 212	} else {
 213		netdev_info(priv->dev, "HOST_SLEEP_CFG failed %d\n", ret);
 214	}
 215
 216	return ret;
 217}
 218EXPORT_SYMBOL_GPL(lbs_host_sleep_cfg);
 219
 220/**
 221 *  lbs_set_ps_mode - Sets the Power Save mode
 222 *
 223 *  @priv:	A pointer to &struct lbs_private structure
 224 *  @cmd_action: The Power Save operation (PS_MODE_ACTION_ENTER_PS or
 225 *                         PS_MODE_ACTION_EXIT_PS)
 226 *  @block:	Whether to block on a response or not
 227 *
 228 *  returns:	0 on success, error on failure
 229 */
 230int lbs_set_ps_mode(struct lbs_private *priv, u16 cmd_action, bool block)
 231{
 232	struct cmd_ds_802_11_ps_mode cmd;
 233	int ret = 0;
 234
 235	memset(&cmd, 0, sizeof(cmd));
 236	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 237	cmd.action = cpu_to_le16(cmd_action);
 238
 239	if (cmd_action == PS_MODE_ACTION_ENTER_PS) {
 240		lbs_deb_cmd("PS_MODE: action ENTER_PS\n");
 241		cmd.multipledtim = cpu_to_le16(1);  /* Default DTIM multiple */
 242	} else if (cmd_action == PS_MODE_ACTION_EXIT_PS) {
 243		lbs_deb_cmd("PS_MODE: action EXIT_PS\n");
 244	} else {
 245		/* We don't handle CONFIRM_SLEEP here because it needs to
 246		 * be fastpathed to the firmware.
 247		 */
 248		lbs_deb_cmd("PS_MODE: unknown action 0x%X\n", cmd_action);
 249		ret = -EOPNOTSUPP;
 250		goto out;
 251	}
 252
 253	if (block)
 254		ret = lbs_cmd_with_response(priv, CMD_802_11_PS_MODE, &cmd);
 255	else
 256		lbs_cmd_async(priv, CMD_802_11_PS_MODE, &cmd.hdr, sizeof (cmd));
 257
 258out:
 259	return ret;
 260}
 261
 262int lbs_cmd_802_11_sleep_params(struct lbs_private *priv, uint16_t cmd_action,
 263				struct sleep_params *sp)
 264{
 265	struct cmd_ds_802_11_sleep_params cmd;
 266	int ret;
 267
 268	if (cmd_action == CMD_ACT_GET) {
 269		memset(&cmd, 0, sizeof(cmd));
 270	} else {
 271		cmd.error = cpu_to_le16(sp->sp_error);
 272		cmd.offset = cpu_to_le16(sp->sp_offset);
 273		cmd.stabletime = cpu_to_le16(sp->sp_stabletime);
 274		cmd.calcontrol = sp->sp_calcontrol;
 275		cmd.externalsleepclk = sp->sp_extsleepclk;
 276		cmd.reserved = cpu_to_le16(sp->sp_reserved);
 277	}
 278	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 279	cmd.action = cpu_to_le16(cmd_action);
 280
 281	ret = lbs_cmd_with_response(priv, CMD_802_11_SLEEP_PARAMS, &cmd);
 282
 283	if (!ret) {
 284		lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, "
 285			    "calcontrol 0x%x extsleepclk 0x%x\n",
 286			    le16_to_cpu(cmd.error), le16_to_cpu(cmd.offset),
 287			    le16_to_cpu(cmd.stabletime), cmd.calcontrol,
 288			    cmd.externalsleepclk);
 289
 290		sp->sp_error = le16_to_cpu(cmd.error);
 291		sp->sp_offset = le16_to_cpu(cmd.offset);
 292		sp->sp_stabletime = le16_to_cpu(cmd.stabletime);
 293		sp->sp_calcontrol = cmd.calcontrol;
 294		sp->sp_extsleepclk = cmd.externalsleepclk;
 295		sp->sp_reserved = le16_to_cpu(cmd.reserved);
 296	}
 297
 298	return ret;
 299}
 300
 301static int lbs_wait_for_ds_awake(struct lbs_private *priv)
 302{
 303	int ret = 0;
 304
 305	if (priv->is_deep_sleep) {
 306		if (!wait_event_interruptible_timeout(priv->ds_awake_q,
 307					!priv->is_deep_sleep, (10 * HZ))) {
 308			netdev_err(priv->dev, "ds_awake_q: timer expired\n");
 309			ret = -1;
 310		}
 311	}
 312
 313	return ret;
 314}
 315
 316int lbs_set_deep_sleep(struct lbs_private *priv, int deep_sleep)
 317{
 318	int ret =  0;
 319
 320	if (deep_sleep) {
 321		if (priv->is_deep_sleep != 1) {
 322			lbs_deb_cmd("deep sleep: sleep\n");
 323			BUG_ON(!priv->enter_deep_sleep);
 324			ret = priv->enter_deep_sleep(priv);
 325			if (!ret) {
 326				netif_stop_queue(priv->dev);
 327				netif_carrier_off(priv->dev);
 328			}
 329		} else {
 330			netdev_err(priv->dev, "deep sleep: already enabled\n");
 331		}
 332	} else {
 333		if (priv->is_deep_sleep) {
 334			lbs_deb_cmd("deep sleep: wakeup\n");
 335			BUG_ON(!priv->exit_deep_sleep);
 336			ret = priv->exit_deep_sleep(priv);
 337			if (!ret) {
 338				ret = lbs_wait_for_ds_awake(priv);
 339				if (ret)
 340					netdev_err(priv->dev,
 341						   "deep sleep: wakeup failed\n");
 342			}
 343		}
 344	}
 345
 346	return ret;
 347}
 348
 349static int lbs_ret_host_sleep_activate(struct lbs_private *priv,
 350		unsigned long dummy,
 351		struct cmd_header *cmd)
 352{
 353	priv->is_host_sleep_activated = 1;
 354	wake_up_interruptible(&priv->host_sleep_q);
 355
 356	return 0;
 357}
 358
 359int lbs_set_host_sleep(struct lbs_private *priv, int host_sleep)
 360{
 361	struct cmd_header cmd;
 362	int ret = 0;
 363	uint32_t criteria = EHS_REMOVE_WAKEUP;
 364
 365	if (host_sleep) {
 366		if (priv->is_host_sleep_activated != 1) {
 367			memset(&cmd, 0, sizeof(cmd));
 368			ret = lbs_host_sleep_cfg(priv, priv->wol_criteria,
 369					(struct wol_config *)NULL);
 370			if (ret) {
 371				netdev_info(priv->dev,
 372					    "Host sleep configuration failed: %d\n",
 373					    ret);
 374				return ret;
 375			}
 376			if (priv->psstate == PS_STATE_FULL_POWER) {
 377				ret = __lbs_cmd(priv,
 378						CMD_802_11_HOST_SLEEP_ACTIVATE,
 379						&cmd,
 380						sizeof(cmd),
 381						lbs_ret_host_sleep_activate, 0);
 382				if (ret)
 383					netdev_info(priv->dev,
 384						    "HOST_SLEEP_ACTIVATE failed: %d\n",
 385						    ret);
 386			}
 387
 388			if (!wait_event_interruptible_timeout(
 389						priv->host_sleep_q,
 390						priv->is_host_sleep_activated,
 391						(10 * HZ))) {
 392				netdev_err(priv->dev,
 393					   "host_sleep_q: timer expired\n");
 394				ret = -1;
 395			}
 396		} else {
 397			netdev_err(priv->dev, "host sleep: already enabled\n");
 398		}
 399	} else {
 400		if (priv->is_host_sleep_activated)
 401			ret = lbs_host_sleep_cfg(priv, criteria,
 402					(struct wol_config *)NULL);
 403	}
 404
 405	return ret;
 406}
 407
 408/**
 409 *  lbs_set_snmp_mib - Set an SNMP MIB value
 410 *
 411 *  @priv:	A pointer to &struct lbs_private structure
 412 *  @oid:	The OID to set in the firmware
 413 *  @val:	Value to set the OID to
 414 *
 415 *  returns: 	   	0 on success, error on failure
 416 */
 417int lbs_set_snmp_mib(struct lbs_private *priv, u32 oid, u16 val)
 418{
 419	struct cmd_ds_802_11_snmp_mib cmd;
 420	int ret;
 421
 422	memset(&cmd, 0, sizeof (cmd));
 423	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 424	cmd.action = cpu_to_le16(CMD_ACT_SET);
 425	cmd.oid = cpu_to_le16((u16) oid);
 426
 427	switch (oid) {
 428	case SNMP_MIB_OID_BSS_TYPE:
 429		cmd.bufsize = cpu_to_le16(sizeof(u8));
 430		cmd.value[0] = val;
 431		break;
 432	case SNMP_MIB_OID_11D_ENABLE:
 433	case SNMP_MIB_OID_FRAG_THRESHOLD:
 434	case SNMP_MIB_OID_RTS_THRESHOLD:
 435	case SNMP_MIB_OID_SHORT_RETRY_LIMIT:
 436	case SNMP_MIB_OID_LONG_RETRY_LIMIT:
 437		cmd.bufsize = cpu_to_le16(sizeof(u16));
 438		*((__le16 *)(&cmd.value)) = cpu_to_le16(val);
 439		break;
 440	default:
 441		lbs_deb_cmd("SNMP_CMD: (set) unhandled OID 0x%x\n", oid);
 442		ret = -EINVAL;
 443		goto out;
 444	}
 445
 446	lbs_deb_cmd("SNMP_CMD: (set) oid 0x%x, oid size 0x%x, value 0x%x\n",
 447		    le16_to_cpu(cmd.oid), le16_to_cpu(cmd.bufsize), val);
 448
 449	ret = lbs_cmd_with_response(priv, CMD_802_11_SNMP_MIB, &cmd);
 450
 451out:
 452	return ret;
 453}
 454
 455/**
 456 *  lbs_get_tx_power - Get the min, max, and current TX power
 457 *
 458 *  @priv:	A pointer to &struct lbs_private structure
 459 *  @curlevel:	Current power level in dBm
 460 *  @minlevel:	Minimum supported power level in dBm (optional)
 461 *  @maxlevel:	Maximum supported power level in dBm (optional)
 462 *
 463 *  returns:	0 on success, error on failure
 464 */
 465int lbs_get_tx_power(struct lbs_private *priv, s16 *curlevel, s16 *minlevel,
 466		     s16 *maxlevel)
 467{
 468	struct cmd_ds_802_11_rf_tx_power cmd;
 469	int ret;
 470
 471	memset(&cmd, 0, sizeof(cmd));
 472	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 473	cmd.action = cpu_to_le16(CMD_ACT_GET);
 474
 475	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_TX_POWER, &cmd);
 476	if (ret == 0) {
 477		*curlevel = le16_to_cpu(cmd.curlevel);
 478		if (minlevel)
 479			*minlevel = cmd.minlevel;
 480		if (maxlevel)
 481			*maxlevel = cmd.maxlevel;
 482	}
 483
 484	return ret;
 485}
 486
 487/**
 488 *  lbs_set_monitor_mode - Enable or disable monitor mode
 489 *  (only implemented on OLPC usb8388 FW)
 490 *
 491 *  @priv:	A pointer to &struct lbs_private structure
 492 *  @enable:	1 to enable monitor mode, 0 to disable
 493 *
 494 *  returns:	0 on success, error on failure
 495 */
 496int lbs_set_monitor_mode(struct lbs_private *priv, int enable)
 497{
 498	struct cmd_ds_802_11_monitor_mode cmd;
 499	int ret;
 500
 501	memset(&cmd, 0, sizeof(cmd));
 502	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 503	cmd.action = cpu_to_le16(CMD_ACT_SET);
 504	if (enable)
 505		cmd.mode = cpu_to_le16(0x1);
 506
 507	lbs_deb_cmd("SET_MONITOR_MODE: %d\n", enable);
 508
 509	ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
 510	if (ret == 0) {
 511		priv->dev->type = enable ? ARPHRD_IEEE80211_RADIOTAP :
 512						ARPHRD_ETHER;
 513	}
 514
 515	return ret;
 516}
 517
 518/**
 519 *  lbs_get_channel - Get the radio channel
 520 *
 521 *  @priv:	A pointer to &struct lbs_private structure
 522 *
 523 *  returns:	The channel on success, error on failure
 524 */
 525static int lbs_get_channel(struct lbs_private *priv)
 526{
 527	struct cmd_ds_802_11_rf_channel cmd;
 528	int ret = 0;
 529
 530	memset(&cmd, 0, sizeof(cmd));
 531	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 532	cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_GET);
 533
 534	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
 535	if (ret)
 536		goto out;
 537
 538	ret = le16_to_cpu(cmd.channel);
 539	lbs_deb_cmd("current radio channel is %d\n", ret);
 540
 541out:
 542	return ret;
 543}
 544
 545int lbs_update_channel(struct lbs_private *priv)
 546{
 547	int ret;
 548
 549	/* the channel in f/w could be out of sync; get the current channel */
 550	ret = lbs_get_channel(priv);
 551	if (ret > 0) {
 552		priv->channel = ret;
 553		ret = 0;
 554	}
 555
 556	return ret;
 557}
 558
 559/**
 560 *  lbs_set_channel - Set the radio channel
 561 *
 562 *  @priv:	A pointer to &struct lbs_private structure
 563 *  @channel:	The desired channel, or 0 to clear a locked channel
 564 *
 565 *  returns:	0 on success, error on failure
 566 */
 567int lbs_set_channel(struct lbs_private *priv, u8 channel)
 568{
 569	struct cmd_ds_802_11_rf_channel cmd;
 570#ifdef DEBUG
 571	u8 old_channel = priv->channel;
 572#endif
 573	int ret = 0;
 574
 575	memset(&cmd, 0, sizeof(cmd));
 576	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 577	cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
 578	cmd.channel = cpu_to_le16(channel);
 579
 580	ret = lbs_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
 581	if (ret)
 582		goto out;
 583
 584	priv->channel = (uint8_t) le16_to_cpu(cmd.channel);
 585	lbs_deb_cmd("channel switch from %d to %d\n", old_channel,
 586		priv->channel);
 587
 588out:
 589	return ret;
 590}
 591
 592/**
 593 * lbs_get_rssi - Get current RSSI and noise floor
 594 *
 595 * @priv:	A pointer to &struct lbs_private structure
 596 * @rssi:	On successful return, signal level in mBm
 597 * @nf:		On successful return, Noise floor
 598 *
 599 * returns:	The channel on success, error on failure
 600 */
 601int lbs_get_rssi(struct lbs_private *priv, s8 *rssi, s8 *nf)
 602{
 603	struct cmd_ds_802_11_rssi cmd;
 604	int ret = 0;
 605
 606	BUG_ON(rssi == NULL);
 607	BUG_ON(nf == NULL);
 608
 609	memset(&cmd, 0, sizeof(cmd));
 610	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 611	/* Average SNR over last 8 beacons */
 612	cmd.n_or_snr = cpu_to_le16(8);
 613
 614	ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
 615	if (ret == 0) {
 616		*nf = CAL_NF(le16_to_cpu(cmd.nf));
 617		*rssi = CAL_RSSI(le16_to_cpu(cmd.n_or_snr), le16_to_cpu(cmd.nf));
 618	}
 619
 620	return ret;
 621}
 622
 623/**
 624 *  lbs_set_11d_domain_info - Send regulatory and 802.11d domain information
 625 *  to the firmware
 626 *
 627 *  @priv:	pointer to &struct lbs_private
 628 *
 629 *  returns:	0 on success, error code on failure
 630*/
 631int lbs_set_11d_domain_info(struct lbs_private *priv)
 632{
 633	struct wiphy *wiphy = priv->wdev->wiphy;
 634	struct ieee80211_supported_band **bands = wiphy->bands;
 635	struct cmd_ds_802_11d_domain_info cmd;
 636	struct mrvl_ie_domain_param_set *domain = &cmd.domain;
 637	struct ieee80211_country_ie_triplet *t;
 638	enum nl80211_band band;
 639	struct ieee80211_channel *ch;
 640	u8 num_triplet = 0;
 641	u8 num_parsed_chan = 0;
 642	u8 first_channel = 0, next_chan = 0, max_pwr = 0;
 643	u8 i, flag = 0;
 644	size_t triplet_size;
 645	int ret = 0;
 646
 647	if (!priv->country_code[0])
 648		goto out;
 649
 650	memset(&cmd, 0, sizeof(cmd));
 651	cmd.action = cpu_to_le16(CMD_ACT_SET);
 652
 653	lbs_deb_11d("Setting country code '%c%c'\n",
 654		    priv->country_code[0], priv->country_code[1]);
 655
 656	domain->header.type = cpu_to_le16(TLV_TYPE_DOMAIN);
 657
 658	/* Set country code */
 659	domain->country_code[0] = priv->country_code[0];
 660	domain->country_code[1] = priv->country_code[1];
 661	domain->country_code[2] = ' ';
 662
 663	/* Now set up the channel triplets; firmware is somewhat picky here
 664	 * and doesn't validate channel numbers and spans; hence it would
 665	 * interpret a triplet of (36, 4, 20) as channels 36, 37, 38, 39.  Since
 666	 * the last 3 aren't valid channels, the driver is responsible for
 667	 * splitting that up into 4 triplet pairs of (36, 1, 20) + (40, 1, 20)
 668	 * etc.
 669	 */
 670	for (band = 0;
 671	     (band < NUM_NL80211_BANDS) && (num_triplet < MAX_11D_TRIPLETS);
 672	     band++) {
 673
 674		if (!bands[band])
 675			continue;
 676
 677		for (i = 0;
 678		     (i < bands[band]->n_channels) && (num_triplet < MAX_11D_TRIPLETS);
 679		     i++) {
 680			ch = &bands[band]->channels[i];
 681			if (ch->flags & IEEE80211_CHAN_DISABLED)
 682				continue;
 683
 684			if (!flag) {
 685				flag = 1;
 686				next_chan = first_channel = (u32) ch->hw_value;
 687				max_pwr = ch->max_power;
 688				num_parsed_chan = 1;
 689				continue;
 690			}
 691
 692			if ((ch->hw_value == next_chan + 1) &&
 693					(ch->max_power == max_pwr)) {
 694				/* Consolidate adjacent channels */
 695				next_chan++;
 696				num_parsed_chan++;
 697			} else {
 698				/* Add this triplet */
 699				lbs_deb_11d("11D triplet (%d, %d, %d)\n",
 700					first_channel, num_parsed_chan,
 701					max_pwr);
 702				t = &domain->triplet[num_triplet];
 703				t->chans.first_channel = first_channel;
 704				t->chans.num_channels = num_parsed_chan;
 705				t->chans.max_power = max_pwr;
 706				num_triplet++;
 707				flag = 0;
 708			}
 709		}
 710
 711		if (flag) {
 712			/* Add last triplet */
 713			lbs_deb_11d("11D triplet (%d, %d, %d)\n", first_channel,
 714				num_parsed_chan, max_pwr);
 715			t = &domain->triplet[num_triplet];
 716			t->chans.first_channel = first_channel;
 717			t->chans.num_channels = num_parsed_chan;
 718			t->chans.max_power = max_pwr;
 719			num_triplet++;
 720		}
 721	}
 722
 723	lbs_deb_11d("# triplets %d\n", num_triplet);
 724
 725	/* Set command header sizes */
 726	triplet_size = num_triplet * sizeof(struct ieee80211_country_ie_triplet);
 727	domain->header.len = cpu_to_le16(sizeof(domain->country_code) +
 728					triplet_size);
 729
 730	lbs_deb_hex(LBS_DEB_11D, "802.11D domain param set",
 731			(u8 *) &cmd.domain.country_code,
 732			le16_to_cpu(domain->header.len));
 733
 734	cmd.hdr.size = cpu_to_le16(sizeof(cmd.hdr) +
 735				   sizeof(cmd.action) +
 736				   sizeof(cmd.domain.header) +
 737				   sizeof(cmd.domain.country_code) +
 738				   triplet_size);
 739
 740	ret = lbs_cmd_with_response(priv, CMD_802_11D_DOMAIN_INFO, &cmd);
 741
 742out:
 743	return ret;
 744}
 745
 746/**
 747 *  lbs_get_reg - Read a MAC, Baseband, or RF register
 748 *
 749 *  @priv:	pointer to &struct lbs_private
 750 *  @reg:	register command, one of CMD_MAC_REG_ACCESS,
 751 *		CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
 752 *  @offset:	byte offset of the register to get
 753 *  @value:	on success, the value of the register at 'offset'
 754 *
 755 *  returns:	0 on success, error code on failure
 756*/
 757int lbs_get_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 *value)
 758{
 759	struct cmd_ds_reg_access cmd;
 760	int ret = 0;
 761
 762	BUG_ON(value == NULL);
 763
 764	memset(&cmd, 0, sizeof(cmd));
 765	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 766	cmd.action = cpu_to_le16(CMD_ACT_GET);
 767	cmd.offset = cpu_to_le16(offset);
 768
 769	if (reg != CMD_MAC_REG_ACCESS &&
 770	    reg != CMD_BBP_REG_ACCESS &&
 771	    reg != CMD_RF_REG_ACCESS) {
 772		ret = -EINVAL;
 773		goto out;
 774	}
 775
 776	ret = lbs_cmd_with_response(priv, reg, &cmd);
 777	if (!ret) {
 778		if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
 779			*value = cmd.value.bbp_rf;
 780		else if (reg == CMD_MAC_REG_ACCESS)
 781			*value = le32_to_cpu(cmd.value.mac);
 782	}
 783
 784out:
 785	return ret;
 786}
 787
 788/**
 789 *  lbs_set_reg - Write a MAC, Baseband, or RF register
 790 *
 791 *  @priv:	pointer to &struct lbs_private
 792 *  @reg:	register command, one of CMD_MAC_REG_ACCESS,
 793 *		CMD_BBP_REG_ACCESS, or CMD_RF_REG_ACCESS
 794 *  @offset:	byte offset of the register to set
 795 *  @value:	the value to write to the register at 'offset'
 796 *
 797 *  returns:	0 on success, error code on failure
 798*/
 799int lbs_set_reg(struct lbs_private *priv, u16 reg, u16 offset, u32 value)
 800{
 801	struct cmd_ds_reg_access cmd;
 802	int ret = 0;
 803
 804	memset(&cmd, 0, sizeof(cmd));
 805	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 806	cmd.action = cpu_to_le16(CMD_ACT_SET);
 807	cmd.offset = cpu_to_le16(offset);
 808
 809	if (reg == CMD_BBP_REG_ACCESS || reg == CMD_RF_REG_ACCESS)
 810		cmd.value.bbp_rf = (u8) (value & 0xFF);
 811	else if (reg == CMD_MAC_REG_ACCESS)
 812		cmd.value.mac = cpu_to_le32(value);
 813	else {
 814		ret = -EINVAL;
 815		goto out;
 816	}
 817
 818	ret = lbs_cmd_with_response(priv, reg, &cmd);
 819
 820out:
 821	return ret;
 822}
 823
 824static void lbs_queue_cmd(struct lbs_private *priv,
 825			  struct cmd_ctrl_node *cmdnode)
 826{
 827	unsigned long flags;
 828	int addtail = 1;
 829
 830	if (!cmdnode) {
 831		lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
 832		return;
 833	}
 834	if (!cmdnode->cmdbuf->size) {
 835		lbs_deb_host("DNLD_CMD: cmd size is zero\n");
 836		return;
 837	}
 838	cmdnode->result = 0;
 839
 840	/* Exit_PS command needs to be queued in the header always. */
 841	if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_PS_MODE) {
 842		struct cmd_ds_802_11_ps_mode *psm = (void *)cmdnode->cmdbuf;
 843
 844		if (psm->action == cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
 845			if (priv->psstate != PS_STATE_FULL_POWER)
 846				addtail = 0;
 847		}
 848	}
 849
 850	if (le16_to_cpu(cmdnode->cmdbuf->command) == CMD_802_11_WAKEUP_CONFIRM)
 851		addtail = 0;
 852
 853	spin_lock_irqsave(&priv->driver_lock, flags);
 854
 855	if (addtail)
 856		list_add_tail(&cmdnode->list, &priv->cmdpendingq);
 857	else
 858		list_add(&cmdnode->list, &priv->cmdpendingq);
 859
 860	spin_unlock_irqrestore(&priv->driver_lock, flags);
 861
 862	lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
 863		     le16_to_cpu(cmdnode->cmdbuf->command));
 864}
 865
 866static void lbs_submit_command(struct lbs_private *priv,
 867			       struct cmd_ctrl_node *cmdnode)
 868{
 869	unsigned long flags;
 870	struct cmd_header *cmd;
 871	uint16_t cmdsize;
 872	uint16_t command;
 873	int timeo = 3 * HZ;
 874	int ret;
 875
 876	cmd = cmdnode->cmdbuf;
 877
 878	spin_lock_irqsave(&priv->driver_lock, flags);
 879	priv->seqnum++;
 880	cmd->seqnum = cpu_to_le16(priv->seqnum);
 881	priv->cur_cmd = cmdnode;
 882	spin_unlock_irqrestore(&priv->driver_lock, flags);
 883
 884	cmdsize = le16_to_cpu(cmd->size);
 885	command = le16_to_cpu(cmd->command);
 886
 887	/* These commands take longer */
 888	if (command == CMD_802_11_SCAN || command == CMD_802_11_ASSOCIATE)
 889		timeo = 5 * HZ;
 890
 891	lbs_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
 892		     command, le16_to_cpu(cmd->seqnum), cmdsize);
 893	lbs_deb_hex(LBS_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
 894
 895	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
 896
 897	if (ret) {
 898		netdev_info(priv->dev, "DNLD_CMD: hw_host_to_card failed: %d\n",
 899			    ret);
 900		/* Reset dnld state machine, report failure */
 901		priv->dnld_sent = DNLD_RES_RECEIVED;
 902		lbs_complete_command(priv, cmdnode, ret);
 903	}
 904
 905	if (command == CMD_802_11_DEEP_SLEEP) {
 906		priv->is_deep_sleep = 1;
 907		lbs_complete_command(priv, cmdnode, 0);
 908	} else {
 909		/* Setup the timer after transmit command */
 910		mod_timer(&priv->command_timer, jiffies + timeo);
 911	}
 912}
 913
 914/*
 915 *  This function inserts command node to cmdfreeq
 916 *  after cleans it. Requires priv->driver_lock held.
 917 */
 918static void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
 919					 struct cmd_ctrl_node *cmdnode)
 920{
 921	if (!cmdnode)
 922		return;
 923
 924	cmdnode->callback = NULL;
 925	cmdnode->callback_arg = 0;
 926
 927	memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
 928
 929	list_add_tail(&cmdnode->list, &priv->cmdfreeq);
 930}
 931
 932static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
 933	struct cmd_ctrl_node *ptempcmd)
 934{
 935	unsigned long flags;
 936
 937	spin_lock_irqsave(&priv->driver_lock, flags);
 938	__lbs_cleanup_and_insert_cmd(priv, ptempcmd);
 939	spin_unlock_irqrestore(&priv->driver_lock, flags);
 940}
 941
 942void __lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
 943			    int result)
 944{
 945	/*
 946	 * Normally, commands are removed from cmdpendingq before being
 947	 * submitted. However, we can arrive here on alternative codepaths
 948	 * where the command is still pending. Make sure the command really
 949	 * isn't part of a list at this point.
 950	 */
 951	list_del_init(&cmd->list);
 952
 953	cmd->result = result;
 954	cmd->cmdwaitqwoken = 1;
 955	wake_up(&cmd->cmdwait_q);
 956
 957	if (!cmd->callback || cmd->callback == lbs_cmd_async_callback)
 958		__lbs_cleanup_and_insert_cmd(priv, cmd);
 959	priv->cur_cmd = NULL;
 960	wake_up(&priv->waitq);
 961}
 962
 963void lbs_complete_command(struct lbs_private *priv, struct cmd_ctrl_node *cmd,
 964			  int result)
 965{
 966	unsigned long flags;
 967	spin_lock_irqsave(&priv->driver_lock, flags);
 968	__lbs_complete_command(priv, cmd, result);
 969	spin_unlock_irqrestore(&priv->driver_lock, flags);
 970}
 971
 972int lbs_set_radio(struct lbs_private *priv, u8 preamble, u8 radio_on)
 973{
 974	struct cmd_ds_802_11_radio_control cmd;
 975	int ret = -EINVAL;
 976
 977	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
 978	cmd.action = cpu_to_le16(CMD_ACT_SET);
 979	cmd.control = 0;
 980
 981	/* Only v8 and below support setting the preamble */
 982	if (priv->fwrelease < 0x09000000) {
 983		switch (preamble) {
 984		case RADIO_PREAMBLE_SHORT:
 985		case RADIO_PREAMBLE_AUTO:
 986		case RADIO_PREAMBLE_LONG:
 987			cmd.control = cpu_to_le16(preamble);
 988			break;
 989		default:
 990			goto out;
 991		}
 992	}
 993
 994	if (radio_on)
 995		cmd.control |= cpu_to_le16(0x1);
 996	else {
 997		cmd.control &= cpu_to_le16(~0x1);
 998		priv->txpower_cur = 0;
 999	}
1000
1001	lbs_deb_cmd("RADIO_CONTROL: radio %s, preamble %d\n",
1002		    radio_on ? "ON" : "OFF", preamble);
1003
1004	priv->radio_on = radio_on;
1005
1006	ret = lbs_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
1007
1008out:
1009	return ret;
1010}
1011
1012void lbs_set_mac_control(struct lbs_private *priv)
1013{
1014	struct cmd_ds_mac_control cmd;
1015
1016	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1017	cmd.action = cpu_to_le16(priv->mac_control);
1018	cmd.reserved = 0;
1019
1020	lbs_cmd_async(priv, CMD_MAC_CONTROL, &cmd.hdr, sizeof(cmd));
1021}
1022
1023int lbs_set_mac_control_sync(struct lbs_private *priv)
1024{
1025	struct cmd_ds_mac_control cmd;
1026	int ret = 0;
1027
1028	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1029	cmd.action = cpu_to_le16(priv->mac_control);
1030	cmd.reserved = 0;
1031	ret = lbs_cmd_with_response(priv, CMD_MAC_CONTROL, &cmd);
1032
1033	return ret;
1034}
1035
1036/**
1037 *  lbs_allocate_cmd_buffer - allocates the command buffer and links
1038 *  it to command free queue
1039 *
1040 *  @priv:	A pointer to &struct lbs_private structure
1041 *
1042 *  returns:	0 for success or -1 on error
1043 */
1044int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1045{
1046	int ret = 0;
1047	u32 bufsize;
1048	u32 i;
1049	struct cmd_ctrl_node *cmdarray;
1050
1051	/* Allocate and initialize the command array */
1052	bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
1053	if (!(cmdarray = kzalloc(bufsize, GFP_KERNEL))) {
1054		lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1055		ret = -1;
1056		goto done;
1057	}
1058	priv->cmd_array = cmdarray;
1059
1060	/* Allocate and initialize each command buffer in the command array */
1061	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1062		cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
1063		if (!cmdarray[i].cmdbuf) {
1064			lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1065			ret = -1;
1066			goto free_cmd_array;
1067		}
1068	}
1069
1070	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1071		init_waitqueue_head(&cmdarray[i].cmdwait_q);
1072		lbs_cleanup_and_insert_cmd(priv, &cmdarray[i]);
1073	}
1074	return 0;
1075
1076free_cmd_array:
1077	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1078		if (cmdarray[i].cmdbuf) {
1079			kfree(cmdarray[i].cmdbuf);
1080			cmdarray[i].cmdbuf = NULL;
1081		}
1082	}
1083	kfree(priv->cmd_array);
1084	priv->cmd_array = NULL;
1085done:
1086	return ret;
1087}
1088
1089/**
1090 *  lbs_free_cmd_buffer - free the command buffer
1091 *
1092 *  @priv:	A pointer to &struct lbs_private structure
1093 *
1094 *  returns:	0 for success
1095 */
1096int lbs_free_cmd_buffer(struct lbs_private *priv)
1097{
1098	struct cmd_ctrl_node *cmdarray;
1099	unsigned int i;
1100
1101	/* need to check if cmd array is allocated or not */
1102	if (priv->cmd_array == NULL) {
1103		lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1104		goto done;
1105	}
1106
1107	cmdarray = priv->cmd_array;
1108
1109	/* Release shared memory buffers */
1110	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
1111		if (cmdarray[i].cmdbuf) {
1112			kfree(cmdarray[i].cmdbuf);
1113			cmdarray[i].cmdbuf = NULL;
1114		}
1115	}
1116
1117	/* Release cmd_ctrl_node */
1118	if (priv->cmd_array) {
1119		kfree(priv->cmd_array);
1120		priv->cmd_array = NULL;
1121	}
1122
1123done:
1124	return 0;
1125}
1126
1127/**
1128 *  lbs_get_free_cmd_node - gets a free command node if available in
1129 *  command free queue
1130 *
1131 *  @priv:	A pointer to &struct lbs_private structure
1132 *
1133 *  returns:	A pointer to &cmd_ctrl_node structure on success
1134 *		or %NULL on error
1135 */
1136static struct cmd_ctrl_node *lbs_get_free_cmd_node(struct lbs_private *priv)
1137{
1138	struct cmd_ctrl_node *tempnode;
1139	unsigned long flags;
1140
1141	if (!priv)
1142		return NULL;
1143
1144	spin_lock_irqsave(&priv->driver_lock, flags);
1145
1146	if (!list_empty(&priv->cmdfreeq)) {
1147		tempnode = list_first_entry(&priv->cmdfreeq,
1148					    struct cmd_ctrl_node, list);
1149		list_del_init(&tempnode->list);
1150	} else {
1151		lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1152		tempnode = NULL;
1153	}
1154
1155	spin_unlock_irqrestore(&priv->driver_lock, flags);
1156
1157	return tempnode;
1158}
1159
1160/**
1161 *  lbs_execute_next_command - execute next command in command
1162 *  pending queue. Will put firmware back to PS mode if applicable.
1163 *
1164 *  @priv:	A pointer to &struct lbs_private structure
1165 *
1166 *  returns:	0 on success or -1 on error
1167 */
1168int lbs_execute_next_command(struct lbs_private *priv)
1169{
1170	struct cmd_ctrl_node *cmdnode = NULL;
1171	struct cmd_header *cmd;
1172	unsigned long flags;
1173	int ret = 0;
1174
1175	/* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1176	 * only caller to us is lbs_thread() and we get even when a
1177	 * data packet is received */
1178	spin_lock_irqsave(&priv->driver_lock, flags);
1179
1180	if (priv->cur_cmd) {
1181		netdev_alert(priv->dev,
1182			     "EXEC_NEXT_CMD: already processing command!\n");
1183		spin_unlock_irqrestore(&priv->driver_lock, flags);
1184		ret = -1;
1185		goto done;
1186	}
1187
1188	if (!list_empty(&priv->cmdpendingq)) {
1189		cmdnode = list_first_entry(&priv->cmdpendingq,
1190					   struct cmd_ctrl_node, list);
1191	}
1192
1193	spin_unlock_irqrestore(&priv->driver_lock, flags);
1194
1195	if (cmdnode) {
1196		cmd = cmdnode->cmdbuf;
1197
1198		if (is_command_allowed_in_ps(le16_to_cpu(cmd->command))) {
1199			if ((priv->psstate == PS_STATE_SLEEP) ||
1200			    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1201				lbs_deb_host(
1202				       "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1203				       le16_to_cpu(cmd->command),
1204				       priv->psstate);
1205				ret = -1;
1206				goto done;
1207			}
1208			lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
1209				     "0x%04x in psstate %d\n",
1210				     le16_to_cpu(cmd->command), priv->psstate);
1211		} else if (priv->psstate != PS_STATE_FULL_POWER) {
1212			/*
1213			 * 1. Non-PS command:
1214			 * Queue it. set needtowakeup to TRUE if current state
1215			 * is SLEEP, otherwise call send EXIT_PS.
1216			 * 2. PS command but not EXIT_PS:
1217			 * Ignore it.
1218			 * 3. PS command EXIT_PS:
1219			 * Set needtowakeup to TRUE if current state is SLEEP,
1220			 * otherwise send this command down to firmware
1221			 * immediately.
1222			 */
1223			if (cmd->command != cpu_to_le16(CMD_802_11_PS_MODE)) {
1224				/*  Prepare to send Exit PS,
1225				 *  this non PS command will be sent later */
1226				if ((priv->psstate == PS_STATE_SLEEP)
1227				    || (priv->psstate == PS_STATE_PRE_SLEEP)
1228				    ) {
1229					/* w/ new scheme, it will not reach here.
1230					   since it is blocked in main_thread. */
1231					priv->needtowakeup = 1;
1232				} else {
1233					lbs_set_ps_mode(priv,
1234							PS_MODE_ACTION_EXIT_PS,
1235							false);
1236				}
1237
1238				ret = 0;
1239				goto done;
1240			} else {
1241				/*
1242				 * PS command. Ignore it if it is not Exit_PS.
1243				 * otherwise send it down immediately.
1244				 */
1245				struct cmd_ds_802_11_ps_mode *psm = (void *)cmd;
1246
1247				lbs_deb_host(
1248				       "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1249				       psm->action);
1250				if (psm->action !=
1251				    cpu_to_le16(PS_MODE_ACTION_EXIT_PS)) {
1252					lbs_deb_host(
1253					       "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1254					lbs_complete_command(priv, cmdnode, 0);
1255
1256					ret = 0;
1257					goto done;
1258				}
1259
1260				if ((priv->psstate == PS_STATE_SLEEP) ||
1261				    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1262					lbs_deb_host(
1263					       "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1264					lbs_complete_command(priv, cmdnode, 0);
1265					priv->needtowakeup = 1;
1266
1267					ret = 0;
1268					goto done;
1269				}
1270
1271				lbs_deb_host(
1272				       "EXEC_NEXT_CMD: sending EXIT_PS\n");
1273			}
1274		}
1275		spin_lock_irqsave(&priv->driver_lock, flags);
1276		list_del_init(&cmdnode->list);
1277		spin_unlock_irqrestore(&priv->driver_lock, flags);
1278		lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1279			    le16_to_cpu(cmd->command));
1280		lbs_submit_command(priv, cmdnode);
1281	} else {
1282		/*
1283		 * check if in power save mode, if yes, put the device back
1284		 * to PS mode
1285		 */
1286		if ((priv->psmode != LBS802_11POWERMODECAM) &&
1287		    (priv->psstate == PS_STATE_FULL_POWER) &&
1288		    (priv->connect_status == LBS_CONNECTED)) {
1289			lbs_deb_host(
1290				"EXEC_NEXT_CMD: cmdpendingq empty, go back to PS_SLEEP");
1291			lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS,
1292					false);
1293		}
1294	}
1295
1296	ret = 0;
1297done:
1298	return ret;
1299}
1300
1301static void lbs_send_confirmsleep(struct lbs_private *priv)
1302{
1303	unsigned long flags;
1304	int ret;
1305
1306	lbs_deb_hex(LBS_DEB_HOST, "sleep confirm", (u8 *) &confirm_sleep,
1307		sizeof(confirm_sleep));
1308
1309	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &confirm_sleep,
1310		sizeof(confirm_sleep));
1311	if (ret) {
1312		netdev_alert(priv->dev, "confirm_sleep failed\n");
1313		return;
1314	}
1315
1316	spin_lock_irqsave(&priv->driver_lock, flags);
1317
1318	/* We don't get a response on the sleep-confirmation */
1319	priv->dnld_sent = DNLD_RES_RECEIVED;
1320
1321	if (priv->is_host_sleep_configured) {
1322		priv->is_host_sleep_activated = 1;
1323		wake_up_interruptible(&priv->host_sleep_q);
1324	}
1325
1326	/* If nothing to do, go back to sleep (?) */
1327	if (!kfifo_len(&priv->event_fifo) && !priv->resp_len[priv->resp_idx])
1328		priv->psstate = PS_STATE_SLEEP;
1329
1330	spin_unlock_irqrestore(&priv->driver_lock, flags);
1331}
1332
1333/**
1334 * lbs_ps_confirm_sleep - checks condition and prepares to
1335 * send sleep confirm command to firmware if ok
1336 *
1337 * @priv:	A pointer to &struct lbs_private structure
1338 *
1339 * returns:	n/a
1340 */
1341void lbs_ps_confirm_sleep(struct lbs_private *priv)
1342{
1343	unsigned long flags =0;
1344	int allowed = 1;
1345
1346	spin_lock_irqsave(&priv->driver_lock, flags);
1347	if (priv->dnld_sent) {
1348		allowed = 0;
1349		lbs_deb_host("dnld_sent was set\n");
1350	}
1351
1352	/* In-progress command? */
1353	if (priv->cur_cmd) {
1354		allowed = 0;
1355		lbs_deb_host("cur_cmd was set\n");
1356	}
1357
1358	/* Pending events or command responses? */
1359	if (kfifo_len(&priv->event_fifo) || priv->resp_len[priv->resp_idx]) {
1360		allowed = 0;
1361		lbs_deb_host("pending events or command responses\n");
1362	}
1363	spin_unlock_irqrestore(&priv->driver_lock, flags);
1364
1365	if (allowed) {
1366		lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1367		lbs_send_confirmsleep(priv);
1368	} else {
1369		lbs_deb_host("sleep confirm has been delayed\n");
1370	}
1371}
1372
1373
1374struct cmd_ctrl_node *__lbs_cmd_async(struct lbs_private *priv,
1375	uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
1376	int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1377	unsigned long callback_arg)
1378{
1379	struct cmd_ctrl_node *cmdnode;
1380
1381	if (priv->surpriseremoved) {
1382		lbs_deb_host("PREP_CMD: card removed\n");
1383		cmdnode = ERR_PTR(-ENOENT);
1384		goto done;
1385	}
1386
1387	/* No commands are allowed in Deep Sleep until we toggle the GPIO
1388	 * to wake up the card and it has signaled that it's ready.
1389	 */
1390	if (priv->is_deep_sleep) {
1391		lbs_deb_cmd("command not allowed in deep sleep\n");
1392		cmdnode = ERR_PTR(-EBUSY);
1393		goto done;
1394	}
1395
1396	cmdnode = lbs_get_free_cmd_node(priv);
1397	if (cmdnode == NULL) {
1398		lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1399
1400		/* Wake up main thread to execute next command */
1401		wake_up(&priv->waitq);
1402		cmdnode = ERR_PTR(-ENOBUFS);
1403		goto done;
1404	}
1405
1406	cmdnode->callback = callback;
1407	cmdnode->callback_arg = callback_arg;
1408
1409	/* Copy the incoming command to the buffer */
1410	memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
1411
1412	/* Set command, clean result, move to buffer */
1413	cmdnode->cmdbuf->command = cpu_to_le16(command);
1414	cmdnode->cmdbuf->size    = cpu_to_le16(in_cmd_size);
1415	cmdnode->cmdbuf->result  = 0;
1416
1417	lbs_deb_host("PREP_CMD: command 0x%04x\n", command);
1418
1419	cmdnode->cmdwaitqwoken = 0;
1420	lbs_queue_cmd(priv, cmdnode);
1421	wake_up(&priv->waitq);
1422
1423 done:
1424	return cmdnode;
1425}
1426
1427void lbs_cmd_async(struct lbs_private *priv, uint16_t command,
1428	struct cmd_header *in_cmd, int in_cmd_size)
1429{
1430	__lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1431		lbs_cmd_async_callback, 0);
1432}
1433
1434int __lbs_cmd(struct lbs_private *priv, uint16_t command,
1435	      struct cmd_header *in_cmd, int in_cmd_size,
1436	      int (*callback)(struct lbs_private *, unsigned long, struct cmd_header *),
1437	      unsigned long callback_arg)
1438{
1439	struct cmd_ctrl_node *cmdnode;
1440	unsigned long flags;
1441	int ret = 0;
1442
1443	cmdnode = __lbs_cmd_async(priv, command, in_cmd, in_cmd_size,
1444				  callback, callback_arg);
1445	if (IS_ERR(cmdnode)) {
1446		ret = PTR_ERR(cmdnode);
1447		goto done;
1448	}
1449
1450	might_sleep();
1451
1452	/*
1453	 * Be careful with signals here. A signal may be received as the system
1454	 * goes into suspend or resume. We do not want this to interrupt the
1455	 * command, so we perform an uninterruptible sleep.
1456	 */
1457	wait_event(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);
1458
1459	spin_lock_irqsave(&priv->driver_lock, flags);
1460	ret = cmdnode->result;
1461	if (ret)
1462		netdev_info(priv->dev, "PREP_CMD: command 0x%04x failed: %d\n",
1463			    command, ret);
1464
1465	__lbs_cleanup_and_insert_cmd(priv, cmdnode);
1466	spin_unlock_irqrestore(&priv->driver_lock, flags);
1467
1468done:
1469	return ret;
1470}
1471EXPORT_SYMBOL_GPL(__lbs_cmd);
~/ Read-only file preview.