cmdresp.c 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
  * This file contains the handling of command
  * responses as well as events generated by firmware.
  */
#include <linux/delay.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>

#include <net/iw_handler.h>

#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "join.h"
#include "wext.h"

/**
 *  @brief This function handles disconnect event. it
 *  reports disconnect to upper layer, clean tx/rx packets,
 *  reset link state etc.
 *
23
 *  @param priv    A pointer to struct lbs_private structure
24 25
 *  @return 	   n/a
 */
26
void lbs_mac_event_disconnected(struct lbs_private *priv)
27 28 29
{
	union iwreq_data wrqu;

30
	if (priv->connect_status != LBS_CONNECTED)
31 32
		return;

33
	lbs_deb_enter(LBS_DEB_ASSOC);
34 35 36 37 38 39 40 41 42 43

	memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;

	/*
	 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
	 * It causes problem in the Supplicant
	 */

	msleep_interruptible(1000);
44
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
45 46

	/* report disconnect to upper layer */
47 48
	netif_stop_queue(priv->dev);
	netif_carrier_off(priv->dev);
49

50 51 52 53 54
	/* Free Tx and Rx packets */
	kfree_skb(priv->currenttxskb);
	priv->currenttxskb = NULL;
	priv->tx_pending_len = 0;

55
	/* reset SNR/NF/RSSI values */
56 57 58 59 60 61 62 63
	memset(priv->SNR, 0x00, sizeof(priv->SNR));
	memset(priv->NF, 0x00, sizeof(priv->NF));
	memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
	memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
	memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
	priv->nextSNRNF = 0;
	priv->numSNRNF = 0;
	priv->connect_status = LBS_DISCONNECTED;
64

65 66 67
	/* Clear out associated SSID and BSSID since connection is
	 * no longer valid.
	 */
68 69 70
	memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
	memset(&priv->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
	priv->curbssparams.ssid_len = 0;
71

72
	if (priv->psstate != PS_STATE_FULL_POWER) {
73
		/* make firmware to exit PS mode */
74
		lbs_deb_cmd("disconnected, so exit PS mode\n");
75
		lbs_ps_wakeup(priv, 0);
76
	}
77
	lbs_deb_leave(LBS_DEB_ASSOC);
78 79 80 81 82
}

/**
 *  @brief This function handles MIC failure event.
 *
83
 *  @param priv    A pointer to struct lbs_private structure
84 85 86
 *  @para  event   the event id
 *  @return 	   n/a
 */
87
static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
88 89 90
{
	char buf[50];

91
	lbs_deb_enter(LBS_DEB_CMD);
92 93 94 95 96 97 98 99 100 101
	memset(buf, 0, sizeof(buf));

	sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");

	if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
		strcat(buf, "unicast ");
	} else {
		strcat(buf, "multicast ");
	}

102
	lbs_send_iwevcustom_event(priv, buf);
103
	lbs_deb_leave(LBS_DEB_CMD);
104 105
}

106
static int lbs_ret_reg_access(struct lbs_private *priv,
107 108
			       u16 type, struct cmd_ds_command *resp)
{
109
	int ret = 0;
110

111
	lbs_deb_enter(LBS_DEB_CMD);
112 113

	switch (type) {
114
	case CMD_RET(CMD_MAC_REG_ACCESS):
115
		{
116
			struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
117

118 119
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = le32_to_cpu(reg->value);
120 121 122
			break;
		}

123
	case CMD_RET(CMD_BBP_REG_ACCESS):
124
		{
125
			struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
126

127 128
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = reg->value;
129 130 131
			break;
		}

132
	case CMD_RET(CMD_RF_REG_ACCESS):
133
		{
134
			struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
135

136 137
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = reg->value;
138 139 140 141
			break;
		}

	default:
142
		ret = -1;
143 144
	}

145
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
146
	return ret;
147 148
}

149
static int lbs_ret_802_11_snmp_mib(struct lbs_private *priv,
150 151 152 153 154 155
				    struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
	u16 oid = le16_to_cpu(smib->oid);
	u16 querytype = le16_to_cpu(smib->querytype);

156
	lbs_deb_enter(LBS_DEB_CMD);
157

158
	lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
159
	       querytype);
160
	lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
161

162
	if (querytype == CMD_ACT_GET) {
163
		switch (oid) {
164
		case FRAGTHRESH_I:
165
			priv->fragthsd =
166
				le16_to_cpu(*((__le16 *)(smib->value)));
167
			lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
168
				    priv->fragthsd);
169
			break;
170
		case RTSTHRESH_I:
171
			priv->rtsthsd =
172
				le16_to_cpu(*((__le16 *)(smib->value)));
173
			lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
174
				    priv->rtsthsd);
175
			break;
176
		case SHORT_RETRYLIM_I:
177
			priv->txretrycount =
178
				le16_to_cpu(*((__le16 *)(smib->value)));
179
			lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
180
				    priv->rtsthsd);
181 182 183 184 185 186
			break;
		default:
			break;
		}
	}

187
	lbs_deb_enter(LBS_DEB_CMD);
188 189 190
	return 0;
}

191
static int lbs_ret_802_11_mac_address(struct lbs_private *priv,
192 193 194 195
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;

196
	lbs_deb_enter(LBS_DEB_CMD);
197

198
	memcpy(priv->current_addr, macadd->macadd, ETH_ALEN);
199

200
	lbs_deb_enter(LBS_DEB_CMD);
201 202 203
	return 0;
}

204
static int lbs_ret_802_11_rf_tx_power(struct lbs_private *priv,
205 206 207 208
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;

209
	lbs_deb_enter(LBS_DEB_CMD);
210

211
	priv->txpowerlevel = le16_to_cpu(rtp->currentlevel);
212

213
	lbs_deb_cmd("TX power currently %d\n", priv->txpowerlevel);
214

215
	lbs_deb_leave(LBS_DEB_CMD);
216 217 218
	return 0;
}

219
static int lbs_ret_802_11_rate_adapt_rateset(struct lbs_private *priv,
220 221
					      struct cmd_ds_command *resp)
{
222
	struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
223

224
	lbs_deb_enter(LBS_DEB_CMD);
225

226
	if (rates->action == CMD_ACT_GET) {
227 228
		priv->enablehwauto = le16_to_cpu(rates->enablehwauto);
		priv->ratebitmap = le16_to_cpu(rates->bitmap);
229 230
	}

231
	lbs_deb_leave(LBS_DEB_CMD);
232 233 234
	return 0;
}

235
static int lbs_ret_802_11_rssi(struct lbs_private *priv,
236 237 238 239
				struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;

240 241
	lbs_deb_enter(LBS_DEB_CMD);

242
	/* store the non average value */
243 244
	priv->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
	priv->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
245

246 247
	priv->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
	priv->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
248

249 250 251
	priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
	    CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
		     priv->NF[TYPE_BEACON][TYPE_NOAVG]);
252

253 254 255
	priv->RSSI[TYPE_BEACON][TYPE_AVG] =
	    CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
		     priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
256

257
	lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
258 259
	       priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
	       priv->RSSI[TYPE_BEACON][TYPE_AVG]);
260

261
	lbs_deb_leave(LBS_DEB_CMD);
262 263 264
	return 0;
}

265
static int lbs_ret_802_11_eeprom_access(struct lbs_private *priv,
266 267
				  struct cmd_ds_command *resp)
{
268
	struct lbs_ioctl_regrdwr *pbuf;
269
	pbuf = (struct lbs_ioctl_regrdwr *) priv->prdeeprom;
270

271
	lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
272 273 274
	       le16_to_cpu(resp->params.rdeeprom.bytecount));
	if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
		pbuf->NOB = 0;
275
		lbs_deb_cmd("EEPROM read length too big\n");
276 277 278 279 280 281 282
		return -1;
	}
	pbuf->NOB = le16_to_cpu(resp->params.rdeeprom.bytecount);
	if (pbuf->NOB > 0) {

		memcpy(&pbuf->value, (u8 *) & resp->params.rdeeprom.value,
		       le16_to_cpu(resp->params.rdeeprom.bytecount));
283
		lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
284 285
			le16_to_cpu(resp->params.rdeeprom.bytecount));
	}
286
	lbs_deb_leave(LBS_DEB_CMD);
287 288 289
	return 0;
}

290 291 292 293 294 295 296 297 298
static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
					struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_beacon_control *bcn_ctrl =
	    &resp->params.bcn_ctrl;

	lbs_deb_enter(LBS_DEB_CMD);

	if (bcn_ctrl->action == CMD_ACT_GET) {
299 300
		priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
		priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
301 302 303 304 305 306
	}

	lbs_deb_enter(LBS_DEB_CMD);
	return 0;
}

307
static inline int handle_cmd_response(struct lbs_private *priv,
308
				      struct cmd_header *cmd_response)
309
{
310
	struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
311 312
	int ret = 0;
	unsigned long flags;
313
	uint16_t respcmd = le16_to_cpu(resp->command);
314

315 316
	lbs_deb_enter(LBS_DEB_HOST);

317
	switch (respcmd) {
318 319 320
	case CMD_RET(CMD_MAC_REG_ACCESS):
	case CMD_RET(CMD_BBP_REG_ACCESS):
	case CMD_RET(CMD_RF_REG_ACCESS):
321
		ret = lbs_ret_reg_access(priv, respcmd, resp);
322 323
		break;

324
	case CMD_RET_802_11_ASSOCIATE:
325 326
	case CMD_RET(CMD_802_11_ASSOCIATE):
	case CMD_RET(CMD_802_11_REASSOCIATE):
327
		ret = lbs_ret_80211_associate(priv, resp);
328 329
		break;

330 331
	case CMD_RET(CMD_802_11_DISASSOCIATE):
	case CMD_RET(CMD_802_11_DEAUTHENTICATE):
332
		ret = lbs_ret_80211_disassociate(priv);
333 334
		break;

335 336
	case CMD_RET(CMD_802_11_AD_HOC_START):
	case CMD_RET(CMD_802_11_AD_HOC_JOIN):
337
		ret = lbs_ret_80211_ad_hoc_start(priv, resp);
338 339
		break;

340
	case CMD_RET(CMD_802_11_SNMP_MIB):
341
		ret = lbs_ret_802_11_snmp_mib(priv, resp);
342 343
		break;

344
	case CMD_RET(CMD_802_11_RF_TX_POWER):
345
		ret = lbs_ret_802_11_rf_tx_power(priv, resp);
346 347
		break;

348 349
	case CMD_RET(CMD_802_11_SET_AFC):
	case CMD_RET(CMD_802_11_GET_AFC):
350
		spin_lock_irqsave(&priv->driver_lock, flags);
351
		memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
352
			sizeof(struct cmd_ds_802_11_afc));
353
		spin_unlock_irqrestore(&priv->driver_lock, flags);
354 355 356

		break;

357 358 359 360
	case CMD_RET(CMD_MAC_MULTICAST_ADR):
	case CMD_RET(CMD_802_11_RESET):
	case CMD_RET(CMD_802_11_AUTHENTICATE):
	case CMD_RET(CMD_802_11_BEACON_STOP):
361 362
		break;

363
	case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
364
		ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
365 366
		break;

367
	case CMD_RET(CMD_802_11_RSSI):
368
		ret = lbs_ret_802_11_rssi(priv, resp);
369 370
		break;

371
	case CMD_RET(CMD_802_11_MAC_ADDRESS):
372
		ret = lbs_ret_802_11_mac_address(priv, resp);
373 374
		break;

375
	case CMD_RET(CMD_802_11_AD_HOC_STOP):
376
		ret = lbs_ret_80211_ad_hoc_stop(priv);
377 378
		break;

379
	case CMD_RET(CMD_802_11_EEPROM_ACCESS):
380
		ret = lbs_ret_802_11_eeprom_access(priv, resp);
381 382
		break;

383
	case CMD_RET(CMD_802_11D_DOMAIN_INFO):
384
		ret = lbs_ret_802_11d_domain_info(resp);
385 386
		break;

387
	case CMD_RET(CMD_802_11_TPC_CFG):
388
		spin_lock_irqsave(&priv->driver_lock, flags);
389
		memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
390
			sizeof(struct cmd_ds_802_11_tpc_cfg));
391
		spin_unlock_irqrestore(&priv->driver_lock, flags);
392
		break;
393
	case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
394
		spin_lock_irqsave(&priv->driver_lock, flags);
395
		memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
396
			sizeof(struct cmd_ds_802_11_led_ctrl));
397
		spin_unlock_irqrestore(&priv->driver_lock, flags);
398
		break;
399

400
	case CMD_RET(CMD_GET_TSF):
401
		spin_lock_irqsave(&priv->driver_lock, flags);
402
		memcpy((void *)priv->cur_cmd->callback_arg,
403
		       &resp->params.gettsf.tsfvalue, sizeof(u64));
404
		spin_unlock_irqrestore(&priv->driver_lock, flags);
405
		break;
406
	case CMD_RET(CMD_BT_ACCESS):
407
		spin_lock_irqsave(&priv->driver_lock, flags);
408 409
		if (priv->cur_cmd->callback_arg)
			memcpy((void *)priv->cur_cmd->callback_arg,
410
			       &resp->params.bt.addr1, 2 * ETH_ALEN);
411
		spin_unlock_irqrestore(&priv->driver_lock, flags);
412
		break;
413
	case CMD_RET(CMD_FWT_ACCESS):
414
		spin_lock_irqsave(&priv->driver_lock, flags);
415 416
		if (priv->cur_cmd->callback_arg)
			memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
417
			       sizeof(resp->params.fwt));
418
		spin_unlock_irqrestore(&priv->driver_lock, flags);
419
		break;
420 421 422 423
	case CMD_RET(CMD_802_11_BEACON_CTRL):
		ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
		break;

424
	default:
425
		lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
426
			     le16_to_cpu(resp->command));
427 428
		break;
	}
429
	lbs_deb_leave(LBS_DEB_HOST);
430 431 432
	return ret;
}

433
int lbs_process_rx_command(struct lbs_private *priv)
434
{
435
	uint16_t respcmd, curcmd;
436
	struct cmd_header *resp;
437
	int ret = 0;
438 439
	unsigned long flags;
	uint16_t result;
440

441
	lbs_deb_enter(LBS_DEB_HOST);
442

443 444
	mutex_lock(&priv->lock);
	spin_lock_irqsave(&priv->driver_lock, flags);
445

446
	if (!priv->cur_cmd) {
447
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
448
		ret = -1;
449
		spin_unlock_irqrestore(&priv->driver_lock, flags);
450 451
		goto done;
	}
452

453
	resp = (void *)priv->upld_buf;
454
	curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
455 456 457
	respcmd = le16_to_cpu(resp->command);
	result = le16_to_cpu(resp->result);

458
	lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d, jiffies %lu\n",
459
		     respcmd, le16_to_cpu(resp->seqnum), priv->upld_len, jiffies);
460
	lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, priv->upld_len);
461

462
	if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
463
		lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
464
			    le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
465
		spin_unlock_irqrestore(&priv->driver_lock, flags);
466 467 468
		ret = -1;
		goto done;
	}
469
	if (respcmd != CMD_RET(curcmd) &&
470
	    respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
471 472 473 474 475 476
		lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
		spin_unlock_irqrestore(&priv->driver_lock, flags);
		ret = -1;
		goto done;
	}

477 478 479 480 481 482 483 484 485 486
	if (resp->result == cpu_to_le16(0x0004)) {
		/* 0x0004 means -EAGAIN. Drop the response, let it time out
		   and be resubmitted */
		lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
			    le16_to_cpu(resp->command));
		spin_unlock_irqrestore(&priv->driver_lock, flags);
		ret = -1;
		goto done;
	}

487 488
	/* Now we got response from FW, cancel the command timer */
	del_timer(&priv->command_timer);
489 490 491 492 493 494
	priv->cmd_timed_out = 0;
	if (priv->nr_retries) {
		lbs_pr_info("Received result %x to command %x after %d retries\n",
			    result, curcmd, priv->nr_retries);
		priv->nr_retries = 0;
	}
495 496

	/* Store the response code to cur_cmd_retcode. */
497
	priv->cur_cmd_retcode = result;
498

499
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
500
		struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
501
		u16 action = le16_to_cpu(psmode->action);
502

503 504
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
505
		       result, action);
506 507

		if (result) {
508
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
509 510 511 512
				    result);
			/*
			 * We should not re-try enter-ps command in
			 * ad-hoc mode. It takes place in
513
			 * lbs_execute_next_command().
514
			 */
515
			if (priv->mode == IW_MODE_ADHOC &&
516
			    action == CMD_SUBCMD_ENTER_PS)
517
				priv->psmode = LBS802_11POWERMODECAM;
518
		} else if (action == CMD_SUBCMD_ENTER_PS) {
519 520
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_AWAKE;
521

522
			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
523
			if (priv->connect_status != LBS_CONNECTED) {
524 525 526 527
				/*
				 * When Deauth Event received before Enter_PS command
				 * response, We need to wake up the firmware.
				 */
528
				lbs_deb_host(
529
				       "disconnected, invoking lbs_ps_wakeup\n");
530

531 532
				spin_unlock_irqrestore(&priv->driver_lock, flags);
				mutex_unlock(&priv->lock);
533
				lbs_ps_wakeup(priv, 0);
534 535
				mutex_lock(&priv->lock);
				spin_lock_irqsave(&priv->driver_lock, flags);
536
			}
537
		} else if (action == CMD_SUBCMD_EXIT_PS) {
538 539
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_FULL_POWER;
540
			lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
541
		} else {
542
			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
543 544
		}

545
		lbs_complete_command(priv, priv->cur_cmd, result);
546
		spin_unlock_irqrestore(&priv->driver_lock, flags);
547 548 549 550 551 552 553

		ret = 0;
		goto done;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
554 555
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
556 557 558 559
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
560 561
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
562
			lbs_deb_host("CMD_RESP: reset failed\n");
563 564 565
			break;

		}
566
		lbs_complete_command(priv, priv->cur_cmd, result);
567
		spin_unlock_irqrestore(&priv->driver_lock, flags);
568 569 570 571 572

		ret = -1;
		goto done;
	}

573
	spin_unlock_irqrestore(&priv->driver_lock, flags);
574

575 576
	if (priv->cur_cmd && priv->cur_cmd->callback) {
		ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
577
				resp);
578
	} else
579
		ret = handle_cmd_response(priv, resp);
580

581
	spin_lock_irqsave(&priv->driver_lock, flags);
582

583
	if (priv->cur_cmd) {
584
		/* Clean up and Put current command back to cmdfreeq */
585
		lbs_complete_command(priv, priv->cur_cmd, result);
586
	}
587
	spin_unlock_irqrestore(&priv->driver_lock, flags);
588 589

done:
590
	mutex_unlock(&priv->lock);
591
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
592 593 594
	return ret;
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
static int lbs_send_confirmwake(struct lbs_private *priv)
{
	struct cmd_header *cmd = &priv->lbs_ps_confirm_wake;
	int ret = 0;

	lbs_deb_enter(LBS_DEB_HOST);

	cmd->command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
	cmd->size = cpu_to_le16(sizeof(*cmd));
	cmd->seqnum = cpu_to_le16(++priv->seqnum);
	cmd->result = 0;

	lbs_deb_host("SEND_WAKEC_CMD: before download\n");

	lbs_deb_hex(LBS_DEB_HOST, "wake confirm command", (void *)cmd, sizeof(*cmd));

	ret = priv->hw_host_to_card(priv, MVMS_CMD, (void *)cmd, sizeof(*cmd));
	if (ret)
		lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");

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

619
int lbs_process_event(struct lbs_private *priv)
620 621 622 623
{
	int ret = 0;
	u32 eventcause;

624 625
	lbs_deb_enter(LBS_DEB_CMD);

626 627 628
	spin_lock_irq(&priv->driver_lock);
	eventcause = priv->eventcause >> SBI_EVENT_CAUSE_SHIFT;
	spin_unlock_irq(&priv->driver_lock);
629

630
	switch (eventcause) {
631
	case MACREG_INT_CODE_LINK_SENSED:
632
		lbs_deb_cmd("EVENT: link sensed\n");
633 634 635
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
636
		lbs_deb_cmd("EVENT: deauthenticated\n");
637
		lbs_mac_event_disconnected(priv);
638 639 640
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
641
		lbs_deb_cmd("EVENT: disassociated\n");
642
		lbs_mac_event_disconnected(priv);
643 644
		break;

645
	case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
646
		lbs_deb_cmd("EVENT: link lost\n");
647
		lbs_mac_event_disconnected(priv);
648 649 650
		break;

	case MACREG_INT_CODE_PS_SLEEP:
651
		lbs_deb_cmd("EVENT: ps sleep\n");
652 653

		/* handle unexpected PS SLEEP event */
654
		if (priv->psstate == PS_STATE_FULL_POWER) {
655
			lbs_deb_cmd(
656
			       "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
657 658
			break;
		}
659
		priv->psstate = PS_STATE_PRE_SLEEP;
660

661
		lbs_ps_confirm_sleep(priv);
662 663 664

		break;

665
	case MACREG_INT_CODE_HOST_AWAKE:
666
		lbs_deb_cmd("EVENT: host awake\n");
667 668 669
		lbs_send_confirmwake(priv);
		break;

670
	case MACREG_INT_CODE_PS_AWAKE:
671
		lbs_deb_cmd("EVENT: ps awake\n");
672
		/* handle unexpected PS AWAKE event */
673
		if (priv->psstate == PS_STATE_FULL_POWER) {
674
			lbs_deb_cmd(
675 676 677 678
			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
			break;
		}

679
		priv->psstate = PS_STATE_AWAKE;
680

681
		if (priv->needtowakeup) {
682 683 684
			/*
			 * wait for the command processing to finish
			 * before resuming sending
685
			 * priv->needtowakeup will be set to FALSE
686
			 * in lbs_ps_wakeup()
687
			 */
688
			lbs_deb_cmd("waking up ...\n");
689
			lbs_ps_wakeup(priv, 0);
690 691 692 693
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
694
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
695 696 697 698
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
699
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
700 701
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
		break;
702

703
	case MACREG_INT_CODE_MIB_CHANGED:
704 705
		lbs_deb_cmd("EVENT: MIB CHANGED\n");
		break;
706
	case MACREG_INT_CODE_INIT_DONE:
707
		lbs_deb_cmd("EVENT: INIT DONE\n");
708 709
		break;
	case MACREG_INT_CODE_ADHOC_BCN_LOST:
710
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
711 712
		break;
	case MACREG_INT_CODE_RSSI_LOW:
713
		lbs_pr_alert("EVENT: rssi low\n");
714 715
		break;
	case MACREG_INT_CODE_SNR_LOW:
716
		lbs_pr_alert("EVENT: snr low\n");
717 718
		break;
	case MACREG_INT_CODE_MAX_FAIL:
719
		lbs_pr_alert("EVENT: max fail\n");
720 721
		break;
	case MACREG_INT_CODE_RSSI_HIGH:
722
		lbs_pr_alert("EVENT: rssi high\n");
723 724
		break;
	case MACREG_INT_CODE_SNR_HIGH:
725
		lbs_pr_alert("EVENT: snr high\n");
726 727
		break;

728
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
729 730 731 732 733
		/* Ignore spurious autostart events if autostart is disabled */
		if (!priv->mesh_autostart_enabled) {
			lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
			break;
		}
734
		lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
735
		priv->mesh_connect_status = LBS_CONNECTED;
736
		if (priv->mesh_open) {
737
			netif_carrier_on(priv->mesh_dev);
738 739
			if (!priv->tx_pending_len)
				netif_wake_queue(priv->mesh_dev);
740
		}
741
		priv->mode = IW_MODE_ADHOC;
742
		schedule_work(&priv->sync_channel);
743 744
		break;

745
	default:
746
		lbs_pr_alert("EVENT: unknown event id %d\n", eventcause);
747 748 749
		break;
	}

750 751 752
	spin_lock_irq(&priv->driver_lock);
	priv->eventcause = 0;
	spin_unlock_irq(&priv->driver_lock);
753

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