cmd.c 53.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
  * This file contains the handling of command.
  * It prepares command and sends it to firmware when it is ready.
  */

#include <net/iw_handler.h>
#include "host.h"
#include "hostcmd.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "join.h"
#include "wext.h"

static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode);
16 17 18 19 20
struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv);
void lbs_set_cmd_ctrl_node(struct lbs_private *priv,
		    struct cmd_ctrl_node *ptempnode,
		    u16 wait_option, void *pdata_buf);

21 22

/**
23
 *  @brief Checks whether a command is allowed in Power Save mode
24 25
 *
 *  @param command the command ID
26
 *  @return 	   1 if allowed, 0 if not allowed
27
 */
28
static u8 is_command_allowed_in_ps(u16 cmd)
29
{
30 31 32 33 34
	switch (cmd) {
	case CMD_802_11_RSSI:
		return 1;
	default:
		break;
35 36 37 38
	}
	return 0;
}

39
static int lbs_cmd_hw_spec(struct lbs_private *priv, struct cmd_ds_command *cmd)
40 41 42
{
	struct cmd_ds_get_hw_spec *hwspec = &cmd->params.hwspec;

43
	lbs_deb_enter(LBS_DEB_CMD);
44

45
	cmd->command = cpu_to_le16(CMD_GET_HW_SPEC);
46
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_get_hw_spec) + S_DS_GEN);
47
	memcpy(hwspec->permanentaddr, priv->current_addr, ETH_ALEN);
48

49
	lbs_deb_leave(LBS_DEB_CMD);
50 51 52
	return 0;
}

53
static int lbs_cmd_802_11_ps_mode(struct lbs_private *priv,
54 55 56 57 58
				   struct cmd_ds_command *cmd,
				   u16 cmd_action)
{
	struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;

59
	lbs_deb_enter(LBS_DEB_CMD);
60

61
	cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
62 63
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
				S_DS_GEN);
64 65
	psm->action = cpu_to_le16(cmd_action);
	psm->multipledtim = 0;
66
	switch (cmd_action) {
67
	case CMD_SUBCMD_ENTER_PS:
68
		lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
69

70
		psm->locallisteninterval = 0;
71
		psm->nullpktinterval = 0;
72
		psm->multipledtim =
73
		    cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
74 75
		break;

76
	case CMD_SUBCMD_EXIT_PS:
77
		lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
78 79
		break;

80
	case CMD_SUBCMD_SLEEP_CONFIRMED:
81
		lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
82 83 84 85 86 87
		break;

	default:
		break;
	}

88
	lbs_deb_leave(LBS_DEB_CMD);
89 90 91
	return 0;
}

92
static int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
93 94 95 96 97
					      struct cmd_ds_command *cmd,
					      u16 cmd_action, void *pdata_buf)
{
	u16 *timeout = pdata_buf;

98 99
	lbs_deb_enter(LBS_DEB_CMD);

100
	cmd->command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
101 102 103 104 105 106 107
	cmd->size =
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_inactivity_timeout)
			     + S_DS_GEN);

	cmd->params.inactivity_timeout.action = cpu_to_le16(cmd_action);

	if (cmd_action)
108
		cmd->params.inactivity_timeout.timeout = cpu_to_le16(*timeout);
109 110 111
	else
		cmd->params.inactivity_timeout.timeout = 0;

112
	lbs_deb_leave(LBS_DEB_CMD);
113 114 115
	return 0;
}

116
static int lbs_cmd_802_11_sleep_params(struct lbs_private *priv,
117 118 119 120 121
					struct cmd_ds_command *cmd,
					u16 cmd_action)
{
	struct cmd_ds_802_11_sleep_params *sp = &cmd->params.sleep_params;

122
	lbs_deb_enter(LBS_DEB_CMD);
123

124 125
	cmd->size = cpu_to_le16((sizeof(struct cmd_ds_802_11_sleep_params)) +
				S_DS_GEN);
126
	cmd->command = cpu_to_le16(CMD_802_11_SLEEP_PARAMS);
127

128
	if (cmd_action == CMD_ACT_GET) {
129
		memset(&priv->sp, 0, sizeof(struct sleep_params));
130 131
		memset(sp, 0, sizeof(struct cmd_ds_802_11_sleep_params));
		sp->action = cpu_to_le16(cmd_action);
132
	} else if (cmd_action == CMD_ACT_SET) {
133
		sp->action = cpu_to_le16(cmd_action);
134 135 136 137 138 139
		sp->error = cpu_to_le16(priv->sp.sp_error);
		sp->offset = cpu_to_le16(priv->sp.sp_offset);
		sp->stabletime = cpu_to_le16(priv->sp.sp_stabletime);
		sp->calcontrol = (u8) priv->sp.sp_calcontrol;
		sp->externalsleepclk = (u8) priv->sp.sp_extsleepclk;
		sp->reserved = cpu_to_le16(priv->sp.sp_reserved);
140 141
	}

142
	lbs_deb_leave(LBS_DEB_CMD);
143 144 145
	return 0;
}

146
static int lbs_cmd_802_11_set_wep(struct lbs_private *priv,
147 148 149 150 151 152 153 154
                                   struct cmd_ds_command *cmd,
                                   u32 cmd_act,
                                   void * pdata_buf)
{
	struct cmd_ds_802_11_set_wep *wep = &cmd->params.wep;
	int ret = 0;
	struct assoc_request * assoc_req = pdata_buf;

155
	lbs_deb_enter(LBS_DEB_CMD);
156

157
	cmd->command = cpu_to_le16(CMD_802_11_SET_WEP);
158
	cmd->size = cpu_to_le16(sizeof(*wep) + S_DS_GEN);
159

160
	if (cmd_act == CMD_ACT_ADD) {
161 162 163
		int i;

		if (!assoc_req) {
164
			lbs_deb_cmd("Invalid association request!");
165 166 167 168
			ret = -1;
			goto done;
		}

169
		wep->action = cpu_to_le16(CMD_ACT_ADD);
170 171

		/* default tx key index */
172
		wep->keyindex = cpu_to_le16((u16)(assoc_req->wep_tx_keyidx &
173
						  (u32)CMD_WEP_KEY_INDEX_MASK));
174 175 176

		/* Copy key types and material to host command structure */
		for (i = 0; i < 4; i++) {
177
			struct enc_key * pkey = &assoc_req->wep_keys[i];
178 179 180

			switch (pkey->len) {
			case KEY_LEN_WEP_40:
H
Holger Schurig 已提交
181
				wep->keytype[i] = CMD_TYPE_WEP_40_BIT;
182 183
				memmove(&wep->keymaterial[i], pkey->key,
				        pkey->len);
184
				lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
185 186
				break;
			case KEY_LEN_WEP_104:
H
Holger Schurig 已提交
187
				wep->keytype[i] = CMD_TYPE_WEP_104_BIT;
188 189
				memmove(&wep->keymaterial[i], pkey->key,
				        pkey->len);
190
				lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
191 192 193 194
				break;
			case 0:
				break;
			default:
195
				lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
196 197 198 199 200 201
				       i, pkey->len);
				ret = -1;
				goto done;
				break;
			}
		}
202
	} else if (cmd_act == CMD_ACT_REMOVE) {
203
		/* ACT_REMOVE clears _all_ WEP keys */
204
		wep->action = cpu_to_le16(CMD_ACT_REMOVE);
205 206

		/* default tx key index */
207
		wep->keyindex = cpu_to_le16((u16)(priv->wep_tx_keyidx &
208
						  (u32)CMD_WEP_KEY_INDEX_MASK));
209
		lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
210 211 212 213 214
	}

	ret = 0;

done:
215
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
216 217 218
	return ret;
}

219
static int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv,
220
				      struct cmd_ds_command *cmd,
221 222
				      u16 cmd_action,
				      void * pdata_buf)
223 224
{
	struct cmd_ds_802_11_enable_rsn *penableRSN = &cmd->params.enbrsn;
225
	u32 * enable = pdata_buf;
226 227

	lbs_deb_enter(LBS_DEB_CMD);
228

229
	cmd->command = cpu_to_le16(CMD_802_11_ENABLE_RSN);
230
	cmd->size = cpu_to_le16(sizeof(*penableRSN) + S_DS_GEN);
231
	penableRSN->action = cpu_to_le16(cmd_action);
232

233
	if (cmd_action == CMD_ACT_SET) {
234
		if (*enable)
235
			penableRSN->enable = cpu_to_le16(CMD_ENABLE_RSN);
236
		else
237
			penableRSN->enable = cpu_to_le16(CMD_DISABLE_RSN);
238
		lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
239 240
	}

241
	lbs_deb_leave(LBS_DEB_CMD);
242 243 244 245
	return 0;
}


246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static ssize_t lbs_tlv_size(const u8 *tlv, u16 size)
{
	ssize_t pos = 0;
	struct mrvlietypesheader *tlv_h;
	while (pos < size) {
		u16 length;
		tlv_h = (struct mrvlietypesheader *) tlv;
		if (tlv_h->len == 0)
			return pos;
		length = le16_to_cpu(tlv_h->len) +
			sizeof(struct mrvlietypesheader);
		pos += length;
		tlv += length;
	}
	return pos;
}


static void lbs_cmd_802_11_subscribe_event(struct lbs_private *priv,
	struct cmd_ds_command *cmd, u16 cmd_action,
	void *pdata_buf)
{
	struct cmd_ds_802_11_subscribe_event *events =
		(struct cmd_ds_802_11_subscribe_event *) pdata_buf;

	/* pdata_buf points to a struct cmd_ds_802_11_subscribe_event and room
	 * for various Marvell TLVs */

	lbs_deb_enter(LBS_DEB_CMD);

	cmd->size = cpu_to_le16(sizeof(*events)
			- sizeof(events->tlv)
			+ S_DS_GEN);
	cmd->params.subscribe_event.action = cpu_to_le16(cmd_action);
	if (cmd_action == CMD_ACT_GET) {
		cmd->params.subscribe_event.events = 0;
	} else {
		ssize_t sz = lbs_tlv_size(events->tlv, sizeof(events->tlv));
		cmd->size = cpu_to_le16(le16_to_cpu(cmd->size) + sz);
		cmd->params.subscribe_event.events = events->events;
		memcpy(cmd->params.subscribe_event.tlv, events->tlv, sz);
	}

	lbs_deb_leave(LBS_DEB_CMD);
}

292
static void set_one_wpa_key(struct MrvlIEtype_keyParamSet * pkeyparamset,
293
                            struct enc_key * pkey)
294
{
295 296
	lbs_deb_enter(LBS_DEB_CMD);

297
	if (pkey->flags & KEY_INFO_WPA_ENABLED) {
298
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
299 300 301
	}
	if (pkey->flags & KEY_INFO_WPA_UNICAST) {
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
302 303
	}
	if (pkey->flags & KEY_INFO_WPA_MCAST) {
304 305 306 307
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
	}

	pkeyparamset->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
308
	pkeyparamset->keytypeid = cpu_to_le16(pkey->type);
309 310 311 312 313 314
	pkeyparamset->keylen = cpu_to_le16(pkey->len);
	memcpy(pkeyparamset->key, pkey->key, pkey->len);
	pkeyparamset->length = cpu_to_le16(  sizeof(pkeyparamset->keytypeid)
	                                        + sizeof(pkeyparamset->keyinfo)
	                                        + sizeof(pkeyparamset->keylen)
	                                        + sizeof(pkeyparamset->key));
315
	lbs_deb_leave(LBS_DEB_CMD);
316 317
}

318
static int lbs_cmd_802_11_key_material(struct lbs_private *priv,
319 320 321 322 323 324
					struct cmd_ds_command *cmd,
					u16 cmd_action,
					u32 cmd_oid, void *pdata_buf)
{
	struct cmd_ds_802_11_key_material *pkeymaterial =
	    &cmd->params.keymaterial;
325
	struct assoc_request * assoc_req = pdata_buf;
326 327 328
	int ret = 0;
	int index = 0;

329
	lbs_deb_enter(LBS_DEB_CMD);
330

331
	cmd->command = cpu_to_le16(CMD_802_11_KEY_MATERIAL);
332 333
	pkeymaterial->action = cpu_to_le16(cmd_action);

334
	if (cmd_action == CMD_ACT_GET) {
335
		cmd->size = cpu_to_le16(S_DS_GEN + sizeof (pkeymaterial->action));
336 337 338 339 340 341
		ret = 0;
		goto done;
	}

	memset(&pkeymaterial->keyParamSet, 0, sizeof(pkeymaterial->keyParamSet));

342
	if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
343
		set_one_wpa_key(&pkeymaterial->keyParamSet[index],
344
		                &assoc_req->wpa_unicast_key);
345 346 347
		index++;
	}

348
	if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
349
		set_one_wpa_key(&pkeymaterial->keyParamSet[index],
350
		                &assoc_req->wpa_mcast_key);
351 352 353 354
		index++;
	}

	cmd->size = cpu_to_le16(  S_DS_GEN
355 356
	                        + sizeof (pkeymaterial->action)
	                        + (index * sizeof(struct MrvlIEtype_keyParamSet)));
357 358 359 360

	ret = 0;

done:
361
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
362 363 364
	return ret;
}

365
static int lbs_cmd_802_11_reset(struct lbs_private *priv,
366 367 368 369
				 struct cmd_ds_command *cmd, int cmd_action)
{
	struct cmd_ds_802_11_reset *reset = &cmd->params.reset;

370 371
	lbs_deb_enter(LBS_DEB_CMD);

372
	cmd->command = cpu_to_le16(CMD_802_11_RESET);
373 374 375
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
	reset->action = cpu_to_le16(cmd_action);

376
	lbs_deb_leave(LBS_DEB_CMD);
377 378 379
	return 0;
}

380
static int lbs_cmd_802_11_get_log(struct lbs_private *priv,
381 382
				   struct cmd_ds_command *cmd)
{
383
	lbs_deb_enter(LBS_DEB_CMD);
384
	cmd->command = cpu_to_le16(CMD_802_11_GET_LOG);
385 386 387
	cmd->size =
		cpu_to_le16(sizeof(struct cmd_ds_802_11_get_log) + S_DS_GEN);

388
	lbs_deb_leave(LBS_DEB_CMD);
389 390 391
	return 0;
}

392
static int lbs_cmd_802_11_get_stat(struct lbs_private *priv,
393 394
				    struct cmd_ds_command *cmd)
{
395
	lbs_deb_enter(LBS_DEB_CMD);
396
	cmd->command = cpu_to_le16(CMD_802_11_GET_STAT);
397
	cmd->size =
398
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_get_stat) + S_DS_GEN);
399

400
	lbs_deb_leave(LBS_DEB_CMD);
401 402 403
	return 0;
}

404
static int lbs_cmd_802_11_snmp_mib(struct lbs_private *priv,
405 406 407 408 409 410 411
				    struct cmd_ds_command *cmd,
				    int cmd_action,
				    int cmd_oid, void *pdata_buf)
{
	struct cmd_ds_802_11_snmp_mib *pSNMPMIB = &cmd->params.smib;
	u8 ucTemp;

412
	lbs_deb_enter(LBS_DEB_CMD);
413

414
	lbs_deb_cmd("SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
415

416
	cmd->command = cpu_to_le16(CMD_802_11_SNMP_MIB);
417
	cmd->size = cpu_to_le16(sizeof(*pSNMPMIB) + S_DS_GEN);
418 419 420 421

	switch (cmd_oid) {
	case OID_802_11_INFRASTRUCTURE_MODE:
	{
422
		u8 mode = (u8) (size_t) pdata_buf;
423 424
		pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
		pSNMPMIB->oid = cpu_to_le16((u16) DESIRED_BSSTYPE_I);
H
Holger Schurig 已提交
425
		pSNMPMIB->bufsize = cpu_to_le16(sizeof(u8));
426
		if (mode == IW_MODE_ADHOC) {
427
			ucTemp = SNMP_MIB_VALUE_ADHOC;
428 429 430 431
		} else {
			/* Infra and Auto modes */
			ucTemp = SNMP_MIB_VALUE_INFRA;
		}
432 433 434 435 436 437 438 439 440 441

		memmove(pSNMPMIB->value, &ucTemp, sizeof(u8));

		break;
	}

	case OID_802_11D_ENABLE:
		{
			u32 ulTemp;

442
			pSNMPMIB->oid = cpu_to_le16((u16) DOT11D_I);
443

444
			if (cmd_action == CMD_ACT_SET) {
H
Holger Schurig 已提交
445 446
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
447
				ulTemp = *(u32 *)pdata_buf;
448
				*((__le16 *)(pSNMPMIB->value)) =
449 450 451 452 453 454 455 456 457
				    cpu_to_le16((u16) ulTemp);
			}
			break;
		}

	case OID_802_11_FRAGMENTATION_THRESHOLD:
		{
			u32 ulTemp;

458
			pSNMPMIB->oid = cpu_to_le16((u16) FRAGTHRESH_I);
459

460 461 462 463
			if (cmd_action == CMD_ACT_GET) {
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_GET);
			} else if (cmd_action == CMD_ACT_SET) {
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
464
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
465
				ulTemp = *((u32 *) pdata_buf);
466
				*((__le16 *)(pSNMPMIB->value)) =
467 468 469 470 471 472 473 474 475 476 477
				    cpu_to_le16((u16) ulTemp);

			}

			break;
		}

	case OID_802_11_RTS_THRESHOLD:
		{

			u32 ulTemp;
H
Holger Schurig 已提交
478
			pSNMPMIB->oid = cpu_to_le16(RTSTHRESH_I);
479

480 481 482 483
			if (cmd_action == CMD_ACT_GET) {
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_GET);
			} else if (cmd_action == CMD_ACT_SET) {
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
484 485 486
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
				ulTemp = *((u32 *)pdata_buf);
				*(__le16 *)(pSNMPMIB->value) =
487 488 489 490 491 492
				    cpu_to_le16((u16) ulTemp);

			}
			break;
		}
	case OID_802_11_TX_RETRYCOUNT:
493
		pSNMPMIB->oid = cpu_to_le16((u16) SHORT_RETRYLIM_I);
494

495 496 497 498
		if (cmd_action == CMD_ACT_GET) {
			pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_GET);
		} else if (cmd_action == CMD_ACT_SET) {
			pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
499
			pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
500
			*((__le16 *)(pSNMPMIB->value)) =
501
			    cpu_to_le16((u16) priv->txretrycount);
502 503 504 505 506 507 508
		}

		break;
	default:
		break;
	}

509
	lbs_deb_cmd(
510
	       "SNMP_CMD: command=0x%x, size=0x%x, seqnum=0x%x, result=0x%x\n",
511 512
	       le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
	       le16_to_cpu(cmd->seqnum), le16_to_cpu(cmd->result));
513

514
	lbs_deb_cmd(
515
	       "SNMP_CMD: action 0x%x, oid 0x%x, oidsize 0x%x, value 0x%x\n",
516 517 518
	       le16_to_cpu(pSNMPMIB->querytype), le16_to_cpu(pSNMPMIB->oid),
	       le16_to_cpu(pSNMPMIB->bufsize),
	       le16_to_cpu(*(__le16 *) pSNMPMIB->value));
519

520
	lbs_deb_leave(LBS_DEB_CMD);
521 522 523
	return 0;
}

524
static int lbs_cmd_802_11_radio_control(struct lbs_private *priv,
525 526 527
					 struct cmd_ds_command *cmd,
					 int cmd_action)
{
528
	struct cmd_ds_802_11_radio_control *pradiocontrol = &cmd->params.radio;
529

530
	lbs_deb_enter(LBS_DEB_CMD);
531 532 533 534

	cmd->size =
	    cpu_to_le16((sizeof(struct cmd_ds_802_11_radio_control)) +
			     S_DS_GEN);
535
	cmd->command = cpu_to_le16(CMD_802_11_RADIO_CONTROL);
536 537 538

	pradiocontrol->action = cpu_to_le16(cmd_action);

539
	switch (priv->preamble) {
540
	case CMD_TYPE_SHORT_PREAMBLE:
541 542 543
		pradiocontrol->control = cpu_to_le16(SET_SHORT_PREAMBLE);
		break;

544
	case CMD_TYPE_LONG_PREAMBLE:
545 546 547
		pradiocontrol->control = cpu_to_le16(SET_LONG_PREAMBLE);
		break;

548
	case CMD_TYPE_AUTO_PREAMBLE:
549 550 551 552 553
	default:
		pradiocontrol->control = cpu_to_le16(SET_AUTO_PREAMBLE);
		break;
	}

554
	if (priv->radioon)
555 556 557 558
		pradiocontrol->control |= cpu_to_le16(TURN_ON_RF);
	else
		pradiocontrol->control &= cpu_to_le16(~TURN_ON_RF);

559
	lbs_deb_leave(LBS_DEB_CMD);
560 561 562
	return 0;
}

563
static int lbs_cmd_802_11_rf_tx_power(struct lbs_private *priv,
564 565 566 567 568 569
				       struct cmd_ds_command *cmd,
				       u16 cmd_action, void *pdata_buf)
{

	struct cmd_ds_802_11_rf_tx_power *prtp = &cmd->params.txp;

570
	lbs_deb_enter(LBS_DEB_CMD);
571 572

	cmd->size =
573
	    cpu_to_le16((sizeof(struct cmd_ds_802_11_rf_tx_power)) + S_DS_GEN);
574
	cmd->command = cpu_to_le16(CMD_802_11_RF_TX_POWER);
575
	prtp->action = cpu_to_le16(cmd_action);
576

577 578 579
	lbs_deb_cmd("RF_TX_POWER_CMD: size:%d cmd:0x%x Act:%d\n",
		    le16_to_cpu(cmd->size), le16_to_cpu(cmd->command),
		    le16_to_cpu(prtp->action));
580 581

	switch (cmd_action) {
582 583
	case CMD_ACT_TX_POWER_OPT_GET:
		prtp->action = cpu_to_le16(CMD_ACT_GET);
584 585 586
		prtp->currentlevel = 0;
		break;

587 588 589
	case CMD_ACT_TX_POWER_OPT_SET_HIGH:
		prtp->action = cpu_to_le16(CMD_ACT_SET);
		prtp->currentlevel = cpu_to_le16(CMD_ACT_TX_POWER_INDEX_HIGH);
590 591
		break;

592 593 594
	case CMD_ACT_TX_POWER_OPT_SET_MID:
		prtp->action = cpu_to_le16(CMD_ACT_SET);
		prtp->currentlevel = cpu_to_le16(CMD_ACT_TX_POWER_INDEX_MID);
595 596
		break;

597 598
	case CMD_ACT_TX_POWER_OPT_SET_LOW:
		prtp->action = cpu_to_le16(CMD_ACT_SET);
599 600 601
		prtp->currentlevel = cpu_to_le16(*((u16 *) pdata_buf));
		break;
	}
602 603

	lbs_deb_leave(LBS_DEB_CMD);
604 605 606
	return 0;
}

607
static int lbs_cmd_802_11_monitor_mode(struct lbs_private *priv,
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
				      struct cmd_ds_command *cmd,
				      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) +
			     S_DS_GEN);

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

	return 0;
}

627
static int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
628 629 630 631 632 633
					      struct cmd_ds_command *cmd,
					      u16 cmd_action)
{
	struct cmd_ds_802_11_rate_adapt_rateset
	*rateadapt = &cmd->params.rateset;

634
	lbs_deb_enter(LBS_DEB_CMD);
635 636 637
	cmd->size =
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_rate_adapt_rateset)
			     + S_DS_GEN);
638
	cmd->command = cpu_to_le16(CMD_802_11_RATE_ADAPT_RATESET);
639

640
	rateadapt->action = cpu_to_le16(cmd_action);
641 642
	rateadapt->enablehwauto = cpu_to_le16(priv->enablehwauto);
	rateadapt->bitmap = cpu_to_le16(priv->ratebitmap);
643

644
	lbs_deb_leave(LBS_DEB_CMD);
645 646 647
	return 0;
}

648
static int lbs_cmd_802_11_data_rate(struct lbs_private *priv,
649 650 651 652 653
				     struct cmd_ds_command *cmd,
				     u16 cmd_action)
{
	struct cmd_ds_802_11_data_rate *pdatarate = &cmd->params.drate;

654
	lbs_deb_enter(LBS_DEB_CMD);
655

656
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_data_rate) +
657
			     S_DS_GEN);
658
	cmd->command = cpu_to_le16(CMD_802_11_DATA_RATE);
659 660 661
	memset(pdatarate, 0, sizeof(struct cmd_ds_802_11_data_rate));
	pdatarate->action = cpu_to_le16(cmd_action);

662
	if (cmd_action == CMD_ACT_SET_TX_FIX_RATE) {
663
		pdatarate->rates[0] = lbs_data_rate_to_fw_index(priv->cur_rate);
664
		lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n",
665
		       priv->cur_rate);
666
	} else if (cmd_action == CMD_ACT_SET_TX_AUTO) {
667
		lbs_deb_cmd("DATA_RATE: setting auto\n");
668 669
	}

670
	lbs_deb_leave(LBS_DEB_CMD);
671 672 673
	return 0;
}

674
static int lbs_cmd_mac_multicast_adr(struct lbs_private *priv,
675 676 677 678 679
				      struct cmd_ds_command *cmd,
				      u16 cmd_action)
{
	struct cmd_ds_mac_multicast_adr *pMCastAdr = &cmd->params.madr;

680
	lbs_deb_enter(LBS_DEB_CMD);
681
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_multicast_adr) +
682
			     S_DS_GEN);
683
	cmd->command = cpu_to_le16(CMD_MAC_MULTICAST_ADR);
684

685
	lbs_deb_cmd("MULTICAST_ADR: setting %d addresses\n", pMCastAdr->nr_of_adrs);
686 687
	pMCastAdr->action = cpu_to_le16(cmd_action);
	pMCastAdr->nr_of_adrs =
688 689 690
	    cpu_to_le16((u16) priv->nr_of_multicastmacaddr);
	memcpy(pMCastAdr->maclist, priv->multicastlist,
	       priv->nr_of_multicastmacaddr * ETH_ALEN);
691

692
	lbs_deb_leave(LBS_DEB_CMD);
693 694 695
	return 0;
}

696
static int lbs_cmd_802_11_rf_channel(struct lbs_private *priv,
697 698 699 700 701
				      struct cmd_ds_command *cmd,
				      int option, void *pdata_buf)
{
	struct cmd_ds_802_11_rf_channel *rfchan = &cmd->params.rfchannel;

702
	lbs_deb_enter(LBS_DEB_CMD);
703
	cmd->command = cpu_to_le16(CMD_802_11_RF_CHANNEL);
704 705
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_channel) +
				S_DS_GEN);
706

707
	if (option == CMD_OPT_802_11_RF_CHANNEL_SET) {
708 709 710 711 712
		rfchan->currentchannel = cpu_to_le16(*((u16 *) pdata_buf));
	}

	rfchan->action = cpu_to_le16(option);

713
	lbs_deb_leave(LBS_DEB_CMD);
714 715 716
	return 0;
}

717
static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
718 719 720
				struct cmd_ds_command *cmd)
{

721
	lbs_deb_enter(LBS_DEB_CMD);
722
	cmd->command = cpu_to_le16(CMD_802_11_RSSI);
723
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
724
	cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
725 726

	/* reset Beacon SNR/NF/RSSI values */
727 728 729 730 731 732
	priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
	priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
	priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
	priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
	priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
	priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
733

734
	lbs_deb_leave(LBS_DEB_CMD);
735 736 737
	return 0;
}

738
static int lbs_cmd_reg_access(struct lbs_private *priv,
739 740 741
			       struct cmd_ds_command *cmdptr,
			       u8 cmd_action, void *pdata_buf)
{
742
	struct lbs_offset_value *offval;
743

744
	lbs_deb_enter(LBS_DEB_CMD);
745

746
	offval = (struct lbs_offset_value *)pdata_buf;
747

H
Holger Schurig 已提交
748
	switch (le16_to_cpu(cmdptr->command)) {
749
	case CMD_MAC_REG_ACCESS:
750 751 752 753
		{
			struct cmd_ds_mac_reg_access *macreg;

			cmdptr->size =
754 755
			    cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
					+ S_DS_GEN);
756 757 758 759 760 761 762 763 764 765 766
			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;
		}

767
	case CMD_BBP_REG_ACCESS:
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
		{
			struct cmd_ds_bbp_reg_access *bbpreg;

			cmdptr->size =
			    cpu_to_le16(sizeof
					     (struct cmd_ds_bbp_reg_access)
					     + S_DS_GEN);
			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;
		}

786
	case CMD_RF_REG_ACCESS:
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
		{
			struct cmd_ds_rf_reg_access *rfreg;

			cmdptr->size =
			    cpu_to_le16(sizeof
					     (struct cmd_ds_rf_reg_access) +
					     S_DS_GEN);
			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;
	}

809
	lbs_deb_leave(LBS_DEB_CMD);
810 811 812
	return 0;
}

813
static int lbs_cmd_802_11_mac_address(struct lbs_private *priv,
814 815 816 817
				       struct cmd_ds_command *cmd,
				       u16 cmd_action)
{

818
	lbs_deb_enter(LBS_DEB_CMD);
819
	cmd->command = cpu_to_le16(CMD_802_11_MAC_ADDRESS);
820
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_mac_address) +
821 822 823 824 825
			     S_DS_GEN);
	cmd->result = 0;

	cmd->params.macadd.action = cpu_to_le16(cmd_action);

826
	if (cmd_action == CMD_ACT_SET) {
827
		memcpy(cmd->params.macadd.macadd,
828 829
		       priv->current_addr, ETH_ALEN);
		lbs_deb_hex(LBS_DEB_CMD, "SET_CMD: MAC addr", priv->current_addr, 6);
830 831
	}

832
	lbs_deb_leave(LBS_DEB_CMD);
833 834 835
	return 0;
}

836
static int lbs_cmd_802_11_eeprom_access(struct lbs_private *priv,
837 838 839
					 struct cmd_ds_command *cmd,
					 int cmd_action, void *pdata_buf)
{
840
	struct lbs_ioctl_regrdwr *ea = pdata_buf;
841

842
	lbs_deb_enter(LBS_DEB_CMD);
843

844
	cmd->command = cpu_to_le16(CMD_802_11_EEPROM_ACCESS);
845 846
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) +
				S_DS_GEN);
847 848 849 850 851 852 853
	cmd->result = 0;

	cmd->params.rdeeprom.action = cpu_to_le16(ea->action);
	cmd->params.rdeeprom.offset = cpu_to_le16(ea->offset);
	cmd->params.rdeeprom.bytecount = cpu_to_le16(ea->NOB);
	cmd->params.rdeeprom.value = 0;

854
	lbs_deb_leave(LBS_DEB_CMD);
855 856 857
	return 0;
}

858
static int lbs_cmd_bt_access(struct lbs_private *priv,
859 860 861 862
			       struct cmd_ds_command *cmd,
			       u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
863
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
864

865
	cmd->command = cpu_to_le16(CMD_BT_ACCESS);
866
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
867 868 869 870
	cmd->result = 0;
	bt_access->action = cpu_to_le16(cmd_action);

	switch (cmd_action) {
871
	case CMD_ACT_BT_ACCESS_ADD:
872
		memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
873
		lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr", bt_access->addr1, 6);
874
		break;
875
	case CMD_ACT_BT_ACCESS_DEL:
876
		memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
877
		lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr", bt_access->addr1, 6);
878
		break;
879
	case CMD_ACT_BT_ACCESS_LIST:
880 881
		bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
		break;
882
	case CMD_ACT_BT_ACCESS_RESET:
883
		break;
884
	case CMD_ACT_BT_ACCESS_SET_INVERT:
885 886
		bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
		break;
887
	case CMD_ACT_BT_ACCESS_GET_INVERT:
888
		break;
889 890 891
	default:
		break;
	}
892
	lbs_deb_leave(LBS_DEB_CMD);
893 894 895
	return 0;
}

896
static int lbs_cmd_fwt_access(struct lbs_private *priv,
897 898 899 900
			       struct cmd_ds_command *cmd,
			       u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
901
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
902

903
	cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
904
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
905 906 907 908 909 910 911 912 913
	cmd->result = 0;

	if (pdata_buf)
		memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
	else
		memset(fwt_access, 0, sizeof(*fwt_access));

	fwt_access->action = cpu_to_le16(cmd_action);

914
	lbs_deb_leave(LBS_DEB_CMD);
915 916 917
	return 0;
}

918
static int lbs_cmd_mesh_access(struct lbs_private *priv,
919 920 921 922
				struct cmd_ds_command *cmd,
				u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_mesh_access *mesh_access = &cmd->params.mesh;
923
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
924

925
	cmd->command = cpu_to_le16(CMD_MESH_ACCESS);
926
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mesh_access) + S_DS_GEN);
927 928 929 930 931 932 933 934 935
	cmd->result = 0;

	if (pdata_buf)
		memcpy(mesh_access, pdata_buf, sizeof(*mesh_access));
	else
		memset(mesh_access, 0, sizeof(*mesh_access));

	mesh_access->action = cpu_to_le16(cmd_action);

936
	lbs_deb_leave(LBS_DEB_CMD);
937 938 939
	return 0;
}

940 941 942 943 944 945 946 947 948 949 950 951 952 953
static int lbs_cmd_bcn_ctrl(struct lbs_private * priv,
				struct cmd_ds_command *cmd,
				u16 cmd_action)
{
	struct cmd_ds_802_11_beacon_control
		*bcn_ctrl = &cmd->params.bcn_ctrl;

	lbs_deb_enter(LBS_DEB_CMD);
	cmd->size =
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
			     + S_DS_GEN);
	cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);

	bcn_ctrl->action = cpu_to_le16(cmd_action);
954 955
	bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
	bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
956 957 958 959 960

	lbs_deb_leave(LBS_DEB_CMD);
	return 0;
}

961
/*
962
 * Note: NEVER use lbs_queue_cmd() with addtail==0 other than for
963 964
 * the command timer, because it does not account for queued commands.
 */
965
void lbs_queue_cmd(struct lbs_private *priv,
966 967
	struct cmd_ctrl_node *cmdnode,
	u8 addtail)
968 969 970 971
{
	unsigned long flags;
	struct cmd_ds_command *cmdptr;

972
	lbs_deb_enter(LBS_DEB_HOST);
973 974

	if (!cmdnode) {
975
		lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
976 977 978 979 980
		goto done;
	}

	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
	if (!cmdptr) {
981
		lbs_deb_host("QUEUE_CMD: cmdptr is NULL\n");
982 983 984 985
		goto done;
	}

	/* Exit_PS command needs to be queued in the header always. */
H
Holger Schurig 已提交
986
	if (le16_to_cpu(cmdptr->command) == CMD_802_11_PS_MODE) {
987
		struct cmd_ds_802_11_ps_mode *psm = &cmdptr->params.psmode;
988
		if (psm->action == cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
989
			if (priv->psstate != PS_STATE_FULL_POWER)
990 991 992 993
				addtail = 0;
		}
	}

994
	spin_lock_irqsave(&priv->driver_lock, flags);
995

996
	if (addtail)
997
		list_add_tail(&cmdnode->list, &priv->cmdpendingq);
998
	else
999
		list_add(&cmdnode->list, &priv->cmdpendingq);
1000

1001
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1002

1003
	lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1004
	       le16_to_cpu(((struct cmd_ds_gen*)cmdnode->bufvirtualaddr)->command));
1005 1006

done:
1007
	lbs_deb_leave(LBS_DEB_HOST);
1008 1009 1010 1011
}

/*
 * TODO: Fix the issue when DownloadcommandToStation is being called the
1012
 * second time when the command times out. All the cmdptr->xxx are in little
1013 1014 1015 1016
 * endian and therefore all the comparissions will fail.
 * For now - we are not performing the endian conversion the second time - but
 * for PS and DEEP_SLEEP we need to worry
 */
1017
static int DownloadcommandToStation(struct lbs_private *priv,
1018 1019 1020 1021
				    struct cmd_ctrl_node *cmdnode)
{
	unsigned long flags;
	struct cmd_ds_command *cmdptr;
1022
	int ret = -1;
1023 1024 1025
	u16 cmdsize;
	u16 command;

1026
	lbs_deb_enter(LBS_DEB_HOST);
1027

1028 1029
	if (!priv || !cmdnode) {
		lbs_deb_host("DNLD_CMD: priv or cmdmode is NULL\n");
1030 1031 1032 1033 1034
		goto done;
	}

	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;

1035
	spin_lock_irqsave(&priv->driver_lock, flags);
1036
	if (!cmdptr || !cmdptr->size) {
1037
		lbs_deb_host("DNLD_CMD: cmdptr is NULL or zero\n");
1038
		__lbs_cleanup_and_insert_cmd(priv, cmdnode);
1039
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1040 1041 1042
		goto done;
	}

1043 1044 1045
	priv->cur_cmd = cmdnode;
	priv->cur_cmd_retcode = 0;
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1046

H
Holger Schurig 已提交
1047 1048
	cmdsize = le16_to_cpu(cmdptr->size);
	command = le16_to_cpu(cmdptr->command);
1049

1050
	lbs_deb_host("DNLD_CMD: command 0x%04x, size %d, jiffies %lu\n",
H
Holger Schurig 已提交
1051
		    command, cmdsize, jiffies);
1052 1053
	lbs_deb_hex(LBS_DEB_HOST, "DNLD_CMD", cmdnode->bufvirtualaddr, cmdsize);

1054 1055
	cmdnode->cmdwaitqwoken = 0;

1056
	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmdptr, cmdsize);
1057 1058

	if (ret != 0) {
1059
		lbs_deb_host("DNLD_CMD: hw_host_to_card failed\n");
1060 1061 1062 1063 1064
		spin_lock_irqsave(&priv->driver_lock, flags);
		priv->cur_cmd_retcode = ret;
		__lbs_cleanup_and_insert_cmd(priv, priv->cur_cmd);
		priv->cur_cmd = NULL;
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1065 1066 1067
		goto done;
	}

1068
	lbs_deb_cmd("DNLD_CMD: sent command 0x%04x, jiffies %lu\n", command, jiffies);
1069 1070

	/* Setup the timer after transmit command */
1071 1072
	if (command == CMD_802_11_SCAN || command == CMD_802_11_AUTHENTICATE
	    || command == CMD_802_11_ASSOCIATE)
1073
		mod_timer(&priv->command_timer, jiffies + (10*HZ));
1074
	else
1075
		mod_timer(&priv->command_timer, jiffies + (5*HZ));
1076 1077 1078

	ret = 0;

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

1084
static int lbs_cmd_mac_control(struct lbs_private *priv,
1085 1086 1087 1088
				struct cmd_ds_command *cmd)
{
	struct cmd_ds_mac_control *mac = &cmd->params.macctrl;

1089
	lbs_deb_enter(LBS_DEB_CMD);
1090

1091
	cmd->command = cpu_to_le16(CMD_MAC_CONTROL);
1092
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_control) + S_DS_GEN);
1093
	mac->action = cpu_to_le16(priv->currentpacketfilter);
1094

1095
	lbs_deb_cmd("MAC_CONTROL: action 0x%x, size %d\n",
1096
		    le16_to_cpu(mac->action), le16_to_cpu(cmd->size));
1097

1098
	lbs_deb_leave(LBS_DEB_CMD);
1099 1100 1101 1102 1103
	return 0;
}

/**
 *  This function inserts command node to cmdfreeq
1104
 *  after cleans it. Requires priv->driver_lock held.
1105
 */
1106 1107
void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
	struct cmd_ctrl_node *ptempcmd)
1108 1109 1110
{

	if (!ptempcmd)
1111
		return;
1112 1113

	cleanup_cmdnode(ptempcmd);
1114
	list_add_tail(&ptempcmd->list, &priv->cmdfreeq);
1115 1116
}

1117 1118
static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
	struct cmd_ctrl_node *ptempcmd)
1119 1120 1121
{
	unsigned long flags;

1122
	spin_lock_irqsave(&priv->driver_lock, flags);
1123
	__lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1124
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1125 1126
}

1127
int lbs_set_radio_control(struct lbs_private *priv)
1128 1129 1130
{
	int ret = 0;

1131
	lbs_deb_enter(LBS_DEB_CMD);
1132

1133
	ret = lbs_prepare_and_send_command(priv,
1134 1135 1136
				    CMD_802_11_RADIO_CONTROL,
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP, 0, NULL);
1137

1138
	lbs_deb_cmd("RADIO_SET: radio %d, preamble %d\n",
1139
	       priv->radioon, priv->preamble);
1140

1141
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1142 1143 1144
	return ret;
}

1145
int lbs_set_mac_packet_filter(struct lbs_private *priv)
1146 1147 1148
{
	int ret = 0;

1149
	lbs_deb_enter(LBS_DEB_CMD);
1150 1151

	/* Send MAC control command to station */
1152
	ret = lbs_prepare_and_send_command(priv,
1153
				    CMD_MAC_CONTROL, 0, 0, 0, NULL);
1154

1155
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1156 1157 1158 1159 1160 1161
	return ret;
}

/**
 *  @brief This function prepare the command before send to firmware.
 *
1162
 *  @param priv		A pointer to struct lbs_private structure
1163 1164 1165 1166 1167 1168 1169
 *  @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
 */
1170
int lbs_prepare_and_send_command(struct lbs_private *priv,
1171 1172 1173 1174 1175 1176 1177 1178 1179
			  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;

1180
	lbs_deb_enter(LBS_DEB_HOST);
1181

1182 1183
	if (!priv) {
		lbs_deb_host("PREP_CMD: priv is NULL\n");
1184 1185 1186 1187
		ret = -1;
		goto done;
	}

1188
	if (priv->surpriseremoved) {
1189
		lbs_deb_host("PREP_CMD: card removed\n");
1190 1191 1192 1193
		ret = -1;
		goto done;
	}

1194
	cmdnode = lbs_get_cmd_ctrl_node(priv);
1195 1196

	if (cmdnode == NULL) {
1197
		lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1198 1199

		/* Wake up main thread to execute next command */
1200
		wake_up_interruptible(&priv->waitq);
1201 1202 1203 1204
		ret = -1;
		goto done;
	}

1205
	lbs_set_cmd_ctrl_node(priv, cmdnode, wait_option, pdata_buf);
1206 1207 1208

	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;

1209
	lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1210 1211

	if (!cmdptr) {
1212
		lbs_deb_host("PREP_CMD: cmdptr is NULL\n");
1213
		lbs_cleanup_and_insert_cmd(priv, cmdnode);
1214 1215 1216 1217 1218
		ret = -1;
		goto done;
	}

	/* Set sequence number, command and INT option */
1219 1220
	priv->seqnum++;
	cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1221

1222
	cmdptr->command = cpu_to_le16(cmd_no);
1223 1224 1225
	cmdptr->result = 0;

	switch (cmd_no) {
1226
	case CMD_GET_HW_SPEC:
1227
		ret = lbs_cmd_hw_spec(priv, cmdptr);
1228
		break;
1229
	case CMD_802_11_PS_MODE:
1230
		ret = lbs_cmd_802_11_ps_mode(priv, cmdptr, cmd_action);
1231 1232
		break;

1233
	case CMD_802_11_SCAN:
1234
		ret = lbs_cmd_80211_scan(priv, cmdptr, pdata_buf);
1235 1236
		break;

1237
	case CMD_MAC_CONTROL:
1238
		ret = lbs_cmd_mac_control(priv, cmdptr);
1239 1240
		break;

1241 1242
	case CMD_802_11_ASSOCIATE:
	case CMD_802_11_REASSOCIATE:
1243
		ret = lbs_cmd_80211_associate(priv, cmdptr, pdata_buf);
1244 1245
		break;

1246
	case CMD_802_11_DEAUTHENTICATE:
1247
		ret = lbs_cmd_80211_deauthenticate(priv, cmdptr);
1248 1249
		break;

1250
	case CMD_802_11_SET_WEP:
1251
		ret = lbs_cmd_802_11_set_wep(priv, cmdptr, cmd_action, pdata_buf);
1252 1253
		break;

1254
	case CMD_802_11_AD_HOC_START:
1255
		ret = lbs_cmd_80211_ad_hoc_start(priv, cmdptr, pdata_buf);
1256
		break;
1257
	case CMD_CODE_DNLD:
1258 1259
		break;

1260
	case CMD_802_11_RESET:
1261
		ret = lbs_cmd_802_11_reset(priv, cmdptr, cmd_action);
1262 1263
		break;

1264
	case CMD_802_11_GET_LOG:
1265
		ret = lbs_cmd_802_11_get_log(priv, cmdptr);
1266 1267
		break;

1268
	case CMD_802_11_AUTHENTICATE:
1269
		ret = lbs_cmd_80211_authenticate(priv, cmdptr, pdata_buf);
1270 1271
		break;

1272
	case CMD_802_11_GET_STAT:
1273
		ret = lbs_cmd_802_11_get_stat(priv, cmdptr);
1274 1275
		break;

1276
	case CMD_802_11_SNMP_MIB:
1277
		ret = lbs_cmd_802_11_snmp_mib(priv, cmdptr,
1278 1279 1280
					       cmd_action, cmd_oid, pdata_buf);
		break;

1281 1282 1283
	case CMD_MAC_REG_ACCESS:
	case CMD_BBP_REG_ACCESS:
	case CMD_RF_REG_ACCESS:
1284
		ret = lbs_cmd_reg_access(priv, cmdptr, cmd_action, pdata_buf);
1285 1286
		break;

1287
	case CMD_802_11_RF_CHANNEL:
1288
		ret = lbs_cmd_802_11_rf_channel(priv, cmdptr,
1289 1290 1291
						 cmd_action, pdata_buf);
		break;

1292
	case CMD_802_11_RF_TX_POWER:
1293
		ret = lbs_cmd_802_11_rf_tx_power(priv, cmdptr,
1294 1295 1296
						  cmd_action, pdata_buf);
		break;

1297
	case CMD_802_11_RADIO_CONTROL:
1298
		ret = lbs_cmd_802_11_radio_control(priv, cmdptr, cmd_action);
1299 1300
		break;

1301
	case CMD_802_11_DATA_RATE:
1302
		ret = lbs_cmd_802_11_data_rate(priv, cmdptr, cmd_action);
1303
		break;
1304
	case CMD_802_11_RATE_ADAPT_RATESET:
1305
		ret = lbs_cmd_802_11_rate_adapt_rateset(priv,
1306 1307 1308
							 cmdptr, cmd_action);
		break;

1309
	case CMD_MAC_MULTICAST_ADR:
1310
		ret = lbs_cmd_mac_multicast_adr(priv, cmdptr, cmd_action);
1311 1312
		break;

1313
	case CMD_802_11_MONITOR_MODE:
1314
		ret = lbs_cmd_802_11_monitor_mode(priv, cmdptr,
1315 1316 1317
				          cmd_action, pdata_buf);
		break;

1318
	case CMD_802_11_AD_HOC_JOIN:
1319
		ret = lbs_cmd_80211_ad_hoc_join(priv, cmdptr, pdata_buf);
1320 1321
		break;

1322
	case CMD_802_11_RSSI:
1323
		ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1324 1325
		break;

1326
	case CMD_802_11_AD_HOC_STOP:
1327
		ret = lbs_cmd_80211_ad_hoc_stop(priv, cmdptr);
1328 1329
		break;

1330
	case CMD_802_11_ENABLE_RSN:
1331
		ret = lbs_cmd_802_11_enable_rsn(priv, cmdptr, cmd_action,
1332
				pdata_buf);
1333 1334
		break;

1335
	case CMD_802_11_KEY_MATERIAL:
1336
		ret = lbs_cmd_802_11_key_material(priv, cmdptr, cmd_action,
1337
				cmd_oid, pdata_buf);
1338 1339
		break;

1340
	case CMD_802_11_PAIRWISE_TSC:
1341
		break;
1342
	case CMD_802_11_GROUP_TSC:
1343 1344
		break;

1345
	case CMD_802_11_MAC_ADDRESS:
1346
		ret = lbs_cmd_802_11_mac_address(priv, cmdptr, cmd_action);
1347 1348
		break;

1349
	case CMD_802_11_EEPROM_ACCESS:
1350
		ret = lbs_cmd_802_11_eeprom_access(priv, cmdptr,
1351 1352 1353
						    cmd_action, pdata_buf);
		break;

1354 1355
	case CMD_802_11_SET_AFC:
	case CMD_802_11_GET_AFC:
1356 1357

		cmdptr->command = cpu_to_le16(cmd_no);
1358 1359
		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
					   S_DS_GEN);
1360 1361 1362 1363 1364 1365 1366

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

		ret = 0;
		goto done;

1367
	case CMD_802_11D_DOMAIN_INFO:
1368
		ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1369 1370 1371
						   cmd_no, cmd_action);
		break;

1372
	case CMD_802_11_SLEEP_PARAMS:
1373
		ret = lbs_cmd_802_11_sleep_params(priv, cmdptr, cmd_action);
1374
		break;
1375
	case CMD_802_11_INACTIVITY_TIMEOUT:
1376
		ret = lbs_cmd_802_11_inactivity_timeout(priv, cmdptr,
1377
							 cmd_action, pdata_buf);
1378
		lbs_set_cmd_ctrl_node(priv, cmdnode, 0, pdata_buf);
1379 1380
		break;

1381 1382
	case CMD_802_11_TPC_CFG:
		cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1383 1384 1385 1386 1387 1388 1389 1390 1391
		cmdptr->size =
		    cpu_to_le16(sizeof(struct cmd_ds_802_11_tpc_cfg) +
				     S_DS_GEN);

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

		ret = 0;
		break;
1392
	case CMD_802_11_LED_GPIO_CTRL:
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
		{
			struct mrvlietypes_ledgpio *gpio =
			    (struct mrvlietypes_ledgpio*)
			    cmdptr->params.ledgpio.data;

			memmove(&cmdptr->params.ledgpio,
				pdata_buf,
				sizeof(struct cmd_ds_802_11_led_ctrl));

			cmdptr->command =
1403
			    cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1404 1405 1406

#define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
			cmdptr->size =
H
Holger Schurig 已提交
1407 1408 1409 1410
			    cpu_to_le16(le16_to_cpu(gpio->header.len)
				+ S_DS_GEN
				+ ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
			gpio->header.len = gpio->header.len;
1411 1412 1413 1414

			ret = 0;
			break;
		}
1415 1416 1417 1418
	case CMD_802_11_SUBSCRIBE_EVENT:
		lbs_cmd_802_11_subscribe_event(priv, cmdptr,
			cmd_action, pdata_buf);
		break;
1419 1420
	case CMD_802_11_PWR_CFG:
		cmdptr->command = cpu_to_le16(CMD_802_11_PWR_CFG);
1421 1422 1423 1424 1425 1426 1427 1428
		cmdptr->size =
		    cpu_to_le16(sizeof(struct cmd_ds_802_11_pwr_cfg) +
				     S_DS_GEN);
		memmove(&cmdptr->params.pwrcfg, pdata_buf,
			sizeof(struct cmd_ds_802_11_pwr_cfg));

		ret = 0;
		break;
1429
	case CMD_BT_ACCESS:
1430
		ret = lbs_cmd_bt_access(priv, cmdptr, cmd_action, pdata_buf);
1431 1432
		break;

1433
	case CMD_FWT_ACCESS:
1434
		ret = lbs_cmd_fwt_access(priv, cmdptr, cmd_action, pdata_buf);
1435 1436
		break;

1437
	case CMD_MESH_ACCESS:
1438
		ret = lbs_cmd_mesh_access(priv, cmdptr, cmd_action, pdata_buf);
1439 1440
		break;

1441 1442
	case CMD_GET_TSF:
		cmdptr->command = cpu_to_le16(CMD_GET_TSF);
1443 1444
		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_get_tsf) +
					   S_DS_GEN);
1445 1446
		ret = 0;
		break;
1447 1448 1449
	case CMD_802_11_BEACON_CTRL:
		ret = lbs_cmd_bcn_ctrl(priv, cmdptr, cmd_action);
		break;
1450
	default:
1451
		lbs_deb_host("PREP_CMD: unknown command 0x%04x\n", cmd_no);
1452 1453 1454 1455 1456 1457
		ret = -1;
		break;
	}

	/* return error, since the command preparation failed */
	if (ret != 0) {
1458
		lbs_deb_host("PREP_CMD: command preparation failed\n");
1459
		lbs_cleanup_and_insert_cmd(priv, cmdnode);
1460 1461 1462 1463 1464 1465
		ret = -1;
		goto done;
	}

	cmdnode->cmdwaitqwoken = 0;

1466
	lbs_queue_cmd(priv, cmdnode, 1);
1467
	wake_up_interruptible(&priv->waitq);
1468

1469
	if (wait_option & CMD_OPTION_WAITFORRSP) {
1470
		lbs_deb_host("PREP_CMD: wait for response\n");
1471 1472 1473 1474 1475
		might_sleep();
		wait_event_interruptible(cmdnode->cmdwait_q,
					 cmdnode->cmdwaitqwoken);
	}

1476 1477
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd_retcode) {
1478
		lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1479 1480
		       priv->cur_cmd_retcode);
		priv->cur_cmd_retcode = 0;
1481 1482
		ret = -1;
	}
1483
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1484 1485

done:
1486
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1487 1488
	return ret;
}
1489
EXPORT_SYMBOL_GPL(lbs_prepare_and_send_command);
1490 1491 1492 1493 1494

/**
 *  @brief This function allocates the command buffer and link
 *  it to command free queue.
 *
1495
 *  @param priv		A pointer to struct lbs_private structure
1496 1497
 *  @return 		0 or -1
 */
1498
int lbs_allocate_cmd_buffer(struct lbs_private *priv)
1499 1500 1501 1502 1503 1504 1505
{
	int ret = 0;
	u32 ulbufsize;
	u32 i;
	struct cmd_ctrl_node *tempcmd_array;
	u8 *ptempvirtualaddr;

1506
	lbs_deb_enter(LBS_DEB_HOST);
1507 1508 1509 1510

	/* Allocate and initialize cmdCtrlNode */
	ulbufsize = sizeof(struct cmd_ctrl_node) * MRVDRV_NUM_OF_CMD_BUFFER;

1511
	if (!(tempcmd_array = kzalloc(ulbufsize, GFP_KERNEL))) {
1512
		lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1513 1514 1515
		ret = -1;
		goto done;
	}
1516
	priv->cmd_array = tempcmd_array;
1517 1518 1519 1520

	/* Allocate and initialize command buffers */
	ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1521
		if (!(ptempvirtualaddr = kzalloc(ulbufsize, GFP_KERNEL))) {
1522
			lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
			ret = -1;
			goto done;
		}

		/* Update command buffer virtual */
		tempcmd_array[i].bufvirtualaddr = ptempvirtualaddr;
	}

	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
		init_waitqueue_head(&tempcmd_array[i].cmdwait_q);
1533
		lbs_cleanup_and_insert_cmd(priv, &tempcmd_array[i]);
1534 1535 1536
	}

	ret = 0;
1537 1538

done:
1539
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1540 1541 1542 1543 1544 1545
	return ret;
}

/**
 *  @brief This function frees the command buffer.
 *
1546
 *  @param priv		A pointer to struct lbs_private structure
1547 1548
 *  @return 		0 or -1
 */
1549
int lbs_free_cmd_buffer(struct lbs_private *priv)
1550
{
1551
	u32 ulbufsize; /* Someone needs to die for this. Slowly and painfully */
1552 1553 1554
	unsigned int i;
	struct cmd_ctrl_node *tempcmd_array;

1555
	lbs_deb_enter(LBS_DEB_HOST);
1556 1557

	/* need to check if cmd array is allocated or not */
1558
	if (priv->cmd_array == NULL) {
1559
		lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1560 1561 1562
		goto done;
	}

1563
	tempcmd_array = priv->cmd_array;
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

	/* Release shared memory buffers */
	ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
		if (tempcmd_array[i].bufvirtualaddr) {
			kfree(tempcmd_array[i].bufvirtualaddr);
			tempcmd_array[i].bufvirtualaddr = NULL;
		}
	}

	/* Release cmd_ctrl_node */
1575 1576 1577
	if (priv->cmd_array) {
		kfree(priv->cmd_array);
		priv->cmd_array = NULL;
1578 1579 1580
	}

done:
1581
	lbs_deb_leave(LBS_DEB_HOST);
1582 1583 1584 1585 1586 1587 1588
	return 0;
}

/**
 *  @brief This function gets a free command node if available in
 *  command free queue.
 *
1589
 *  @param priv		A pointer to struct lbs_private structure
1590 1591
 *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
 */
1592
struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1593 1594 1595 1596
{
	struct cmd_ctrl_node *tempnode;
	unsigned long flags;

1597 1598
	lbs_deb_enter(LBS_DEB_HOST);

1599
	if (!priv)
1600 1601
		return NULL;

1602
	spin_lock_irqsave(&priv->driver_lock, flags);
1603

1604 1605
	if (!list_empty(&priv->cmdfreeq)) {
		tempnode = list_first_entry(&priv->cmdfreeq,
1606 1607
					    struct cmd_ctrl_node, list);
		list_del(&tempnode->list);
1608
	} else {
1609
		lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1610 1611 1612
		tempnode = NULL;
	}

1613
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1614

1615
	if (tempnode)
1616 1617
		cleanup_cmdnode(tempnode);

1618
	lbs_deb_leave(LBS_DEB_HOST);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
	return tempnode;
}

/**
 *  @brief This function cleans command node.
 *
 *  @param ptempnode	A pointer to cmdCtrlNode structure
 *  @return 		n/a
 */
static void cleanup_cmdnode(struct cmd_ctrl_node *ptempnode)
{
1630 1631
	lbs_deb_enter(LBS_DEB_HOST);

1632 1633 1634 1635 1636 1637
	if (!ptempnode)
		return;
	ptempnode->cmdwaitqwoken = 1;
	wake_up_interruptible(&ptempnode->cmdwait_q);
	ptempnode->wait_option = 0;
	ptempnode->pdata_buf = NULL;
1638
	ptempnode->callback = NULL;
1639
	ptempnode->callback_arg = 0;
1640 1641 1642

	if (ptempnode->bufvirtualaddr != NULL)
		memset(ptempnode->bufvirtualaddr, 0, MRVDRV_SIZE_OF_CMD_BUFFER);
1643 1644

	lbs_deb_leave(LBS_DEB_HOST);
1645 1646 1647 1648 1649
}

/**
 *  @brief This function initializes the command node.
 *
1650
 *  @param priv		A pointer to struct lbs_private structure
1651 1652 1653 1654 1655
 *  @param ptempnode	A pointer to cmd_ctrl_node structure
 *  @param wait_option	wait option: wait response or not
 *  @param pdata_buf	A pointer to informaion buffer
 *  @return 		0 or -1
 */
1656
void lbs_set_cmd_ctrl_node(struct lbs_private *priv,
1657
		    struct cmd_ctrl_node *ptempnode,
1658
		    u16 wait_option, void *pdata_buf)
1659
{
1660
	lbs_deb_enter(LBS_DEB_HOST);
1661 1662 1663 1664 1665 1666

	if (!ptempnode)
		return;

	ptempnode->wait_option = wait_option;
	ptempnode->pdata_buf = pdata_buf;
1667
	ptempnode->callback = NULL;
1668
	ptempnode->callback_arg = 0;
1669

1670
	lbs_deb_leave(LBS_DEB_HOST);
1671 1672 1673 1674 1675 1676 1677
}

/**
 *  @brief This function executes next command in command
 *  pending queue. It will put fimware back to PS mode
 *  if applicable.
 *
1678
 *  @param priv     A pointer to struct lbs_private structure
1679 1680
 *  @return 	   0 or -1
 */
1681
int lbs_execute_next_command(struct lbs_private *priv)
1682 1683 1684 1685 1686 1687
{
	struct cmd_ctrl_node *cmdnode = NULL;
	struct cmd_ds_command *cmdptr;
	unsigned long flags;
	int ret = 0;

1688
	// Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1689
	// only caller to us is lbs_thread() and we get even when a
1690 1691
	// data packet is received
	lbs_deb_enter(LBS_DEB_THREAD);
1692

1693
	spin_lock_irqsave(&priv->driver_lock, flags);
1694

1695
	if (priv->cur_cmd) {
1696
		lbs_pr_alert( "EXEC_NEXT_CMD: already processing command!\n");
1697
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1698 1699 1700 1701
		ret = -1;
		goto done;
	}

1702 1703
	if (!list_empty(&priv->cmdpendingq)) {
		cmdnode = list_first_entry(&priv->cmdpendingq,
1704
					   struct cmd_ctrl_node, list);
1705 1706
	}

1707
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1708 1709 1710 1711

	if (cmdnode) {
		cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;

1712
		if (is_command_allowed_in_ps(le16_to_cpu(cmdptr->command))) {
1713 1714
			if ((priv->psstate == PS_STATE_SLEEP) ||
			    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1715 1716
				lbs_deb_host(
				       "EXEC_NEXT_CMD: cannot send cmd 0x%04x in psstate %d\n",
1717
				       le16_to_cpu(cmdptr->command),
1718
				       priv->psstate);
1719 1720 1721
				ret = -1;
				goto done;
			}
1722 1723
			lbs_deb_host("EXEC_NEXT_CMD: OK to send command "
			       "0x%04x in psstate %d\n",
1724
				    le16_to_cpu(cmdptr->command),
1725 1726
				    priv->psstate);
		} else if (priv->psstate != PS_STATE_FULL_POWER) {
1727 1728 1729
			/*
			 * 1. Non-PS command:
			 * Queue it. set needtowakeup to TRUE if current state
1730
			 * is SLEEP, otherwise call lbs_ps_wakeup to send Exit_PS.
1731 1732 1733 1734 1735 1736 1737 1738
			 * 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.
			 */
			if (cmdptr->command !=
1739
			    cpu_to_le16(CMD_802_11_PS_MODE)) {
1740 1741
				/*  Prepare to send Exit PS,
				 *  this non PS command will be sent later */
1742 1743
				if ((priv->psstate == PS_STATE_SLEEP)
				    || (priv->psstate == PS_STATE_PRE_SLEEP)
1744 1745 1746
				    ) {
					/* w/ new scheme, it will not reach here.
					   since it is blocked in main_thread. */
1747
					priv->needtowakeup = 1;
1748
				} else
1749
					lbs_ps_wakeup(priv, 0);
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760

				ret = 0;
				goto done;
			} else {
				/*
				 * PS command. Ignore it if it is not Exit_PS.
				 * otherwise send it down immediately.
				 */
				struct cmd_ds_802_11_ps_mode *psm =
				    &cmdptr->params.psmode;

1761 1762
				lbs_deb_host(
				       "EXEC_NEXT_CMD: PS cmd, action 0x%02x\n",
1763 1764
				       psm->action);
				if (psm->action !=
1765
				    cpu_to_le16(CMD_SUBCMD_EXIT_PS)) {
1766 1767
					lbs_deb_host(
					       "EXEC_NEXT_CMD: ignore ENTER_PS cmd\n");
1768
					list_del(&cmdnode->list);
1769
					lbs_cleanup_and_insert_cmd(priv, cmdnode);
1770 1771 1772 1773 1774

					ret = 0;
					goto done;
				}

1775 1776
				if ((priv->psstate == PS_STATE_SLEEP) ||
				    (priv->psstate == PS_STATE_PRE_SLEEP)) {
1777 1778
					lbs_deb_host(
					       "EXEC_NEXT_CMD: ignore EXIT_PS cmd in sleep\n");
1779
					list_del(&cmdnode->list);
1780
					lbs_cleanup_and_insert_cmd(priv, cmdnode);
1781
					priv->needtowakeup = 1;
1782 1783 1784 1785 1786

					ret = 0;
					goto done;
				}

1787 1788
				lbs_deb_host(
				       "EXEC_NEXT_CMD: sending EXIT_PS\n");
1789 1790
			}
		}
1791
		list_del(&cmdnode->list);
1792
		lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1793
			    le16_to_cpu(cmdptr->command));
1794 1795 1796 1797 1798 1799
		DownloadcommandToStation(priv, cmdnode);
	} else {
		/*
		 * check if in power save mode, if yes, put the device back
		 * to PS mode
		 */
1800 1801 1802 1803 1804 1805
		if ((priv->psmode != LBS802_11POWERMODECAM) &&
		    (priv->psstate == PS_STATE_FULL_POWER) &&
		    ((priv->connect_status == LBS_CONNECTED) ||
		    (priv->mesh_connect_status == LBS_CONNECTED))) {
			if (priv->secinfo.WPAenabled ||
			    priv->secinfo.WPA2enabled) {
1806
				/* check for valid WPA group keys */
1807 1808
				if (priv->wpa_mcast_key.len ||
				    priv->wpa_unicast_key.len) {
1809
					lbs_deb_host(
1810 1811
					       "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
					       " go back to PS_SLEEP");
1812
					lbs_ps_sleep(priv, 0);
1813 1814
				}
			} else {
1815 1816 1817
				lbs_deb_host(
				       "EXEC_NEXT_CMD: cmdpendingq empty, "
				       "go back to PS_SLEEP");
1818
				lbs_ps_sleep(priv, 0);
1819 1820 1821 1822 1823 1824
			}
		}
	}

	ret = 0;
done:
1825
	lbs_deb_leave(LBS_DEB_THREAD);
1826 1827 1828
	return ret;
}

1829
void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1830 1831 1832 1833
{
	union iwreq_data iwrq;
	u8 buf[50];

1834
	lbs_deb_enter(LBS_DEB_WEXT);
1835 1836 1837 1838 1839 1840 1841 1842 1843

	memset(&iwrq, 0, sizeof(union iwreq_data));
	memset(buf, 0, sizeof(buf));

	snprintf(buf, sizeof(buf) - 1, "%s", str);

	iwrq.data.length = strlen(buf) + 1 + IW_EV_LCP_LEN;

	/* Send Event to upper layer */
1844 1845 1846
	lbs_deb_wext("event indication string %s\n", (char *)buf);
	lbs_deb_wext("event indication length %d\n", iwrq.data.length);
	lbs_deb_wext("sending wireless event IWEVCUSTOM for %s\n", str);
1847

1848
	wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1849

1850
	lbs_deb_leave(LBS_DEB_WEXT);
1851 1852
}

1853
static int sendconfirmsleep(struct lbs_private *priv, u8 *cmdptr, u16 size)
1854 1855 1856 1857
{
	unsigned long flags;
	int ret = 0;

1858
	lbs_deb_enter(LBS_DEB_HOST);
1859

1860
	lbs_deb_host("SEND_SLEEPC_CMD: before download, cmd size %d\n",
1861 1862
	       size);

1863
	lbs_deb_hex(LBS_DEB_HOST, "sleep confirm command", cmdptr, size);
1864

1865
	ret = priv->hw_host_to_card(priv, MVMS_CMD, cmdptr, size);
1866
	priv->dnld_sent = DNLD_RES_RECEIVED;
1867

1868 1869
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->intcounter || priv->currenttxskb)
1870
		lbs_deb_host("SEND_SLEEPC_CMD: intcounter %d, currenttxskb %p\n",
1871 1872
		       priv->intcounter, priv->currenttxskb);
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1873 1874 1875 1876 1877

	if (ret) {
		lbs_pr_alert(
		       "SEND_SLEEPC_CMD: Host to Card failed for Confirm Sleep\n");
	} else {
1878 1879 1880
		spin_lock_irqsave(&priv->driver_lock, flags);
		if (!priv->intcounter) {
			priv->psstate = PS_STATE_SLEEP;
1881
		} else {
1882
			lbs_deb_host("SEND_SLEEPC_CMD: after sent, intcounter %d\n",
1883
			       priv->intcounter);
1884
		}
1885
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1886

1887
		lbs_deb_host("SEND_SLEEPC_CMD: sent confirm sleep\n");
1888 1889
	}

1890
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1891 1892 1893
	return ret;
}

1894
void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1895
{
1896
	lbs_deb_enter(LBS_DEB_HOST);
1897 1898 1899 1900 1901 1902

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

1903
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1904
			      CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1905

1906
	lbs_deb_leave(LBS_DEB_HOST);
1907 1908 1909
}

/**
1910
 *  @brief This function sends Exit_PS command to firmware.
1911
 *
1912
 *  @param priv    	A pointer to struct lbs_private structure
1913 1914 1915
 *  @param wait_option	wait response or not
 *  @return 	   	n/a
 */
1916
void lbs_ps_wakeup(struct lbs_private *priv, int wait_option)
1917
{
1918
	__le32 Localpsmode;
1919

1920
	lbs_deb_enter(LBS_DEB_HOST);
1921

1922
	Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1923

1924
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1925
			      CMD_SUBCMD_EXIT_PS,
1926 1927
			      wait_option, 0, &Localpsmode);

1928
	lbs_deb_leave(LBS_DEB_HOST);
1929 1930 1931 1932 1933 1934
}

/**
 *  @brief This function checks condition and prepares to
 *  send sleep confirm command to firmware if ok.
 *
1935
 *  @param priv    	A pointer to struct lbs_private structure
1936 1937 1938
 *  @param psmode  	Power Saving mode
 *  @return 	   	n/a
 */
1939
void lbs_ps_confirm_sleep(struct lbs_private *priv, u16 psmode)
1940 1941 1942 1943
{
	unsigned long flags =0;
	u8 allowed = 1;

1944
	lbs_deb_enter(LBS_DEB_HOST);
1945

1946
	if (priv->dnld_sent) {
1947
		allowed = 0;
1948
		lbs_deb_host("dnld_sent was set");
1949 1950
	}

1951 1952
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd) {
1953
		allowed = 0;
1954
		lbs_deb_host("cur_cmd was set");
1955
	}
1956
	if (priv->intcounter > 0) {
1957
		allowed = 0;
1958
		lbs_deb_host("intcounter %d", priv->intcounter);
1959
	}
1960
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1961 1962

	if (allowed) {
1963
		lbs_deb_host("sending lbs_ps_confirm_sleep\n");
1964
		sendconfirmsleep(priv, (u8 *) & priv->lbs_ps_confirm_sleep,
1965 1966
				 sizeof(struct PS_CMD_ConfirmSleep));
	} else {
1967
		lbs_deb_host("sleep confirm has been delayed\n");
1968 1969
	}

1970
	lbs_deb_leave(LBS_DEB_HOST);
1971
}
1972 1973


1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
/**
 *  @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;

	lbs_deb_enter(LBS_DEB_CMD);

	copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
	lbs_deb_cmd("Copying back %u bytes; command response was %u bytes, "
		    "copy back buffer was %u bytes", copy_len, resp->size,
		    buf->size);
	memcpy(buf, resp, copy_len);

	lbs_deb_leave(LBS_DEB_CMD);
	return 0;
}

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
/**
 *  @brief Simple way to call firmware functions
 *
 *  @param priv    	A pointer to struct lbs_private structure
 *  @param psmode  	one of the many CMD_802_11_xxxx
 *  @param cmd          pointer to the parameters structure for above command
 *                      (this should not include the command, size, sequence
 *                      and result fields from struct cmd_ds_gen)
 *  @param cmd_size     size structure pointed to by cmd
 *  @param rsp          pointer to an area where the result should be placed
 *  @param rsp_size     pointer to the size of the rsp area. If the firmware
 *                      returns fewer bytes, then this *rsp_size will be
 *                      changed to the actual size.
 *  @return 	   	-1 in case of a higher level error, otherwise
 *                      the result code from the firmware
 */
2018 2019 2020
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 *),
2021
	      unsigned long callback_arg)
2022 2023
{
	struct cmd_ctrl_node *cmdnode;
2024
	struct cmd_header *send_cmd;
2025 2026 2027 2028 2029
	unsigned long flags;
	int ret = 0;

	lbs_deb_enter(LBS_DEB_HOST);

2030 2031
	if (!priv) {
		lbs_deb_host("PREP_CMD: priv is NULL\n");
2032 2033 2034 2035
		ret = -1;
		goto done;
	}

2036
	if (priv->surpriseremoved) {
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
		lbs_deb_host("PREP_CMD: card removed\n");
		ret = -1;
		goto done;
	}

	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);
		ret = -1;
		goto done;
	}

2052
	send_cmd = (struct cmd_header *) cmdnode->bufvirtualaddr;
2053
	cmdnode->wait_option = CMD_OPTION_WAITFORRSP;
2054
	cmdnode->callback = callback;
2055
	cmdnode->callback_arg = callback_arg;
2056

2057 2058 2059
	/* Copy the incoming command to the buffer */
	memcpy(send_cmd, in_cmd, in_cmd_size);

2060
	/* Set sequence number, clean result, move to buffer */
2061
	priv->seqnum++;
2062 2063 2064 2065
	send_cmd->command = cpu_to_le16(command);
	send_cmd->size    = cpu_to_le16(in_cmd_size);
	send_cmd->seqnum  = cpu_to_le16(priv->seqnum);
	send_cmd->result  = 0;
2066 2067 2068 2069 2070 2071 2072

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

	/* here was the big old switch() statement, which is now obsolete,
	 * because the caller of lbs_cmd() sets up all of *cmd for us. */

	cmdnode->cmdwaitqwoken = 0;
2073
	lbs_queue_cmd(priv, cmdnode, 1);
2074 2075 2076 2077 2078
	wake_up_interruptible(&priv->waitq);

	might_sleep();
	wait_event_interruptible(cmdnode->cmdwait_q, cmdnode->cmdwaitqwoken);

2079 2080
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd_retcode) {
2081
		lbs_deb_host("PREP_CMD: command failed with return code %d\n",
2082 2083
		       priv->cur_cmd_retcode);
		priv->cur_cmd_retcode = 0;
2084 2085
		ret = -1;
	}
2086
	spin_unlock_irqrestore(&priv->driver_lock, flags);
2087 2088 2089 2090 2091

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