cmd.c 53.0 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

static u16 commands_allowed_in_ps[] = {
23
	CMD_802_11_RSSI,
24 25 26 27 28 29 30 31 32
};

/**
 *  @brief This function checks if the commans is allowed
 *  in PS mode not.
 *
 *  @param command the command ID
 *  @return 	   TRUE or FALSE
 */
33
static u8 is_command_allowed_in_ps(__le16 command)
34 35 36
{
	int i;

37
	for (i = 0; i < ARRAY_SIZE(commands_allowed_in_ps); i++) {
38 39 40 41 42 43 44
		if (command == cpu_to_le16(commands_allowed_in_ps[i]))
			return 1;
	}

	return 0;
}

45
static int lbs_cmd_hw_spec(struct lbs_private *priv, struct cmd_ds_command *cmd)
46 47 48
{
	struct cmd_ds_get_hw_spec *hwspec = &cmd->params.hwspec;

49
	lbs_deb_enter(LBS_DEB_CMD);
50

51
	cmd->command = cpu_to_le16(CMD_GET_HW_SPEC);
52
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_get_hw_spec) + S_DS_GEN);
53
	memcpy(hwspec->permanentaddr, priv->current_addr, ETH_ALEN);
54

55
	lbs_deb_leave(LBS_DEB_CMD);
56 57 58
	return 0;
}

59
static int lbs_cmd_802_11_ps_mode(struct lbs_private *priv,
60 61 62 63 64
				   struct cmd_ds_command *cmd,
				   u16 cmd_action)
{
	struct cmd_ds_802_11_ps_mode *psm = &cmd->params.psmode;

65
	lbs_deb_enter(LBS_DEB_CMD);
66

67
	cmd->command = cpu_to_le16(CMD_802_11_PS_MODE);
68 69
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ps_mode) +
				S_DS_GEN);
70 71
	psm->action = cpu_to_le16(cmd_action);
	psm->multipledtim = 0;
72
	switch (cmd_action) {
73
	case CMD_SUBCMD_ENTER_PS:
74
		lbs_deb_cmd("PS command:" "SubCode- Enter PS\n");
75

76
		psm->locallisteninterval = 0;
77
		psm->nullpktinterval = 0;
78
		psm->multipledtim =
79
		    cpu_to_le16(MRVDRV_DEFAULT_MULTIPLE_DTIM);
80 81
		break;

82
	case CMD_SUBCMD_EXIT_PS:
83
		lbs_deb_cmd("PS command:" "SubCode- Exit PS\n");
84 85
		break;

86
	case CMD_SUBCMD_SLEEP_CONFIRMED:
87
		lbs_deb_cmd("PS command: SubCode- sleep confirm\n");
88 89 90 91 92 93
		break;

	default:
		break;
	}

94
	lbs_deb_leave(LBS_DEB_CMD);
95 96 97
	return 0;
}

98
static int lbs_cmd_802_11_inactivity_timeout(struct lbs_private *priv,
99 100 101 102 103
					      struct cmd_ds_command *cmd,
					      u16 cmd_action, void *pdata_buf)
{
	u16 *timeout = pdata_buf;

104 105
	lbs_deb_enter(LBS_DEB_CMD);

106
	cmd->command = cpu_to_le16(CMD_802_11_INACTIVITY_TIMEOUT);
107 108 109 110 111 112 113
	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)
114
		cmd->params.inactivity_timeout.timeout = cpu_to_le16(*timeout);
115 116 117
	else
		cmd->params.inactivity_timeout.timeout = 0;

118
	lbs_deb_leave(LBS_DEB_CMD);
119 120 121
	return 0;
}

122
static int lbs_cmd_802_11_sleep_params(struct lbs_private *priv,
123 124 125 126 127
					struct cmd_ds_command *cmd,
					u16 cmd_action)
{
	struct cmd_ds_802_11_sleep_params *sp = &cmd->params.sleep_params;

128
	lbs_deb_enter(LBS_DEB_CMD);
129

130 131
	cmd->size = cpu_to_le16((sizeof(struct cmd_ds_802_11_sleep_params)) +
				S_DS_GEN);
132
	cmd->command = cpu_to_le16(CMD_802_11_SLEEP_PARAMS);
133

134
	if (cmd_action == CMD_ACT_GET) {
135
		memset(&priv->sp, 0, sizeof(struct sleep_params));
136 137
		memset(sp, 0, sizeof(struct cmd_ds_802_11_sleep_params));
		sp->action = cpu_to_le16(cmd_action);
138
	} else if (cmd_action == CMD_ACT_SET) {
139
		sp->action = cpu_to_le16(cmd_action);
140 141 142 143 144 145
		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);
146 147
	}

148
	lbs_deb_leave(LBS_DEB_CMD);
149 150 151
	return 0;
}

152
static int lbs_cmd_802_11_set_wep(struct lbs_private *priv,
153 154 155 156 157 158 159 160
                                   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;

161
	lbs_deb_enter(LBS_DEB_CMD);
162

163
	cmd->command = cpu_to_le16(CMD_802_11_SET_WEP);
164
	cmd->size = cpu_to_le16(sizeof(*wep) + S_DS_GEN);
165

166
	if (cmd_act == CMD_ACT_ADD) {
167 168 169
		int i;

		if (!assoc_req) {
170
			lbs_deb_cmd("Invalid association request!");
171 172 173 174
			ret = -1;
			goto done;
		}

175
		wep->action = cpu_to_le16(CMD_ACT_ADD);
176 177

		/* default tx key index */
178
		wep->keyindex = cpu_to_le16((u16)(assoc_req->wep_tx_keyidx &
179
						  (u32)CMD_WEP_KEY_INDEX_MASK));
180 181 182

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

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

		/* default tx key index */
213
		wep->keyindex = cpu_to_le16((u16)(priv->wep_tx_keyidx &
214
						  (u32)CMD_WEP_KEY_INDEX_MASK));
215
		lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
216 217 218 219 220
	}

	ret = 0;

done:
221
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
222 223 224
	return ret;
}

225
static int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv,
226
				      struct cmd_ds_command *cmd,
227 228
				      u16 cmd_action,
				      void * pdata_buf)
229 230
{
	struct cmd_ds_802_11_enable_rsn *penableRSN = &cmd->params.enbrsn;
231
	u32 * enable = pdata_buf;
232 233

	lbs_deb_enter(LBS_DEB_CMD);
234

235
	cmd->command = cpu_to_le16(CMD_802_11_ENABLE_RSN);
236
	cmd->size = cpu_to_le16(sizeof(*penableRSN) + S_DS_GEN);
237
	penableRSN->action = cpu_to_le16(cmd_action);
238

239
	if (cmd_action == CMD_ACT_SET) {
240
		if (*enable)
241
			penableRSN->enable = cpu_to_le16(CMD_ENABLE_RSN);
242
		else
243
			penableRSN->enable = cpu_to_le16(CMD_DISABLE_RSN);
244
		lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
245 246
	}

247
	lbs_deb_leave(LBS_DEB_CMD);
248 249 250 251
	return 0;
}


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 292 293 294 295 296 297
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);
}

298
static void set_one_wpa_key(struct MrvlIEtype_keyParamSet * pkeyparamset,
299
                            struct enc_key * pkey)
300
{
301 302
	lbs_deb_enter(LBS_DEB_CMD);

303
	if (pkey->flags & KEY_INFO_WPA_ENABLED) {
304
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
305 306 307
	}
	if (pkey->flags & KEY_INFO_WPA_UNICAST) {
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
308 309
	}
	if (pkey->flags & KEY_INFO_WPA_MCAST) {
310 311 312 313
		pkeyparamset->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
	}

	pkeyparamset->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
314
	pkeyparamset->keytypeid = cpu_to_le16(pkey->type);
315 316 317 318 319 320
	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));
321
	lbs_deb_leave(LBS_DEB_CMD);
322 323
}

324
static int lbs_cmd_802_11_key_material(struct lbs_private *priv,
325 326 327 328 329 330
					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;
331
	struct assoc_request * assoc_req = pdata_buf;
332 333 334
	int ret = 0;
	int index = 0;

335
	lbs_deb_enter(LBS_DEB_CMD);
336

337
	cmd->command = cpu_to_le16(CMD_802_11_KEY_MATERIAL);
338 339
	pkeymaterial->action = cpu_to_le16(cmd_action);

340
	if (cmd_action == CMD_ACT_GET) {
341
		cmd->size = cpu_to_le16(S_DS_GEN + sizeof (pkeymaterial->action));
342 343 344 345 346 347
		ret = 0;
		goto done;
	}

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

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

354
	if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
355
		set_one_wpa_key(&pkeymaterial->keyParamSet[index],
356
		                &assoc_req->wpa_mcast_key);
357 358 359 360
		index++;
	}

	cmd->size = cpu_to_le16(  S_DS_GEN
361 362
	                        + sizeof (pkeymaterial->action)
	                        + (index * sizeof(struct MrvlIEtype_keyParamSet)));
363 364 365 366

	ret = 0;

done:
367
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
368 369 370
	return ret;
}

371
static int lbs_cmd_802_11_reset(struct lbs_private *priv,
372 373 374 375
				 struct cmd_ds_command *cmd, int cmd_action)
{
	struct cmd_ds_802_11_reset *reset = &cmd->params.reset;

376 377
	lbs_deb_enter(LBS_DEB_CMD);

378
	cmd->command = cpu_to_le16(CMD_802_11_RESET);
379 380 381
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
	reset->action = cpu_to_le16(cmd_action);

382
	lbs_deb_leave(LBS_DEB_CMD);
383 384 385
	return 0;
}

386
static int lbs_cmd_802_11_get_log(struct lbs_private *priv,
387 388
				   struct cmd_ds_command *cmd)
{
389
	lbs_deb_enter(LBS_DEB_CMD);
390
	cmd->command = cpu_to_le16(CMD_802_11_GET_LOG);
391 392 393
	cmd->size =
		cpu_to_le16(sizeof(struct cmd_ds_802_11_get_log) + S_DS_GEN);

394
	lbs_deb_leave(LBS_DEB_CMD);
395 396 397
	return 0;
}

398
static int lbs_cmd_802_11_get_stat(struct lbs_private *priv,
399 400
				    struct cmd_ds_command *cmd)
{
401
	lbs_deb_enter(LBS_DEB_CMD);
402
	cmd->command = cpu_to_le16(CMD_802_11_GET_STAT);
403
	cmd->size =
404
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_get_stat) + S_DS_GEN);
405

406
	lbs_deb_leave(LBS_DEB_CMD);
407 408 409
	return 0;
}

410
static int lbs_cmd_802_11_snmp_mib(struct lbs_private *priv,
411 412 413 414 415 416 417
				    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;

418
	lbs_deb_enter(LBS_DEB_CMD);
419

420
	lbs_deb_cmd("SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
421

422
	cmd->command = cpu_to_le16(CMD_802_11_SNMP_MIB);
423
	cmd->size = cpu_to_le16(sizeof(*pSNMPMIB) + S_DS_GEN);
424 425 426 427

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

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

		break;
	}

	case OID_802_11D_ENABLE:
		{
			u32 ulTemp;

448
			pSNMPMIB->oid = cpu_to_le16((u16) DOT11D_I);
449

450
			if (cmd_action == CMD_ACT_SET) {
H
Holger Schurig 已提交
451 452
				pSNMPMIB->querytype = cpu_to_le16(CMD_ACT_SET);
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
453
				ulTemp = *(u32 *)pdata_buf;
454
				*((__le16 *)(pSNMPMIB->value)) =
455 456 457 458 459 460 461 462 463
				    cpu_to_le16((u16) ulTemp);
			}
			break;
		}

	case OID_802_11_FRAGMENTATION_THRESHOLD:
		{
			u32 ulTemp;

464
			pSNMPMIB->oid = cpu_to_le16((u16) FRAGTHRESH_I);
465

466 467 468 469
			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);
470
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
471
				ulTemp = *((u32 *) pdata_buf);
472
				*((__le16 *)(pSNMPMIB->value)) =
473 474 475 476 477 478 479 480 481 482 483
				    cpu_to_le16((u16) ulTemp);

			}

			break;
		}

	case OID_802_11_RTS_THRESHOLD:
		{

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

486 487 488 489
			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);
490 491 492
				pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
				ulTemp = *((u32 *)pdata_buf);
				*(__le16 *)(pSNMPMIB->value) =
493 494 495 496 497 498
				    cpu_to_le16((u16) ulTemp);

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

501 502 503 504
		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);
505
			pSNMPMIB->bufsize = cpu_to_le16(sizeof(u16));
506
			*((__le16 *)(pSNMPMIB->value)) =
507
			    cpu_to_le16((u16) priv->txretrycount);
508 509 510 511 512 513 514
		}

		break;
	default:
		break;
	}

515
	lbs_deb_cmd(
516
	       "SNMP_CMD: command=0x%x, size=0x%x, seqnum=0x%x, result=0x%x\n",
517 518
	       le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
	       le16_to_cpu(cmd->seqnum), le16_to_cpu(cmd->result));
519

520
	lbs_deb_cmd(
521
	       "SNMP_CMD: action 0x%x, oid 0x%x, oidsize 0x%x, value 0x%x\n",
522 523 524
	       le16_to_cpu(pSNMPMIB->querytype), le16_to_cpu(pSNMPMIB->oid),
	       le16_to_cpu(pSNMPMIB->bufsize),
	       le16_to_cpu(*(__le16 *) pSNMPMIB->value));
525

526
	lbs_deb_leave(LBS_DEB_CMD);
527 528 529
	return 0;
}

530
static int lbs_cmd_802_11_radio_control(struct lbs_private *priv,
531 532 533
					 struct cmd_ds_command *cmd,
					 int cmd_action)
{
534
	struct cmd_ds_802_11_radio_control *pradiocontrol = &cmd->params.radio;
535

536
	lbs_deb_enter(LBS_DEB_CMD);
537 538 539 540

	cmd->size =
	    cpu_to_le16((sizeof(struct cmd_ds_802_11_radio_control)) +
			     S_DS_GEN);
541
	cmd->command = cpu_to_le16(CMD_802_11_RADIO_CONTROL);
542 543 544

	pradiocontrol->action = cpu_to_le16(cmd_action);

545
	switch (priv->preamble) {
546
	case CMD_TYPE_SHORT_PREAMBLE:
547 548 549
		pradiocontrol->control = cpu_to_le16(SET_SHORT_PREAMBLE);
		break;

550
	case CMD_TYPE_LONG_PREAMBLE:
551 552 553
		pradiocontrol->control = cpu_to_le16(SET_LONG_PREAMBLE);
		break;

554
	case CMD_TYPE_AUTO_PREAMBLE:
555 556 557 558 559
	default:
		pradiocontrol->control = cpu_to_le16(SET_AUTO_PREAMBLE);
		break;
	}

560
	if (priv->radioon)
561 562 563 564
		pradiocontrol->control |= cpu_to_le16(TURN_ON_RF);
	else
		pradiocontrol->control &= cpu_to_le16(~TURN_ON_RF);

565
	lbs_deb_leave(LBS_DEB_CMD);
566 567 568
	return 0;
}

569
static int lbs_cmd_802_11_rf_tx_power(struct lbs_private *priv,
570 571 572 573 574 575
				       struct cmd_ds_command *cmd,
				       u16 cmd_action, void *pdata_buf)
{

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

576
	lbs_deb_enter(LBS_DEB_CMD);
577 578

	cmd->size =
579
	    cpu_to_le16((sizeof(struct cmd_ds_802_11_rf_tx_power)) + S_DS_GEN);
580
	cmd->command = cpu_to_le16(CMD_802_11_RF_TX_POWER);
581
	prtp->action = cpu_to_le16(cmd_action);
582

583 584 585
	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));
586 587

	switch (cmd_action) {
588 589
	case CMD_ACT_TX_POWER_OPT_GET:
		prtp->action = cpu_to_le16(CMD_ACT_GET);
590 591 592
		prtp->currentlevel = 0;
		break;

593 594 595
	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);
596 597
		break;

598 599 600
	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);
601 602
		break;

603 604
	case CMD_ACT_TX_POWER_OPT_SET_LOW:
		prtp->action = cpu_to_le16(CMD_ACT_SET);
605 606 607
		prtp->currentlevel = cpu_to_le16(*((u16 *) pdata_buf));
		break;
	}
608 609

	lbs_deb_leave(LBS_DEB_CMD);
610 611 612
	return 0;
}

613
static int lbs_cmd_802_11_monitor_mode(struct lbs_private *priv,
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
				      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;
}

633
static int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
634 635 636 637 638 639
					      struct cmd_ds_command *cmd,
					      u16 cmd_action)
{
	struct cmd_ds_802_11_rate_adapt_rateset
	*rateadapt = &cmd->params.rateset;

640
	lbs_deb_enter(LBS_DEB_CMD);
641 642 643
	cmd->size =
	    cpu_to_le16(sizeof(struct cmd_ds_802_11_rate_adapt_rateset)
			     + S_DS_GEN);
644
	cmd->command = cpu_to_le16(CMD_802_11_RATE_ADAPT_RATESET);
645

646
	rateadapt->action = cpu_to_le16(cmd_action);
647 648
	rateadapt->enablehwauto = cpu_to_le16(priv->enablehwauto);
	rateadapt->bitmap = cpu_to_le16(priv->ratebitmap);
649

650
	lbs_deb_leave(LBS_DEB_CMD);
651 652 653
	return 0;
}

654
static int lbs_cmd_802_11_data_rate(struct lbs_private *priv,
655 656 657 658 659
				     struct cmd_ds_command *cmd,
				     u16 cmd_action)
{
	struct cmd_ds_802_11_data_rate *pdatarate = &cmd->params.drate;

660
	lbs_deb_enter(LBS_DEB_CMD);
661

662
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_data_rate) +
663
			     S_DS_GEN);
664
	cmd->command = cpu_to_le16(CMD_802_11_DATA_RATE);
665 666 667
	memset(pdatarate, 0, sizeof(struct cmd_ds_802_11_data_rate));
	pdatarate->action = cpu_to_le16(cmd_action);

668
	if (cmd_action == CMD_ACT_SET_TX_FIX_RATE) {
669
		pdatarate->rates[0] = lbs_data_rate_to_fw_index(priv->cur_rate);
670
		lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n",
671
		       priv->cur_rate);
672
	} else if (cmd_action == CMD_ACT_SET_TX_AUTO) {
673
		lbs_deb_cmd("DATA_RATE: setting auto\n");
674 675
	}

676
	lbs_deb_leave(LBS_DEB_CMD);
677 678 679
	return 0;
}

680
static int lbs_cmd_mac_multicast_adr(struct lbs_private *priv,
681 682 683 684 685
				      struct cmd_ds_command *cmd,
				      u16 cmd_action)
{
	struct cmd_ds_mac_multicast_adr *pMCastAdr = &cmd->params.madr;

686
	lbs_deb_enter(LBS_DEB_CMD);
687
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_multicast_adr) +
688
			     S_DS_GEN);
689
	cmd->command = cpu_to_le16(CMD_MAC_MULTICAST_ADR);
690

691
	lbs_deb_cmd("MULTICAST_ADR: setting %d addresses\n", pMCastAdr->nr_of_adrs);
692 693
	pMCastAdr->action = cpu_to_le16(cmd_action);
	pMCastAdr->nr_of_adrs =
694 695 696
	    cpu_to_le16((u16) priv->nr_of_multicastmacaddr);
	memcpy(pMCastAdr->maclist, priv->multicastlist,
	       priv->nr_of_multicastmacaddr * ETH_ALEN);
697

698
	lbs_deb_leave(LBS_DEB_CMD);
699 700 701
	return 0;
}

702
static int lbs_cmd_802_11_rf_channel(struct lbs_private *priv,
703 704 705 706 707
				      struct cmd_ds_command *cmd,
				      int option, void *pdata_buf)
{
	struct cmd_ds_802_11_rf_channel *rfchan = &cmd->params.rfchannel;

708
	lbs_deb_enter(LBS_DEB_CMD);
709
	cmd->command = cpu_to_le16(CMD_802_11_RF_CHANNEL);
710 711
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rf_channel) +
				S_DS_GEN);
712

713
	if (option == CMD_OPT_802_11_RF_CHANNEL_SET) {
714 715 716 717 718
		rfchan->currentchannel = cpu_to_le16(*((u16 *) pdata_buf));
	}

	rfchan->action = cpu_to_le16(option);

719
	lbs_deb_leave(LBS_DEB_CMD);
720 721 722
	return 0;
}

723
static int lbs_cmd_802_11_rssi(struct lbs_private *priv,
724 725 726
				struct cmd_ds_command *cmd)
{

727
	lbs_deb_enter(LBS_DEB_CMD);
728
	cmd->command = cpu_to_le16(CMD_802_11_RSSI);
729
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) + S_DS_GEN);
730
	cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
731 732

	/* reset Beacon SNR/NF/RSSI values */
733 734 735 736 737 738
	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;
739

740
	lbs_deb_leave(LBS_DEB_CMD);
741 742 743
	return 0;
}

744
static int lbs_cmd_reg_access(struct lbs_private *priv,
745 746 747
			       struct cmd_ds_command *cmdptr,
			       u8 cmd_action, void *pdata_buf)
{
748
	struct lbs_offset_value *offval;
749

750
	lbs_deb_enter(LBS_DEB_CMD);
751

752
	offval = (struct lbs_offset_value *)pdata_buf;
753

H
Holger Schurig 已提交
754
	switch (le16_to_cpu(cmdptr->command)) {
755
	case CMD_MAC_REG_ACCESS:
756 757 758 759
		{
			struct cmd_ds_mac_reg_access *macreg;

			cmdptr->size =
760 761
			    cpu_to_le16(sizeof (struct cmd_ds_mac_reg_access)
					+ S_DS_GEN);
762 763 764 765 766 767 768 769 770 771 772
			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;
		}

773
	case CMD_BBP_REG_ACCESS:
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
		{
			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;
		}

792
	case CMD_RF_REG_ACCESS:
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		{
			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;
	}

815
	lbs_deb_leave(LBS_DEB_CMD);
816 817 818
	return 0;
}

819
static int lbs_cmd_802_11_mac_address(struct lbs_private *priv,
820 821 822 823
				       struct cmd_ds_command *cmd,
				       u16 cmd_action)
{

824
	lbs_deb_enter(LBS_DEB_CMD);
825
	cmd->command = cpu_to_le16(CMD_802_11_MAC_ADDRESS);
826
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_mac_address) +
827 828 829 830 831
			     S_DS_GEN);
	cmd->result = 0;

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

832
	if (cmd_action == CMD_ACT_SET) {
833
		memcpy(cmd->params.macadd.macadd,
834 835
		       priv->current_addr, ETH_ALEN);
		lbs_deb_hex(LBS_DEB_CMD, "SET_CMD: MAC addr", priv->current_addr, 6);
836 837
	}

838
	lbs_deb_leave(LBS_DEB_CMD);
839 840 841
	return 0;
}

842
static int lbs_cmd_802_11_eeprom_access(struct lbs_private *priv,
843 844 845
					 struct cmd_ds_command *cmd,
					 int cmd_action, void *pdata_buf)
{
846
	struct lbs_ioctl_regrdwr *ea = pdata_buf;
847

848
	lbs_deb_enter(LBS_DEB_CMD);
849

850
	cmd->command = cpu_to_le16(CMD_802_11_EEPROM_ACCESS);
851 852
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) +
				S_DS_GEN);
853 854 855 856 857 858 859
	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;

860
	lbs_deb_leave(LBS_DEB_CMD);
861 862 863
	return 0;
}

864
static int lbs_cmd_bt_access(struct lbs_private *priv,
865 866 867 868
			       struct cmd_ds_command *cmd,
			       u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
869
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
870

871
	cmd->command = cpu_to_le16(CMD_BT_ACCESS);
872
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) + S_DS_GEN);
873 874 875 876
	cmd->result = 0;
	bt_access->action = cpu_to_le16(cmd_action);

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

902
static int lbs_cmd_fwt_access(struct lbs_private *priv,
903 904 905 906
			       struct cmd_ds_command *cmd,
			       u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
907
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
908

909
	cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
910
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) + S_DS_GEN);
911 912 913 914 915 916 917 918 919
	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);

920
	lbs_deb_leave(LBS_DEB_CMD);
921 922 923
	return 0;
}

924
static int lbs_cmd_mesh_access(struct lbs_private *priv,
925 926 927 928
				struct cmd_ds_command *cmd,
				u16 cmd_action, void *pdata_buf)
{
	struct cmd_ds_mesh_access *mesh_access = &cmd->params.mesh;
929
	lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
930

931
	cmd->command = cpu_to_le16(CMD_MESH_ACCESS);
932
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mesh_access) + S_DS_GEN);
933 934 935 936 937 938 939 940 941
	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);

942
	lbs_deb_leave(LBS_DEB_CMD);
943 944 945
	return 0;
}

946 947 948 949 950 951 952 953 954 955 956 957 958 959
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);
960 961
	bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
	bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
962 963 964 965 966

	lbs_deb_leave(LBS_DEB_CMD);
	return 0;
}

967
/*
968
 * Note: NEVER use lbs_queue_cmd() with addtail==0 other than for
969 970
 * the command timer, because it does not account for queued commands.
 */
971
void lbs_queue_cmd(struct lbs_private *priv,
972 973
	struct cmd_ctrl_node *cmdnode,
	u8 addtail)
974 975 976 977
{
	unsigned long flags;
	struct cmd_ds_command *cmdptr;

978
	lbs_deb_enter(LBS_DEB_HOST);
979 980

	if (!cmdnode) {
981
		lbs_deb_host("QUEUE_CMD: cmdnode is NULL\n");
982 983 984 985 986
		goto done;
	}

	cmdptr = (struct cmd_ds_command *)cmdnode->bufvirtualaddr;
	if (!cmdptr) {
987
		lbs_deb_host("QUEUE_CMD: cmdptr is NULL\n");
988 989 990 991
		goto done;
	}

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

1000
	spin_lock_irqsave(&priv->driver_lock, flags);
1001

1002
	if (addtail)
1003
		list_add_tail(&cmdnode->list, &priv->cmdpendingq);
1004
	else
1005
		list_add(&cmdnode->list, &priv->cmdpendingq);
1006

1007
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1008

1009
	lbs_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
1010
	       le16_to_cpu(((struct cmd_ds_gen*)cmdnode->bufvirtualaddr)->command));
1011 1012

done:
1013
	lbs_deb_leave(LBS_DEB_HOST);
1014 1015 1016 1017
}

/*
 * TODO: Fix the issue when DownloadcommandToStation is being called the
1018
 * second time when the command times out. All the cmdptr->xxx are in little
1019 1020 1021 1022
 * 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
 */
1023
static int DownloadcommandToStation(struct lbs_private *priv,
1024 1025 1026 1027
				    struct cmd_ctrl_node *cmdnode)
{
	unsigned long flags;
	struct cmd_ds_command *cmdptr;
1028
	int ret = -1;
1029 1030 1031
	u16 cmdsize;
	u16 command;

1032
	lbs_deb_enter(LBS_DEB_HOST);
1033

1034 1035
	if (!priv || !cmdnode) {
		lbs_deb_host("DNLD_CMD: priv or cmdmode is NULL\n");
1036 1037 1038 1039 1040
		goto done;
	}

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

1041
	spin_lock_irqsave(&priv->driver_lock, flags);
1042
	if (!cmdptr || !cmdptr->size) {
1043
		lbs_deb_host("DNLD_CMD: cmdptr is NULL or zero\n");
1044
		__lbs_cleanup_and_insert_cmd(priv, cmdnode);
1045
		spin_unlock_irqrestore(&priv->driver_lock, flags);
1046 1047 1048
		goto done;
	}

1049 1050 1051
	priv->cur_cmd = cmdnode;
	priv->cur_cmd_retcode = 0;
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1052

H
Holger Schurig 已提交
1053 1054
	cmdsize = le16_to_cpu(cmdptr->size);
	command = le16_to_cpu(cmdptr->command);
1055

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

1060 1061
	cmdnode->cmdwaitqwoken = 0;

1062
	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmdptr, cmdsize);
1063 1064

	if (ret != 0) {
1065
		lbs_deb_host("DNLD_CMD: hw_host_to_card failed\n");
1066 1067 1068 1069 1070
		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);
1071 1072 1073
		goto done;
	}

1074
	lbs_deb_cmd("DNLD_CMD: sent command 0x%04x, jiffies %lu\n", command, jiffies);
1075 1076

	/* Setup the timer after transmit command */
1077 1078
	if (command == CMD_802_11_SCAN || command == CMD_802_11_AUTHENTICATE
	    || command == CMD_802_11_ASSOCIATE)
1079
		mod_timer(&priv->command_timer, jiffies + (10*HZ));
1080
	else
1081
		mod_timer(&priv->command_timer, jiffies + (5*HZ));
1082 1083 1084

	ret = 0;

1085
done:
1086
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1087 1088 1089
	return ret;
}

1090
static int lbs_cmd_mac_control(struct lbs_private *priv,
1091 1092 1093 1094
				struct cmd_ds_command *cmd)
{
	struct cmd_ds_mac_control *mac = &cmd->params.macctrl;

1095
	lbs_deb_enter(LBS_DEB_CMD);
1096

1097
	cmd->command = cpu_to_le16(CMD_MAC_CONTROL);
1098
	cmd->size = cpu_to_le16(sizeof(struct cmd_ds_mac_control) + S_DS_GEN);
1099
	mac->action = cpu_to_le16(priv->currentpacketfilter);
1100

1101
	lbs_deb_cmd("MAC_CONTROL: action 0x%x, size %d\n",
1102
		    le16_to_cpu(mac->action), le16_to_cpu(cmd->size));
1103

1104
	lbs_deb_leave(LBS_DEB_CMD);
1105 1106 1107 1108 1109
	return 0;
}

/**
 *  This function inserts command node to cmdfreeq
1110
 *  after cleans it. Requires priv->driver_lock held.
1111
 */
1112 1113
void __lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
	struct cmd_ctrl_node *ptempcmd)
1114 1115 1116
{

	if (!ptempcmd)
1117
		return;
1118 1119

	cleanup_cmdnode(ptempcmd);
1120
	list_add_tail(&ptempcmd->list, &priv->cmdfreeq);
1121 1122
}

1123 1124
static void lbs_cleanup_and_insert_cmd(struct lbs_private *priv,
	struct cmd_ctrl_node *ptempcmd)
1125 1126 1127
{
	unsigned long flags;

1128
	spin_lock_irqsave(&priv->driver_lock, flags);
1129
	__lbs_cleanup_and_insert_cmd(priv, ptempcmd);
1130
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1131 1132
}

1133
int lbs_set_radio_control(struct lbs_private *priv)
1134 1135 1136
{
	int ret = 0;

1137
	lbs_deb_enter(LBS_DEB_CMD);
1138

1139
	ret = lbs_prepare_and_send_command(priv,
1140 1141 1142
				    CMD_802_11_RADIO_CONTROL,
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP, 0, NULL);
1143

1144
	lbs_deb_cmd("RADIO_SET: radio %d, preamble %d\n",
1145
	       priv->radioon, priv->preamble);
1146

1147
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1148 1149 1150
	return ret;
}

1151
int lbs_set_mac_packet_filter(struct lbs_private *priv)
1152 1153 1154
{
	int ret = 0;

1155
	lbs_deb_enter(LBS_DEB_CMD);
1156 1157

	/* Send MAC control command to station */
1158
	ret = lbs_prepare_and_send_command(priv,
1159
				    CMD_MAC_CONTROL, 0, 0, 0, NULL);
1160

1161
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1162 1163 1164 1165 1166 1167
	return ret;
}

/**
 *  @brief This function prepare the command before send to firmware.
 *
1168
 *  @param priv		A pointer to struct lbs_private structure
1169 1170 1171 1172 1173 1174 1175
 *  @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
 */
1176
int lbs_prepare_and_send_command(struct lbs_private *priv,
1177 1178 1179 1180 1181 1182 1183 1184 1185
			  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;

1186
	lbs_deb_enter(LBS_DEB_HOST);
1187

1188 1189
	if (!priv) {
		lbs_deb_host("PREP_CMD: priv is NULL\n");
1190 1191 1192 1193
		ret = -1;
		goto done;
	}

1194
	if (priv->surpriseremoved) {
1195
		lbs_deb_host("PREP_CMD: card removed\n");
1196 1197 1198 1199
		ret = -1;
		goto done;
	}

1200
	cmdnode = lbs_get_cmd_ctrl_node(priv);
1201 1202

	if (cmdnode == NULL) {
1203
		lbs_deb_host("PREP_CMD: cmdnode is NULL\n");
1204 1205

		/* Wake up main thread to execute next command */
1206
		wake_up_interruptible(&priv->waitq);
1207 1208 1209 1210
		ret = -1;
		goto done;
	}

1211
	lbs_set_cmd_ctrl_node(priv, cmdnode, wait_option, pdata_buf);
1212 1213 1214

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

1215
	lbs_deb_host("PREP_CMD: command 0x%04x\n", cmd_no);
1216 1217

	if (!cmdptr) {
1218
		lbs_deb_host("PREP_CMD: cmdptr is NULL\n");
1219
		lbs_cleanup_and_insert_cmd(priv, cmdnode);
1220 1221 1222 1223 1224
		ret = -1;
		goto done;
	}

	/* Set sequence number, command and INT option */
1225 1226
	priv->seqnum++;
	cmdptr->seqnum = cpu_to_le16(priv->seqnum);
1227

1228
	cmdptr->command = cpu_to_le16(cmd_no);
1229 1230 1231
	cmdptr->result = 0;

	switch (cmd_no) {
1232
	case CMD_GET_HW_SPEC:
1233
		ret = lbs_cmd_hw_spec(priv, cmdptr);
1234
		break;
1235
	case CMD_802_11_PS_MODE:
1236
		ret = lbs_cmd_802_11_ps_mode(priv, cmdptr, cmd_action);
1237 1238
		break;

1239
	case CMD_802_11_SCAN:
1240
		ret = lbs_cmd_80211_scan(priv, cmdptr, pdata_buf);
1241 1242
		break;

1243
	case CMD_MAC_CONTROL:
1244
		ret = lbs_cmd_mac_control(priv, cmdptr);
1245 1246
		break;

1247 1248
	case CMD_802_11_ASSOCIATE:
	case CMD_802_11_REASSOCIATE:
1249
		ret = lbs_cmd_80211_associate(priv, cmdptr, pdata_buf);
1250 1251
		break;

1252
	case CMD_802_11_DEAUTHENTICATE:
1253
		ret = lbs_cmd_80211_deauthenticate(priv, cmdptr);
1254 1255
		break;

1256
	case CMD_802_11_SET_WEP:
1257
		ret = lbs_cmd_802_11_set_wep(priv, cmdptr, cmd_action, pdata_buf);
1258 1259
		break;

1260
	case CMD_802_11_AD_HOC_START:
1261
		ret = lbs_cmd_80211_ad_hoc_start(priv, cmdptr, pdata_buf);
1262
		break;
1263
	case CMD_CODE_DNLD:
1264 1265
		break;

1266
	case CMD_802_11_RESET:
1267
		ret = lbs_cmd_802_11_reset(priv, cmdptr, cmd_action);
1268 1269
		break;

1270
	case CMD_802_11_GET_LOG:
1271
		ret = lbs_cmd_802_11_get_log(priv, cmdptr);
1272 1273
		break;

1274
	case CMD_802_11_AUTHENTICATE:
1275
		ret = lbs_cmd_80211_authenticate(priv, cmdptr, pdata_buf);
1276 1277
		break;

1278
	case CMD_802_11_GET_STAT:
1279
		ret = lbs_cmd_802_11_get_stat(priv, cmdptr);
1280 1281
		break;

1282
	case CMD_802_11_SNMP_MIB:
1283
		ret = lbs_cmd_802_11_snmp_mib(priv, cmdptr,
1284 1285 1286
					       cmd_action, cmd_oid, pdata_buf);
		break;

1287 1288 1289
	case CMD_MAC_REG_ACCESS:
	case CMD_BBP_REG_ACCESS:
	case CMD_RF_REG_ACCESS:
1290
		ret = lbs_cmd_reg_access(priv, cmdptr, cmd_action, pdata_buf);
1291 1292
		break;

1293
	case CMD_802_11_RF_CHANNEL:
1294
		ret = lbs_cmd_802_11_rf_channel(priv, cmdptr,
1295 1296 1297
						 cmd_action, pdata_buf);
		break;

1298
	case CMD_802_11_RF_TX_POWER:
1299
		ret = lbs_cmd_802_11_rf_tx_power(priv, cmdptr,
1300 1301 1302
						  cmd_action, pdata_buf);
		break;

1303
	case CMD_802_11_RADIO_CONTROL:
1304
		ret = lbs_cmd_802_11_radio_control(priv, cmdptr, cmd_action);
1305 1306
		break;

1307
	case CMD_802_11_DATA_RATE:
1308
		ret = lbs_cmd_802_11_data_rate(priv, cmdptr, cmd_action);
1309
		break;
1310
	case CMD_802_11_RATE_ADAPT_RATESET:
1311
		ret = lbs_cmd_802_11_rate_adapt_rateset(priv,
1312 1313 1314
							 cmdptr, cmd_action);
		break;

1315
	case CMD_MAC_MULTICAST_ADR:
1316
		ret = lbs_cmd_mac_multicast_adr(priv, cmdptr, cmd_action);
1317 1318
		break;

1319
	case CMD_802_11_MONITOR_MODE:
1320
		ret = lbs_cmd_802_11_monitor_mode(priv, cmdptr,
1321 1322 1323
				          cmd_action, pdata_buf);
		break;

1324
	case CMD_802_11_AD_HOC_JOIN:
1325
		ret = lbs_cmd_80211_ad_hoc_join(priv, cmdptr, pdata_buf);
1326 1327
		break;

1328
	case CMD_802_11_RSSI:
1329
		ret = lbs_cmd_802_11_rssi(priv, cmdptr);
1330 1331
		break;

1332
	case CMD_802_11_AD_HOC_STOP:
1333
		ret = lbs_cmd_80211_ad_hoc_stop(priv, cmdptr);
1334 1335
		break;

1336
	case CMD_802_11_ENABLE_RSN:
1337
		ret = lbs_cmd_802_11_enable_rsn(priv, cmdptr, cmd_action,
1338
				pdata_buf);
1339 1340
		break;

1341
	case CMD_802_11_KEY_MATERIAL:
1342
		ret = lbs_cmd_802_11_key_material(priv, cmdptr, cmd_action,
1343
				cmd_oid, pdata_buf);
1344 1345
		break;

1346
	case CMD_802_11_PAIRWISE_TSC:
1347
		break;
1348
	case CMD_802_11_GROUP_TSC:
1349 1350
		break;

1351
	case CMD_802_11_MAC_ADDRESS:
1352
		ret = lbs_cmd_802_11_mac_address(priv, cmdptr, cmd_action);
1353 1354
		break;

1355
	case CMD_802_11_EEPROM_ACCESS:
1356
		ret = lbs_cmd_802_11_eeprom_access(priv, cmdptr,
1357 1358 1359
						    cmd_action, pdata_buf);
		break;

1360 1361
	case CMD_802_11_SET_AFC:
	case CMD_802_11_GET_AFC:
1362 1363

		cmdptr->command = cpu_to_le16(cmd_no);
1364 1365
		cmdptr->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_afc) +
					   S_DS_GEN);
1366 1367 1368 1369 1370 1371 1372

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

		ret = 0;
		goto done;

1373
	case CMD_802_11D_DOMAIN_INFO:
1374
		ret = lbs_cmd_802_11d_domain_info(priv, cmdptr,
1375 1376 1377
						   cmd_no, cmd_action);
		break;

1378
	case CMD_802_11_SLEEP_PARAMS:
1379
		ret = lbs_cmd_802_11_sleep_params(priv, cmdptr, cmd_action);
1380
		break;
1381
	case CMD_802_11_INACTIVITY_TIMEOUT:
1382
		ret = lbs_cmd_802_11_inactivity_timeout(priv, cmdptr,
1383
							 cmd_action, pdata_buf);
1384
		lbs_set_cmd_ctrl_node(priv, cmdnode, 0, pdata_buf);
1385 1386
		break;

1387 1388
	case CMD_802_11_TPC_CFG:
		cmdptr->command = cpu_to_le16(CMD_802_11_TPC_CFG);
1389 1390 1391 1392 1393 1394 1395 1396 1397
		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;
1398
	case CMD_802_11_LED_GPIO_CTRL:
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		{
			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 =
1409
			    cpu_to_le16(CMD_802_11_LED_GPIO_CTRL);
1410 1411 1412

#define ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN 8
			cmdptr->size =
H
Holger Schurig 已提交
1413 1414 1415 1416
			    cpu_to_le16(le16_to_cpu(gpio->header.len)
				+ S_DS_GEN
				+ ACTION_NUMLED_TLVTYPE_LEN_FIELDS_LEN);
			gpio->header.len = gpio->header.len;
1417 1418 1419 1420

			ret = 0;
			break;
		}
1421 1422 1423 1424
	case CMD_802_11_SUBSCRIBE_EVENT:
		lbs_cmd_802_11_subscribe_event(priv, cmdptr,
			cmd_action, pdata_buf);
		break;
1425 1426
	case CMD_802_11_PWR_CFG:
		cmdptr->command = cpu_to_le16(CMD_802_11_PWR_CFG);
1427 1428 1429 1430 1431 1432 1433 1434
		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;
1435
	case CMD_BT_ACCESS:
1436
		ret = lbs_cmd_bt_access(priv, cmdptr, cmd_action, pdata_buf);
1437 1438
		break;

1439
	case CMD_FWT_ACCESS:
1440
		ret = lbs_cmd_fwt_access(priv, cmdptr, cmd_action, pdata_buf);
1441 1442
		break;

1443
	case CMD_MESH_ACCESS:
1444
		ret = lbs_cmd_mesh_access(priv, cmdptr, cmd_action, pdata_buf);
1445 1446
		break;

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

	/* return error, since the command preparation failed */
	if (ret != 0) {
1464
		lbs_deb_host("PREP_CMD: command preparation failed\n");
1465
		lbs_cleanup_and_insert_cmd(priv, cmdnode);
1466 1467 1468 1469 1470 1471
		ret = -1;
		goto done;
	}

	cmdnode->cmdwaitqwoken = 0;

1472
	lbs_queue_cmd(priv, cmdnode, 1);
1473
	wake_up_interruptible(&priv->waitq);
1474

1475
	if (wait_option & CMD_OPTION_WAITFORRSP) {
1476
		lbs_deb_host("PREP_CMD: wait for response\n");
1477 1478 1479 1480 1481
		might_sleep();
		wait_event_interruptible(cmdnode->cmdwait_q,
					 cmdnode->cmdwaitqwoken);
	}

1482 1483
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd_retcode) {
1484
		lbs_deb_host("PREP_CMD: command failed with return code %d\n",
1485 1486
		       priv->cur_cmd_retcode);
		priv->cur_cmd_retcode = 0;
1487 1488
		ret = -1;
	}
1489
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1490 1491

done:
1492
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1493 1494
	return ret;
}
1495
EXPORT_SYMBOL_GPL(lbs_prepare_and_send_command);
1496 1497 1498 1499 1500

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

1512
	lbs_deb_enter(LBS_DEB_HOST);
1513 1514 1515 1516

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

1517
	if (!(tempcmd_array = kzalloc(ulbufsize, GFP_KERNEL))) {
1518
		lbs_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
1519 1520 1521
		ret = -1;
		goto done;
	}
1522
	priv->cmd_array = tempcmd_array;
1523 1524 1525 1526

	/* Allocate and initialize command buffers */
	ulbufsize = MRVDRV_SIZE_OF_CMD_BUFFER;
	for (i = 0; i < MRVDRV_NUM_OF_CMD_BUFFER; i++) {
1527
		if (!(ptempvirtualaddr = kzalloc(ulbufsize, GFP_KERNEL))) {
1528
			lbs_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
			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);
1539
		lbs_cleanup_and_insert_cmd(priv, &tempcmd_array[i]);
1540 1541 1542
	}

	ret = 0;
1543 1544

done:
1545
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1546 1547 1548 1549 1550 1551
	return ret;
}

/**
 *  @brief This function frees the command buffer.
 *
1552
 *  @param priv		A pointer to struct lbs_private structure
1553 1554
 *  @return 		0 or -1
 */
1555
int lbs_free_cmd_buffer(struct lbs_private *priv)
1556
{
1557
	u32 ulbufsize; /* Someone needs to die for this. Slowly and painfully */
1558 1559 1560
	unsigned int i;
	struct cmd_ctrl_node *tempcmd_array;

1561
	lbs_deb_enter(LBS_DEB_HOST);
1562 1563

	/* need to check if cmd array is allocated or not */
1564
	if (priv->cmd_array == NULL) {
1565
		lbs_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
1566 1567 1568
		goto done;
	}

1569
	tempcmd_array = priv->cmd_array;
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580

	/* 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 */
1581 1582 1583
	if (priv->cmd_array) {
		kfree(priv->cmd_array);
		priv->cmd_array = NULL;
1584 1585 1586
	}

done:
1587
	lbs_deb_leave(LBS_DEB_HOST);
1588 1589 1590 1591 1592 1593 1594
	return 0;
}

/**
 *  @brief This function gets a free command node if available in
 *  command free queue.
 *
1595
 *  @param priv		A pointer to struct lbs_private structure
1596 1597
 *  @return cmd_ctrl_node A pointer to cmd_ctrl_node structure or NULL
 */
1598
struct cmd_ctrl_node *lbs_get_cmd_ctrl_node(struct lbs_private *priv)
1599 1600 1601 1602
{
	struct cmd_ctrl_node *tempnode;
	unsigned long flags;

1603 1604
	lbs_deb_enter(LBS_DEB_HOST);

1605
	if (!priv)
1606 1607
		return NULL;

1608
	spin_lock_irqsave(&priv->driver_lock, flags);
1609

1610 1611
	if (!list_empty(&priv->cmdfreeq)) {
		tempnode = list_first_entry(&priv->cmdfreeq,
1612 1613
					    struct cmd_ctrl_node, list);
		list_del(&tempnode->list);
1614
	} else {
1615
		lbs_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
1616 1617 1618
		tempnode = NULL;
	}

1619
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1620

1621
	if (tempnode)
1622 1623
		cleanup_cmdnode(tempnode);

1624
	lbs_deb_leave(LBS_DEB_HOST);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	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)
{
1636 1637
	lbs_deb_enter(LBS_DEB_HOST);

1638 1639 1640 1641 1642 1643
	if (!ptempnode)
		return;
	ptempnode->cmdwaitqwoken = 1;
	wake_up_interruptible(&ptempnode->cmdwait_q);
	ptempnode->wait_option = 0;
	ptempnode->pdata_buf = NULL;
1644
	ptempnode->callback = NULL;
1645 1646 1647

	if (ptempnode->bufvirtualaddr != NULL)
		memset(ptempnode->bufvirtualaddr, 0, MRVDRV_SIZE_OF_CMD_BUFFER);
1648 1649

	lbs_deb_leave(LBS_DEB_HOST);
1650 1651 1652 1653 1654
}

/**
 *  @brief This function initializes the command node.
 *
1655
 *  @param priv		A pointer to struct lbs_private structure
1656 1657 1658 1659 1660
 *  @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
 */
1661
void lbs_set_cmd_ctrl_node(struct lbs_private *priv,
1662
		    struct cmd_ctrl_node *ptempnode,
1663
		    u16 wait_option, void *pdata_buf)
1664
{
1665
	lbs_deb_enter(LBS_DEB_HOST);
1666 1667 1668 1669 1670 1671

	if (!ptempnode)
		return;

	ptempnode->wait_option = wait_option;
	ptempnode->pdata_buf = pdata_buf;
1672
	ptempnode->callback = NULL;
1673

1674
	lbs_deb_leave(LBS_DEB_HOST);
1675 1676 1677 1678 1679 1680 1681
}

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

1692
	// Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
1693
	// only caller to us is lbs_thread() and we get even when a
1694 1695
	// data packet is received
	lbs_deb_enter(LBS_DEB_THREAD);
1696

1697
	spin_lock_irqsave(&priv->driver_lock, flags);
1698

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

1706 1707
	if (!list_empty(&priv->cmdpendingq)) {
		cmdnode = list_first_entry(&priv->cmdpendingq,
1708
					   struct cmd_ctrl_node, list);
1709 1710
	}

1711
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1712 1713 1714 1715 1716

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

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

				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;

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

					ret = 0;
					goto done;
				}

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

					ret = 0;
					goto done;
				}

1791 1792
				lbs_deb_host(
				       "EXEC_NEXT_CMD: sending EXIT_PS\n");
1793 1794
			}
		}
1795
		list_del(&cmdnode->list);
1796
		lbs_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
1797
			    le16_to_cpu(cmdptr->command));
1798 1799 1800 1801 1802 1803
		DownloadcommandToStation(priv, cmdnode);
	} else {
		/*
		 * check if in power save mode, if yes, put the device back
		 * to PS mode
		 */
1804 1805 1806 1807 1808 1809
		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) {
1810
				/* check for valid WPA group keys */
1811 1812
				if (priv->wpa_mcast_key.len ||
				    priv->wpa_unicast_key.len) {
1813
					lbs_deb_host(
1814 1815
					       "EXEC_NEXT_CMD: WPA enabled and GTK_SET"
					       " go back to PS_SLEEP");
1816
					lbs_ps_sleep(priv, 0);
1817 1818
				}
			} else {
1819 1820 1821
				lbs_deb_host(
				       "EXEC_NEXT_CMD: cmdpendingq empty, "
				       "go back to PS_SLEEP");
1822
				lbs_ps_sleep(priv, 0);
1823 1824 1825 1826 1827 1828
			}
		}
	}

	ret = 0;
done:
1829
	lbs_deb_leave(LBS_DEB_THREAD);
1830 1831 1832
	return ret;
}

1833
void lbs_send_iwevcustom_event(struct lbs_private *priv, s8 *str)
1834 1835 1836 1837
{
	union iwreq_data iwrq;
	u8 buf[50];

1838
	lbs_deb_enter(LBS_DEB_WEXT);
1839 1840 1841 1842 1843 1844 1845 1846 1847

	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 */
1848 1849 1850
	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);
1851

1852
	wireless_send_event(priv->dev, IWEVCUSTOM, &iwrq, buf);
1853

1854
	lbs_deb_leave(LBS_DEB_WEXT);
1855 1856
}

1857
static int sendconfirmsleep(struct lbs_private *priv, u8 *cmdptr, u16 size)
1858 1859 1860 1861
{
	unsigned long flags;
	int ret = 0;

1862
	lbs_deb_enter(LBS_DEB_HOST);
1863

1864
	lbs_deb_host("SEND_SLEEPC_CMD: before download, cmd size %d\n",
1865 1866
	       size);

1867
	lbs_deb_hex(LBS_DEB_HOST, "sleep confirm command", cmdptr, size);
1868

1869
	ret = priv->hw_host_to_card(priv, MVMS_CMD, cmdptr, size);
1870
	priv->dnld_sent = DNLD_RES_RECEIVED;
1871

1872 1873
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->intcounter || priv->currenttxskb)
1874
		lbs_deb_host("SEND_SLEEPC_CMD: intcounter %d, currenttxskb %p\n",
1875 1876
		       priv->intcounter, priv->currenttxskb);
	spin_unlock_irqrestore(&priv->driver_lock, flags);
1877 1878 1879 1880 1881

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

1891
		lbs_deb_host("SEND_SLEEPC_CMD: sent confirm sleep\n");
1892 1893
	}

1894
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
1895 1896 1897
	return ret;
}

1898
void lbs_ps_sleep(struct lbs_private *priv, int wait_option)
1899
{
1900
	lbs_deb_enter(LBS_DEB_HOST);
1901 1902 1903 1904 1905 1906

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

1907
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1908
			      CMD_SUBCMD_ENTER_PS, wait_option, 0, NULL);
1909

1910
	lbs_deb_leave(LBS_DEB_HOST);
1911 1912 1913
}

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

1924
	lbs_deb_enter(LBS_DEB_HOST);
1925

1926
	Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1927

1928
	lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1929
			      CMD_SUBCMD_EXIT_PS,
1930 1931
			      wait_option, 0, &Localpsmode);

1932
	lbs_deb_leave(LBS_DEB_HOST);
1933 1934 1935 1936 1937 1938
}

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

1948
	lbs_deb_enter(LBS_DEB_HOST);
1949

1950
	if (priv->dnld_sent) {
1951
		allowed = 0;
1952
		lbs_deb_host("dnld_sent was set");
1953 1954
	}

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

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

1974
	lbs_deb_leave(LBS_DEB_HOST);
1975
}
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993


/**
 *  @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
 */
1994

1995 1996
int lbs_cmd(struct lbs_private *priv, uint16_t command, void *cmd, int cmd_size,
	    int (*callback)(uint16_t, struct cmd_ds_command *, struct lbs_private *))
1997 1998 1999 2000 2001 2002 2003 2004
{
	struct cmd_ctrl_node *cmdnode;
	struct cmd_ds_gen *cmdptr;
	unsigned long flags;
	int ret = 0;

	lbs_deb_enter(LBS_DEB_HOST);

2005 2006
	if (!priv) {
		lbs_deb_host("PREP_CMD: priv is NULL\n");
2007 2008 2009 2010
		ret = -1;
		goto done;
	}

2011
	if (priv->surpriseremoved) {
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
		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;
	}

	cmdptr = (struct cmd_ds_gen *)cmdnode->bufvirtualaddr;
	cmdnode->wait_option = CMD_OPTION_WAITFORRSP;
2030
	cmdnode->callback = callback;
2031 2032

	/* Set sequence number, clean result, move to buffer */
2033
	priv->seqnum++;
2034
	cmdptr->command = cpu_to_le16(command);
2035
	cmdptr->size    = cpu_to_le16(cmd_size + S_DS_GEN);
2036
	cmdptr->seqnum = cpu_to_le16(priv->seqnum);
2037 2038 2039 2040 2041 2042 2043 2044 2045
	cmdptr->result = 0;
	memcpy(cmdptr->cmdresp, cmd, cmd_size);

	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;
2046
	lbs_queue_cmd(priv, cmdnode, 1);
2047 2048 2049 2050 2051
	wake_up_interruptible(&priv->waitq);

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

2052 2053
	spin_lock_irqsave(&priv->driver_lock, flags);
	if (priv->cur_cmd_retcode) {
2054
		lbs_deb_host("PREP_CMD: command failed with return code %d\n",
2055 2056
		       priv->cur_cmd_retcode);
		priv->cur_cmd_retcode = 0;
2057 2058
		ret = -1;
	}
2059
	spin_unlock_irqrestore(&priv->driver_lock, flags);
2060 2061 2062 2063 2064 2065 2066 2067

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