cmdresp.c 27.4 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 23 24 25 26 27 28 29 30
/**
  * 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.
 *
 *  @param priv    A pointer to wlan_private structure
 *  @return 	   n/a
 */
void libertas_mac_event_disconnected(wlan_private * priv)
{
	wlan_adapter *adapter = priv->adapter;
	union iwreq_data wrqu;

31
	if (adapter->connect_status != LIBERTAS_CONNECTED)
32 33
		return;

34
	lbs_deb_enter(LBS_DEB_CMD);
35 36 37 38 39 40 41 42 43 44

	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);
45
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
46 47 48 49 50 51

	/* Free Tx and Rx packets */
	kfree_skb(priv->adapter->currenttxskb);
	priv->adapter->currenttxskb = NULL;

	/* report disconnect to upper layer */
52 53
	netif_stop_queue(priv->dev);
	netif_carrier_off(priv->dev);
54 55 56 57 58 59 60 61 62

	/* reset SNR/NF/RSSI values */
	memset(adapter->SNR, 0x00, sizeof(adapter->SNR));
	memset(adapter->NF, 0x00, sizeof(adapter->NF));
	memset(adapter->RSSI, 0x00, sizeof(adapter->RSSI));
	memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
	memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
	adapter->nextSNRNF = 0;
	adapter->numSNRNF = 0;
63
	lbs_deb_cmd("current SSID '%s', length %u\n",
64 65 66
	            escape_essid(adapter->curbssparams.ssid,
	                         adapter->curbssparams.ssid_len),
	            adapter->curbssparams.ssid_len);
67

68
	adapter->connect_status = LIBERTAS_DISCONNECTED;
69

70 71 72 73
	/* Clear out associated SSID and BSSID since connection is
	 * no longer valid.
	 */
	memset(&adapter->curbssparams.bssid, 0, ETH_ALEN);
74 75
	memset(&adapter->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
	adapter->curbssparams.ssid_len = 0;
76 77 78

	if (adapter->psstate != PS_STATE_FULL_POWER) {
		/* make firmware to exit PS mode */
79
		lbs_deb_cmd("disconnected, so exit PS mode\n");
80 81
		libertas_ps_wakeup(priv, 0);
	}
82
	lbs_deb_leave(LBS_DEB_CMD);
83 84 85 86 87 88 89 90 91 92 93 94 95
}

/**
 *  @brief This function handles MIC failure event.
 *
 *  @param priv    A pointer to wlan_private structure
 *  @para  event   the event id
 *  @return 	   n/a
 */
static void handle_mic_failureevent(wlan_private * priv, u32 event)
{
	char buf[50];

96
	lbs_deb_enter(LBS_DEB_CMD);
97 98 99 100 101 102 103 104 105 106 107
	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 ");
	}

	libertas_send_iwevcustom_event(priv, buf);
108
	lbs_deb_leave(LBS_DEB_CMD);
109 110 111 112 113
}

static int wlan_ret_reg_access(wlan_private * priv,
			       u16 type, struct cmd_ds_command *resp)
{
114
	int ret = 0;
115 116
	wlan_adapter *adapter = priv->adapter;

117
	lbs_deb_enter(LBS_DEB_CMD);
118 119

	switch (type) {
120
	case CMD_RET(CMD_MAC_REG_ACCESS):
121
		{
122
			struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
123

124 125
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			adapter->offsetvalue.value = le32_to_cpu(reg->value);
126 127 128
			break;
		}

129
	case CMD_RET(CMD_BBP_REG_ACCESS):
130
		{
131
			struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
132

133
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
134 135 136 137
			adapter->offsetvalue.value = reg->value;
			break;
		}

138
	case CMD_RET(CMD_RF_REG_ACCESS):
139
		{
140
			struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
141

142
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
143 144 145 146 147
			adapter->offsetvalue.value = reg->value;
			break;
		}

	default:
148
		ret = -1;
149 150
	}

151 152
	lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
	return ret;
153 154 155 156 157 158 159 160 161 162
}

static int wlan_ret_get_hw_spec(wlan_private * priv,
				struct cmd_ds_command *resp)
{
	u32 i;
	struct cmd_ds_get_hw_spec *hwspec = &resp->params.hwspec;
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

163
	lbs_deb_enter(LBS_DEB_CMD);
164 165 166

	adapter->fwcapinfo = le32_to_cpu(hwspec->fwcapinfo);

167
	memcpy(adapter->fwreleasenumber, hwspec->fwreleasenumber, 4);
168

169
	lbs_deb_cmd("GET_HW_SPEC: firmware release %u.%u.%up%u\n",
170 171
		    adapter->fwreleasenumber[2], adapter->fwreleasenumber[1],
		    adapter->fwreleasenumber[0], adapter->fwreleasenumber[3]);
172
	lbs_deb_cmd("GET_HW_SPEC: MAC addr " MAC_FMT "\n",
173
	       MAC_ARG(hwspec->permanentaddr));
174
	lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
175 176
	       hwspec->hwifversion, hwspec->version);

177 178 179 180 181
	/* Clamp region code to 8-bit since FW spec indicates that it should
	 * only ever be 8-bit, even though the field size is 16-bit.  Some firmware
	 * returns non-zero high 8 bits here.
	 */
	adapter->regioncode = le16_to_cpu(hwspec->regioncode) & 0xFF;
182 183 184 185 186 187 188 189 190 191 192

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

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

196 197
	if (adapter->current_addr[0] == 0xff)
		memmove(adapter->current_addr, hwspec->permanentaddr, ETH_ALEN);
198

199
	memcpy(priv->dev->dev_addr, adapter->current_addr, ETH_ALEN);
200
	if (priv->mesh_dev)
201
		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
202 203 204 205 206 207 208 209 210 211 212

	if (libertas_set_regiontable(priv, adapter->regioncode, 0)) {
		ret = -1;
		goto done;
	}

	if (libertas_set_universaltable(priv, 0)) {
		ret = -1;
		goto done;
	}

213 214
done:
	lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
215 216 217 218 219 220 221 222 223
	return ret;
}

static int wlan_ret_802_11_sleep_params(wlan_private * priv,
					struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_sleep_params *sp = &resp->params.sleep_params;
	wlan_adapter *adapter = priv->adapter;

224
	lbs_deb_enter(LBS_DEB_CMD);
225

226 227
	lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, calcontrol 0x%x "
		    "extsleepclk 0x%x\n", le16_to_cpu(sp->error),
228 229 230
		    le16_to_cpu(sp->offset), le16_to_cpu(sp->stabletime),
		    sp->calcontrol, sp->externalsleepclk);

231 232 233
	adapter->sp.sp_error = le16_to_cpu(sp->error);
	adapter->sp.sp_offset = le16_to_cpu(sp->offset);
	adapter->sp.sp_stabletime = le16_to_cpu(sp->stabletime);
234 235
	adapter->sp.sp_calcontrol = sp->calcontrol;
	adapter->sp.sp_extsleepclk = sp->externalsleepclk;
236 237
	adapter->sp.sp_reserved = le16_to_cpu(sp->reserved);

238
	lbs_deb_enter(LBS_DEB_CMD);
239 240 241 242 243 244
	return 0;
}

static int wlan_ret_802_11_stat(wlan_private * priv,
				struct cmd_ds_command *resp)
{
245
	lbs_deb_enter(LBS_DEB_CMD);
246 247 248 249 250 251 252 253 254
/*	currently adapter->wlan802_11Stat is unused

	struct cmd_ds_802_11_get_stat *p11Stat = &resp->params.gstat;
	wlan_adapter *adapter = priv->adapter;

	// TODO Convert it to Big endian befor copy
	memcpy(&adapter->wlan802_11Stat,
	       p11Stat, sizeof(struct cmd_ds_802_11_get_stat));
*/
255
	lbs_deb_leave(LBS_DEB_CMD);
256 257 258 259 260 261 262 263 264 265
	return 0;
}

static int wlan_ret_802_11_snmp_mib(wlan_private * priv,
				    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);

266
	lbs_deb_enter(LBS_DEB_CMD);
267

268
	lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
269
	       querytype);
270
	lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
271

272
	if (querytype == CMD_ACT_GET) {
273
		switch (oid) {
274
		case FRAGTHRESH_I:
275
			priv->adapter->fragthsd =
276
				le16_to_cpu(*((__le16 *)(smib->value)));
277
			lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
278
				    priv->adapter->fragthsd);
279
			break;
280
		case RTSTHRESH_I:
281
			priv->adapter->rtsthsd =
282
				le16_to_cpu(*((__le16 *)(smib->value)));
283
			lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
284
				    priv->adapter->rtsthsd);
285
			break;
286
		case SHORT_RETRYLIM_I:
287
			priv->adapter->txretrycount =
288
				le16_to_cpu(*((__le16 *)(smib->value)));
289
			lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
290
				    priv->adapter->rtsthsd);
291 292 293 294 295 296
			break;
		default:
			break;
		}
	}

297
	lbs_deb_enter(LBS_DEB_CMD);
298 299 300 301 302 303 304 305 306 307 308
	return 0;
}

static int wlan_ret_802_11_key_material(wlan_private * priv,
					struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_key_material *pkeymaterial =
	    &resp->params.keymaterial;
	wlan_adapter *adapter = priv->adapter;
	u16 action = le16_to_cpu(pkeymaterial->action);

309
	lbs_deb_enter(LBS_DEB_CMD);
310 311

	/* Copy the returned key to driver private data */
312
	if (action == CMD_ACT_GET) {
313 314 315 316 317 318
		u8 * buf_ptr = (u8 *) &pkeymaterial->keyParamSet;
		u8 * resp_end = (u8 *) (resp + le16_to_cpu(resp->size));

		while (buf_ptr < resp_end) {
			struct MrvlIEtype_keyParamSet * pkeyparamset =
			    (struct MrvlIEtype_keyParamSet *) buf_ptr;
319
			struct enc_key * pkey;
320 321
			u16 param_set_len = le16_to_cpu(pkeyparamset->length);
			u16 key_len = le16_to_cpu(pkeyparamset->keylen);
322 323 324
			u16 key_flags = le16_to_cpu(pkeyparamset->keyinfo);
			u16 key_type = le16_to_cpu(pkeyparamset->keytypeid);
			u8 * end;
325 326 327 328 329 330 331 332

			end = (u8 *) pkeyparamset + sizeof (pkeyparamset->type)
			                          + sizeof (pkeyparamset->length)
			                          + param_set_len;
			/* Make sure we don't access past the end of the IEs */
			if (end > resp_end)
				break;

333
			if (key_flags & KEY_INFO_WPA_UNICAST)
334
				pkey = &adapter->wpa_unicast_key;
335
			else if (key_flags & KEY_INFO_WPA_MCAST)
336 337 338 339 340
				pkey = &adapter->wpa_mcast_key;
			else
				break;

			/* Copy returned key into driver */
341
			memset(pkey, 0, sizeof(struct enc_key));
342 343
			if (key_len > sizeof(pkey->key))
				break;
344 345 346
			pkey->type = key_type;
			pkey->flags = key_flags;
			pkey->len = key_len;
347 348 349 350 351 352
			memcpy(pkey->key, pkeyparamset->key, pkey->len);

			buf_ptr = end + 1;
		}
	}

353
	lbs_deb_enter(LBS_DEB_CMD);
354 355 356 357 358 359 360 361 362
	return 0;
}

static int wlan_ret_802_11_mac_address(wlan_private * priv,
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;
	wlan_adapter *adapter = priv->adapter;

363
	lbs_deb_enter(LBS_DEB_CMD);
364 365 366

	memcpy(adapter->current_addr, macadd->macadd, ETH_ALEN);

367
	lbs_deb_enter(LBS_DEB_CMD);
368 369 370 371 372 373 374 375 376
	return 0;
}

static int wlan_ret_802_11_rf_tx_power(wlan_private * priv,
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
	wlan_adapter *adapter = priv->adapter;

377
	lbs_deb_enter(LBS_DEB_CMD);
378 379 380

	adapter->txpowerlevel = le16_to_cpu(rtp->currentlevel);

381
	lbs_deb_cmd("TX power currently %d\n", adapter->txpowerlevel);
382

383
	lbs_deb_leave(LBS_DEB_CMD);
384 385 386 387 388 389
	return 0;
}

static int wlan_ret_802_11_rate_adapt_rateset(wlan_private * priv,
					      struct cmd_ds_command *resp)
{
390
	struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
391 392
	wlan_adapter *adapter = priv->adapter;

393
	lbs_deb_enter(LBS_DEB_CMD);
394

395
	if (rates->action == CMD_ACT_GET) {
396 397
		adapter->enablehwauto = le16_to_cpu(rates->enablehwauto);
		adapter->ratebitmap = le16_to_cpu(rates->bitmap);
398 399
	}

400
	lbs_deb_leave(LBS_DEB_CMD);
401 402 403 404 405 406 407 408 409
	return 0;
}

static int wlan_ret_802_11_data_rate(wlan_private * priv,
				     struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_data_rate *pdatarate = &resp->params.drate;
	wlan_adapter *adapter = priv->adapter;

410
	lbs_deb_enter(LBS_DEB_CMD);
411

412
	lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) pdatarate,
413
		sizeof(struct cmd_ds_802_11_data_rate));
414

415 416 417 418
	/* FIXME: get actual rates FW can do if this command actually returns
	 * all data rates supported.
	 */
	adapter->cur_rate = libertas_fw_index_to_data_rate(pdatarate->rates[0]);
419
	lbs_deb_cmd("DATA_RATE: current rate 0x%02x\n", adapter->cur_rate);
420

421
	lbs_deb_leave(LBS_DEB_CMD);
422 423 424 425 426 427
	return 0;
}

static int wlan_ret_802_11_rf_channel(wlan_private * priv,
				      struct cmd_ds_command *resp)
{
428
	struct cmd_ds_802_11_rf_channel *rfchannel = &resp->params.rfchannel;
429 430 431 432
	wlan_adapter *adapter = priv->adapter;
	u16 action = le16_to_cpu(rfchannel->action);
	u16 newchannel = le16_to_cpu(rfchannel->currentchannel);

433
	lbs_deb_enter(LBS_DEB_CMD);
434

435
	if (action == CMD_OPT_802_11_RF_CHANNEL_GET
436
	    && adapter->curbssparams.channel != newchannel) {
437
		lbs_deb_cmd("channel switch from %d to %d\n",
438 439 440 441 442 443
		       adapter->curbssparams.channel, newchannel);

		/* Update the channel again */
		adapter->curbssparams.channel = newchannel;
	}

444
	lbs_deb_enter(LBS_DEB_CMD);
445 446 447 448 449 450 451 452 453
	return 0;
}

static int wlan_ret_802_11_rssi(wlan_private * priv,
				struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
	wlan_adapter *adapter = priv->adapter;

454 455
	lbs_deb_enter(LBS_DEB_CMD);

456 457
	/* store the non average value */
	adapter->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
458
	adapter->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
459 460

	adapter->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
461
	adapter->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
462 463 464 465 466 467 468 469 470

	adapter->RSSI[TYPE_BEACON][TYPE_NOAVG] =
	    CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
		     adapter->NF[TYPE_BEACON][TYPE_NOAVG]);

	adapter->RSSI[TYPE_BEACON][TYPE_AVG] =
	    CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
		     adapter->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);

471 472
	lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
	       adapter->RSSI[TYPE_BEACON][TYPE_NOAVG],
473 474
	       adapter->RSSI[TYPE_BEACON][TYPE_AVG]);

475
	lbs_deb_leave(LBS_DEB_CMD);
476 477 478 479 480 481 482 483 484 485
	return 0;
}

static int wlan_ret_802_11_eeprom_access(wlan_private * priv,
				  struct cmd_ds_command *resp)
{
	wlan_adapter *adapter = priv->adapter;
	struct wlan_ioctl_regrdwr *pbuf;
	pbuf = (struct wlan_ioctl_regrdwr *) adapter->prdeeprom;

486
	lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
487 488 489
	       le16_to_cpu(resp->params.rdeeprom.bytecount));
	if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
		pbuf->NOB = 0;
490
		lbs_deb_cmd("EEPROM read length too big\n");
491 492 493 494 495 496 497
		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));
498
		lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
499 500
			le16_to_cpu(resp->params.rdeeprom.bytecount));
	}
501
	lbs_deb_leave(LBS_DEB_CMD);
502 503 504 505 506 507
	return 0;
}

static int wlan_ret_get_log(wlan_private * priv,
			    struct cmd_ds_command *resp)
{
508
	struct cmd_ds_802_11_get_log *logmessage = &resp->params.glog;
509 510
	wlan_adapter *adapter = priv->adapter;

511
	lbs_deb_enter(LBS_DEB_CMD);
512

513 514
	/* Stored little-endian */
	memcpy(&adapter->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
515

516
	lbs_deb_leave(LBS_DEB_CMD);
517 518 519
	return 0;
}

520 521 522 523 524 525 526 527 528
static int libertas_ret_802_11_enable_rsn(wlan_private * priv,
                                          struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_enable_rsn *enable_rsn = &resp->params.enbrsn;
	wlan_adapter *adapter = priv->adapter;
	u32 * pdata_buf = adapter->cur_cmd->pdata_buf;

	lbs_deb_enter(LBS_DEB_CMD);

529
	if (enable_rsn->action == cpu_to_le16(CMD_ACT_GET)) {
530 531 532 533
		if (pdata_buf)
			*pdata_buf = (u32) le16_to_cpu(enable_rsn->enable);
	}

534
	lbs_deb_leave(LBS_DEB_CMD);
535 536 537
	return 0;
}

538 539 540 541 542 543 544 545
static inline int handle_cmd_response(u16 respcmd,
				      struct cmd_ds_command *resp,
				      wlan_private *priv)
{
	int ret = 0;
	unsigned long flags;
	wlan_adapter *adapter = priv->adapter;

546 547
	lbs_deb_enter(LBS_DEB_HOST);

548
	switch (respcmd) {
549 550 551
	case CMD_RET(CMD_MAC_REG_ACCESS):
	case CMD_RET(CMD_BBP_REG_ACCESS):
	case CMD_RET(CMD_RF_REG_ACCESS):
552 553 554
		ret = wlan_ret_reg_access(priv, respcmd, resp);
		break;

555
	case CMD_RET(CMD_GET_HW_SPEC):
556 557 558
		ret = wlan_ret_get_hw_spec(priv, resp);
		break;

559
	case CMD_RET(CMD_802_11_SCAN):
560 561 562
		ret = libertas_ret_80211_scan(priv, resp);
		break;

563
	case CMD_RET(CMD_802_11_GET_LOG):
564 565 566
		ret = wlan_ret_get_log(priv, resp);
		break;

567
	case CMD_RET_802_11_ASSOCIATE:
568 569
	case CMD_RET(CMD_802_11_ASSOCIATE):
	case CMD_RET(CMD_802_11_REASSOCIATE):
570 571 572
		ret = libertas_ret_80211_associate(priv, resp);
		break;

573 574
	case CMD_RET(CMD_802_11_DISASSOCIATE):
	case CMD_RET(CMD_802_11_DEAUTHENTICATE):
575 576 577
		ret = libertas_ret_80211_disassociate(priv, resp);
		break;

578 579
	case CMD_RET(CMD_802_11_AD_HOC_START):
	case CMD_RET(CMD_802_11_AD_HOC_JOIN):
580 581 582
		ret = libertas_ret_80211_ad_hoc_start(priv, resp);
		break;

583
	case CMD_RET(CMD_802_11_GET_STAT):
584 585 586
		ret = wlan_ret_802_11_stat(priv, resp);
		break;

587
	case CMD_RET(CMD_802_11_SNMP_MIB):
588 589 590
		ret = wlan_ret_802_11_snmp_mib(priv, resp);
		break;

591
	case CMD_RET(CMD_802_11_RF_TX_POWER):
592 593 594
		ret = wlan_ret_802_11_rf_tx_power(priv, resp);
		break;

595 596
	case CMD_RET(CMD_802_11_SET_AFC):
	case CMD_RET(CMD_802_11_GET_AFC):
597
		spin_lock_irqsave(&adapter->driver_lock, flags);
598
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.afc,
599 600 601 602 603
			sizeof(struct cmd_ds_802_11_afc));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		break;

604 605 606 607 608 609 610
	case CMD_RET(CMD_MAC_MULTICAST_ADR):
	case CMD_RET(CMD_MAC_CONTROL):
	case CMD_RET(CMD_802_11_SET_WEP):
	case CMD_RET(CMD_802_11_RESET):
	case CMD_RET(CMD_802_11_AUTHENTICATE):
	case CMD_RET(CMD_802_11_RADIO_CONTROL):
	case CMD_RET(CMD_802_11_BEACON_STOP):
611 612
		break;

613
	case CMD_RET(CMD_802_11_ENABLE_RSN):
614
		ret = libertas_ret_802_11_enable_rsn(priv, resp);
615 616
		break;

617
	case CMD_RET(CMD_802_11_DATA_RATE):
618 619
		ret = wlan_ret_802_11_data_rate(priv, resp);
		break;
620
	case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
621 622
		ret = wlan_ret_802_11_rate_adapt_rateset(priv, resp);
		break;
623
	case CMD_RET(CMD_802_11_RF_CHANNEL):
624 625 626
		ret = wlan_ret_802_11_rf_channel(priv, resp);
		break;

627
	case CMD_RET(CMD_802_11_RSSI):
628 629 630
		ret = wlan_ret_802_11_rssi(priv, resp);
		break;

631
	case CMD_RET(CMD_802_11_MAC_ADDRESS):
632 633 634
		ret = wlan_ret_802_11_mac_address(priv, resp);
		break;

635
	case CMD_RET(CMD_802_11_AD_HOC_STOP):
636 637 638
		ret = libertas_ret_80211_ad_hoc_stop(priv, resp);
		break;

639
	case CMD_RET(CMD_802_11_KEY_MATERIAL):
640 641 642
		ret = wlan_ret_802_11_key_material(priv, resp);
		break;

643
	case CMD_RET(CMD_802_11_EEPROM_ACCESS):
644 645 646
		ret = wlan_ret_802_11_eeprom_access(priv, resp);
		break;

647
	case CMD_RET(CMD_802_11D_DOMAIN_INFO):
648 649 650
		ret = libertas_ret_802_11d_domain_info(priv, resp);
		break;

651
	case CMD_RET(CMD_802_11_SLEEP_PARAMS):
652 653
		ret = wlan_ret_802_11_sleep_params(priv, resp);
		break;
654
	case CMD_RET(CMD_802_11_INACTIVITY_TIMEOUT):
655 656 657 658 659 660
		spin_lock_irqsave(&adapter->driver_lock, flags);
		*((u16 *) adapter->cur_cmd->pdata_buf) =
		    le16_to_cpu(resp->params.inactivity_timeout.timeout);
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;

661
	case CMD_RET(CMD_802_11_TPC_CFG):
662
		spin_lock_irqsave(&adapter->driver_lock, flags);
663
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.tpccfg,
664 665 666
			sizeof(struct cmd_ds_802_11_tpc_cfg));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
667
	case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
668
		spin_lock_irqsave(&adapter->driver_lock, flags);
669
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.ledgpio,
670 671 672
			sizeof(struct cmd_ds_802_11_led_ctrl));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
673
	case CMD_RET(CMD_802_11_PWR_CFG):
674
		spin_lock_irqsave(&adapter->driver_lock, flags);
675
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.pwrcfg,
676 677 678 679 680
			sizeof(struct cmd_ds_802_11_pwr_cfg));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		break;

681
	case CMD_RET(CMD_GET_TSF):
682 683 684 685 686
		spin_lock_irqsave(&adapter->driver_lock, flags);
		memcpy(priv->adapter->cur_cmd->pdata_buf,
		       &resp->params.gettsf.tsfvalue, sizeof(u64));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
687
	case CMD_RET(CMD_BT_ACCESS):
688 689 690 691 692 693
		spin_lock_irqsave(&adapter->driver_lock, flags);
		if (adapter->cur_cmd->pdata_buf)
			memcpy(adapter->cur_cmd->pdata_buf,
			       &resp->params.bt.addr1, 2 * ETH_ALEN);
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
694
	case CMD_RET(CMD_FWT_ACCESS):
695 696
		spin_lock_irqsave(&adapter->driver_lock, flags);
		if (adapter->cur_cmd->pdata_buf)
697 698
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.fwt,
			       sizeof(resp->params.fwt));
699 700
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
701
	case CMD_RET(CMD_MESH_ACCESS):
702
		if (adapter->cur_cmd->pdata_buf)
703
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.mesh,
704 705 706
			       sizeof(resp->params.mesh));
		break;
	default:
707
		lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
708
			    resp->command);
709 710
		break;
	}
711
	lbs_deb_leave(LBS_DEB_HOST);
712 713 714 715 716 717 718 719 720 721 722 723
	return ret;
}

int libertas_process_rx_command(wlan_private * priv)
{
	u16 respcmd;
	struct cmd_ds_command *resp;
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
	ulong flags;
	u16 result;

724
	lbs_deb_enter(LBS_DEB_HOST);
725 726 727 728 729 730 731 732

	/* Now we got response from FW, cancel the command timer */
	del_timer(&adapter->command_timer);

	mutex_lock(&adapter->lock);
	spin_lock_irqsave(&adapter->driver_lock, flags);

	if (!adapter->cur_cmd) {
733
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
734 735 736 737 738 739 740 741 742
		ret = -1;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		goto done;
	}
	resp = (struct cmd_ds_command *)(adapter->cur_cmd->bufvirtualaddr);

	respcmd = le16_to_cpu(resp->command);
	result = le16_to_cpu(resp->result);

743 744 745 746
	lbs_deb_host("CMD_RESP: response 0x%04x, size %d, jiffies %lu\n",
		respcmd, priv->upld_len, jiffies);
	lbs_deb_hex(LBS_DEB_HOST, "CMD_RESP", adapter->cur_cmd->bufvirtualaddr,
		    priv->upld_len);
747 748

	if (!(respcmd & 0x8000)) {
749
		lbs_deb_host("invalid response!\n");
750 751 752 753 754 755 756 757 758 759
		adapter->cur_cmd_retcode = -1;
		__libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		ret = -1;
		goto done;
	}

	/* Store the response code to cur_cmd_retcode. */
760
	adapter->cur_cmd_retcode = result;;
761

762
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
763 764
		struct cmd_ds_802_11_ps_mode *psmode = &resp->params.psmode;
		u16 action = le16_to_cpu(psmode->action);
765

766 767
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
768
		       result, action);
769 770

		if (result) {
771
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
772 773 774 775 776 777 778
				    result);
			/*
			 * We should not re-try enter-ps command in
			 * ad-hoc mode. It takes place in
			 * libertas_execute_next_command().
			 */
			if (adapter->mode == IW_MODE_ADHOC &&
779 780 781
			    action == CMD_SUBCMD_ENTER_PS)
				adapter->psmode = WLAN802_11POWERMODECAM;
		} else if (action == CMD_SUBCMD_ENTER_PS) {
782 783 784
			adapter->needtowakeup = 0;
			adapter->psstate = PS_STATE_AWAKE;

785
			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
786
			if (adapter->connect_status != LIBERTAS_CONNECTED) {
787 788 789 790
				/*
				 * When Deauth Event received before Enter_PS command
				 * response, We need to wake up the firmware.
				 */
791 792
				lbs_deb_host(
				       "disconnected, invoking libertas_ps_wakeup\n");
793 794

				spin_unlock_irqrestore(&adapter->driver_lock, flags);
795
				mutex_unlock(&adapter->lock);
796 797 798 799
				libertas_ps_wakeup(priv, 0);
				mutex_lock(&adapter->lock);
				spin_lock_irqsave(&adapter->driver_lock, flags);
			}
800
		} else if (action == CMD_SUBCMD_EXIT_PS) {
801 802
			adapter->needtowakeup = 0;
			adapter->psstate = PS_STATE_FULL_POWER;
803
			lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
804
		} else {
805
			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
806 807 808 809 810 811 812 813 814 815 816 817 818
		}

		__libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		ret = 0;
		goto done;
	}

	if (adapter->cur_cmd->cmdflags & CMD_F_HOSTCMD) {
		/* Copy the response back to response buffer */
819 820
		memcpy(adapter->cur_cmd->pdata_buf, resp,
		       le16_to_cpu(resp->size));
821 822 823 824 825
		adapter->cur_cmd->cmdflags &= ~CMD_F_HOSTCMD;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
826 827
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
828 829 830 831
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
832 833
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
834
			lbs_deb_host("CMD_RESP: reset failed\n");
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
			break;

		}

		__libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		ret = -1;
		goto done;
	}

	spin_unlock_irqrestore(&adapter->driver_lock, flags);

	ret = handle_cmd_response(respcmd, resp, priv);

	spin_lock_irqsave(&adapter->driver_lock, flags);
	if (adapter->cur_cmd) {
		/* Clean up and Put current command back to cmdfreeq */
		__libertas_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
		adapter->nr_cmd_pending--;
		WARN_ON(adapter->nr_cmd_pending > 128);
		adapter->cur_cmd = NULL;
	}
	spin_unlock_irqrestore(&adapter->driver_lock, flags);

done:
	mutex_unlock(&adapter->lock);
864
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
865 866 867 868 869 870 871 872 873
	return ret;
}

int libertas_process_event(wlan_private * priv)
{
	int ret = 0;
	wlan_adapter *adapter = priv->adapter;
	u32 eventcause;

874 875
	lbs_deb_enter(LBS_DEB_CMD);

876 877 878 879
	spin_lock_irq(&adapter->driver_lock);
	eventcause = adapter->eventcause;
	spin_unlock_irq(&adapter->driver_lock);

880
	lbs_deb_cmd("event cause 0x%x\n", eventcause);
881 882 883

	switch (eventcause >> SBI_EVENT_CAUSE_SHIFT) {
	case MACREG_INT_CODE_LINK_SENSED:
884
		lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
885 886 887
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
888
		lbs_deb_cmd("EVENT: deauthenticated\n");
889 890 891 892
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
893
		lbs_deb_cmd("EVENT: disassociated\n");
894 895 896 897
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_LINK_LOSE_NO_SCAN:
898
		lbs_deb_cmd("EVENT: link lost\n");
899 900 901 902
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_PS_SLEEP:
903
		lbs_deb_cmd("EVENT: sleep\n");
904 905 906

		/* handle unexpected PS SLEEP event */
		if (adapter->psstate == PS_STATE_FULL_POWER) {
907
			lbs_deb_cmd(
908
			       "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
909 910 911 912 913 914 915 916 917
			break;
		}
		adapter->psstate = PS_STATE_PRE_SLEEP;

		libertas_ps_confirm_sleep(priv, (u16) adapter->psmode);

		break;

	case MACREG_INT_CODE_PS_AWAKE:
918
		lbs_deb_cmd("EVENT: awake\n");
919 920 921

		/* handle unexpected PS AWAKE event */
		if (adapter->psstate == PS_STATE_FULL_POWER) {
922
			lbs_deb_cmd(
923 924 925 926 927 928 929 930 931 932 933 934 935
			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
			break;
		}

		adapter->psstate = PS_STATE_AWAKE;

		if (adapter->needtowakeup) {
			/*
			 * wait for the command processing to finish
			 * before resuming sending
			 * adapter->needtowakeup will be set to FALSE
			 * in libertas_ps_wakeup()
			 */
936
			lbs_deb_cmd("waking up ...\n");
937 938 939 940 941
			libertas_ps_wakeup(priv, 0);
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
942
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
943 944 945 946
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
947
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
948 949 950 951 952 953 954
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
		break;
	case MACREG_INT_CODE_MIB_CHANGED:
	case MACREG_INT_CODE_INIT_DONE:
		break;

	case MACREG_INT_CODE_ADHOC_BCN_LOST:
955
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
956 957 958
		break;

	case MACREG_INT_CODE_RSSI_LOW:
959
		lbs_pr_alert("EVENT: rssi low\n");
960 961
		break;
	case MACREG_INT_CODE_SNR_LOW:
962
		lbs_pr_alert("EVENT: snr low\n");
963 964
		break;
	case MACREG_INT_CODE_MAX_FAIL:
965
		lbs_pr_alert("EVENT: max fail\n");
966 967
		break;
	case MACREG_INT_CODE_RSSI_HIGH:
968
		lbs_pr_alert("EVENT: rssi high\n");
969 970
		break;
	case MACREG_INT_CODE_SNR_HIGH:
971
		lbs_pr_alert("EVENT: snr high\n");
972 973
		break;

974
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
975 976 977 978 979
		/* Ignore spurious autostart events if autostart is disabled */
		if (!priv->mesh_autostart_enabled) {
			lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
			break;
		}
980 981
		lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
		adapter->connect_status = LIBERTAS_CONNECTED;
982
		if (priv->mesh_open == 1) {
983 984
			netif_wake_queue(priv->mesh_dev);
			netif_carrier_on(priv->mesh_dev);
985
		}
986
		adapter->mode = IW_MODE_ADHOC;
987
		schedule_work(&priv->sync_channel);
988 989
		break;

990
	default:
991
		lbs_pr_alert("EVENT: unknown event id 0x%04x\n",
992 993 994 995 996 997 998
		       eventcause >> SBI_EVENT_CAUSE_SHIFT);
		break;
	}

	spin_lock_irq(&adapter->driver_lock);
	adapter->eventcause = 0;
	spin_unlock_irq(&adapter->driver_lock);
999

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