cmdresp.c 27.2 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 177 178 179 180 181 182 183 184 185 186 187 188
	       hwspec->hwifversion, hwspec->version);

	adapter->regioncode = le16_to_cpu(hwspec->regioncode);

	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;
189
		lbs_pr_info("unidentified region code; using the default (USA)\n");
190 191
	}

192 193
	if (adapter->current_addr[0] == 0xff)
		memmove(adapter->current_addr, hwspec->permanentaddr, ETH_ALEN);
194

195
	memcpy(priv->dev->dev_addr, adapter->current_addr, ETH_ALEN);
196
	if (priv->mesh_dev)
197
		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
198 199 200 201 202 203 204 205 206 207 208

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

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

209 210
done:
	lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
211 212 213 214 215 216 217 218 219
	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;

220
	lbs_deb_enter(LBS_DEB_CMD);
221

222 223
	lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, calcontrol 0x%x "
		    "extsleepclk 0x%x\n", le16_to_cpu(sp->error),
224 225 226
		    le16_to_cpu(sp->offset), le16_to_cpu(sp->stabletime),
		    sp->calcontrol, sp->externalsleepclk);

227 228 229
	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);
230 231
	adapter->sp.sp_calcontrol = sp->calcontrol;
	adapter->sp.sp_extsleepclk = sp->externalsleepclk;
232 233
	adapter->sp.sp_reserved = le16_to_cpu(sp->reserved);

234
	lbs_deb_enter(LBS_DEB_CMD);
235 236 237 238 239 240
	return 0;
}

static int wlan_ret_802_11_stat(wlan_private * priv,
				struct cmd_ds_command *resp)
{
241
	lbs_deb_enter(LBS_DEB_CMD);
242 243 244 245 246 247 248 249 250
/*	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));
*/
251
	lbs_deb_leave(LBS_DEB_CMD);
252 253 254 255 256 257 258 259 260 261
	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);

262
	lbs_deb_enter(LBS_DEB_CMD);
263

264
	lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
265
	       querytype);
266
	lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
267

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

293
	lbs_deb_enter(LBS_DEB_CMD);
294 295 296 297 298 299 300 301 302 303 304
	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);

305
	lbs_deb_enter(LBS_DEB_CMD);
306 307

	/* Copy the returned key to driver private data */
308
	if (action == CMD_ACT_GET) {
309 310 311 312 313 314
		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;
315
			struct enc_key * pkey;
316 317
			u16 param_set_len = le16_to_cpu(pkeyparamset->length);
			u16 key_len = le16_to_cpu(pkeyparamset->keylen);
318 319 320
			u16 key_flags = le16_to_cpu(pkeyparamset->keyinfo);
			u16 key_type = le16_to_cpu(pkeyparamset->keytypeid);
			u8 * end;
321 322 323 324 325 326 327 328

			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;

329
			if (key_flags & KEY_INFO_WPA_UNICAST)
330
				pkey = &adapter->wpa_unicast_key;
331
			else if (key_flags & KEY_INFO_WPA_MCAST)
332 333 334 335 336
				pkey = &adapter->wpa_mcast_key;
			else
				break;

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

			buf_ptr = end + 1;
		}
	}

349
	lbs_deb_enter(LBS_DEB_CMD);
350 351 352 353 354 355 356 357 358
	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;

359
	lbs_deb_enter(LBS_DEB_CMD);
360 361 362

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

363
	lbs_deb_enter(LBS_DEB_CMD);
364 365 366 367 368 369 370 371 372
	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;

373
	lbs_deb_enter(LBS_DEB_CMD);
374 375 376

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

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

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

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

389
	lbs_deb_enter(LBS_DEB_CMD);
390

391
	if (rates->action == CMD_ACT_GET) {
392 393
		adapter->enablehwauto = le16_to_cpu(rates->enablehwauto);
		adapter->ratebitmap = le16_to_cpu(rates->bitmap);
394 395
	}

396
	lbs_deb_leave(LBS_DEB_CMD);
397 398 399 400 401 402 403 404 405
	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;

406
	lbs_deb_enter(LBS_DEB_CMD);
407

408
	lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) pdatarate,
409
		sizeof(struct cmd_ds_802_11_data_rate));
410

411 412 413 414
	/* 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]);
415
	lbs_deb_cmd("DATA_RATE: current rate 0x%02x\n", adapter->cur_rate);
416

417
	lbs_deb_leave(LBS_DEB_CMD);
418 419 420 421 422 423
	return 0;
}

static int wlan_ret_802_11_rf_channel(wlan_private * priv,
				      struct cmd_ds_command *resp)
{
424
	struct cmd_ds_802_11_rf_channel *rfchannel = &resp->params.rfchannel;
425 426 427 428
	wlan_adapter *adapter = priv->adapter;
	u16 action = le16_to_cpu(rfchannel->action);
	u16 newchannel = le16_to_cpu(rfchannel->currentchannel);

429
	lbs_deb_enter(LBS_DEB_CMD);
430

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

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

440
	lbs_deb_enter(LBS_DEB_CMD);
441 442 443 444 445 446 447 448 449
	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;

450 451
	lbs_deb_enter(LBS_DEB_CMD);

452 453
	/* store the non average value */
	adapter->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
454
	adapter->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
455 456

	adapter->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
457
	adapter->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
458 459 460 461 462 463 464 465 466

	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);

467 468
	lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
	       adapter->RSSI[TYPE_BEACON][TYPE_NOAVG],
469 470
	       adapter->RSSI[TYPE_BEACON][TYPE_AVG]);

471
	lbs_deb_leave(LBS_DEB_CMD);
472 473 474 475 476 477 478 479 480 481
	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;

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

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

507
	lbs_deb_enter(LBS_DEB_CMD);
508

509 510
	/* Stored little-endian */
	memcpy(&adapter->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
511

512
	lbs_deb_leave(LBS_DEB_CMD);
513 514 515
	return 0;
}

516 517 518 519 520 521 522 523 524
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);

525
	if (enable_rsn->action == cpu_to_le16(CMD_ACT_GET)) {
526 527 528 529
		if (pdata_buf)
			*pdata_buf = (u32) le16_to_cpu(enable_rsn->enable);
	}

530
	lbs_deb_leave(LBS_DEB_CMD);
531 532 533
	return 0;
}

534 535 536 537 538 539 540 541
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;

542 543
	lbs_deb_enter(LBS_DEB_HOST);

544
	switch (respcmd) {
545 546 547
	case CMD_RET(CMD_MAC_REG_ACCESS):
	case CMD_RET(CMD_BBP_REG_ACCESS):
	case CMD_RET(CMD_RF_REG_ACCESS):
548 549 550
		ret = wlan_ret_reg_access(priv, respcmd, resp);
		break;

551
	case CMD_RET(CMD_GET_HW_SPEC):
552 553 554
		ret = wlan_ret_get_hw_spec(priv, resp);
		break;

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

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

563
	case CMD_RET_802_11_ASSOCIATE:
564 565
	case CMD_RET(CMD_802_11_ASSOCIATE):
	case CMD_RET(CMD_802_11_REASSOCIATE):
566 567 568
		ret = libertas_ret_80211_associate(priv, resp);
		break;

569 570
	case CMD_RET(CMD_802_11_DISASSOCIATE):
	case CMD_RET(CMD_802_11_DEAUTHENTICATE):
571 572 573
		ret = libertas_ret_80211_disassociate(priv, resp);
		break;

574 575
	case CMD_RET(CMD_802_11_AD_HOC_START):
	case CMD_RET(CMD_802_11_AD_HOC_JOIN):
576 577 578
		ret = libertas_ret_80211_ad_hoc_start(priv, resp);
		break;

579
	case CMD_RET(CMD_802_11_GET_STAT):
580 581 582
		ret = wlan_ret_802_11_stat(priv, resp);
		break;

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

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

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

		break;

600 601 602 603 604 605 606
	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):
607 608
		break;

609
	case CMD_RET(CMD_802_11_ENABLE_RSN):
610
		ret = libertas_ret_802_11_enable_rsn(priv, resp);
611 612
		break;

613
	case CMD_RET(CMD_802_11_DATA_RATE):
614 615
		ret = wlan_ret_802_11_data_rate(priv, resp);
		break;
616
	case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
617 618
		ret = wlan_ret_802_11_rate_adapt_rateset(priv, resp);
		break;
619
	case CMD_RET(CMD_802_11_RF_CHANNEL):
620 621 622
		ret = wlan_ret_802_11_rf_channel(priv, resp);
		break;

623
	case CMD_RET(CMD_802_11_RSSI):
624 625 626
		ret = wlan_ret_802_11_rssi(priv, resp);
		break;

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

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

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

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

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

647
	case CMD_RET(CMD_802_11_SLEEP_PARAMS):
648 649
		ret = wlan_ret_802_11_sleep_params(priv, resp);
		break;
650
	case CMD_RET(CMD_802_11_INACTIVITY_TIMEOUT):
651 652 653 654 655 656
		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;

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

		break;

677
	case CMD_RET(CMD_GET_TSF):
678 679 680 681 682
		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;
683
	case CMD_RET(CMD_BT_ACCESS):
684 685 686 687 688 689
		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;
690
	case CMD_RET(CMD_FWT_ACCESS):
691 692
		spin_lock_irqsave(&adapter->driver_lock, flags);
		if (adapter->cur_cmd->pdata_buf)
693 694
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.fwt,
			       sizeof(resp->params.fwt));
695 696
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
697
	case CMD_RET(CMD_MESH_ACCESS):
698
		if (adapter->cur_cmd->pdata_buf)
699
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.mesh,
700 701 702
			       sizeof(resp->params.mesh));
		break;
	default:
703
		lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
704
			    resp->command);
705 706
		break;
	}
707
	lbs_deb_leave(LBS_DEB_HOST);
708 709 710 711 712 713 714 715 716 717 718 719
	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;

720
	lbs_deb_enter(LBS_DEB_HOST);
721 722 723 724 725 726 727 728

	/* 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) {
729
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
730 731 732 733 734 735 736 737 738
		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);

739 740 741 742
	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);
743 744

	if (!(respcmd & 0x8000)) {
745
		lbs_deb_host("invalid response!\n");
746 747 748 749 750 751 752 753 754 755
		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. */
756
	adapter->cur_cmd_retcode = result;;
757

758
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
759 760
		struct cmd_ds_802_11_ps_mode *psmode = &resp->params.psmode;
		u16 action = le16_to_cpu(psmode->action);
761

762 763
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
764
		       result, action);
765 766

		if (result) {
767
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
768 769 770 771 772 773 774
				    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 &&
775 776 777
			    action == CMD_SUBCMD_ENTER_PS)
				adapter->psmode = WLAN802_11POWERMODECAM;
		} else if (action == CMD_SUBCMD_ENTER_PS) {
778 779 780
			adapter->needtowakeup = 0;
			adapter->psstate = PS_STATE_AWAKE;

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

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

		__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 */
815 816
		memcpy(adapter->cur_cmd->pdata_buf, resp,
		       le16_to_cpu(resp->size));
817 818 819 820 821
		adapter->cur_cmd->cmdflags &= ~CMD_F_HOSTCMD;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
822 823
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
824 825 826 827
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
828 829
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
830
			lbs_deb_host("CMD_RESP: reset failed\n");
831 832 833 834 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
			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);
860
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
861 862 863 864 865 866 867 868 869
	return ret;
}

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

870 871
	lbs_deb_enter(LBS_DEB_CMD);

872 873 874 875
	spin_lock_irq(&adapter->driver_lock);
	eventcause = adapter->eventcause;
	spin_unlock_irq(&adapter->driver_lock);

876
	lbs_deb_cmd("event cause 0x%x\n", eventcause);
877 878 879

	switch (eventcause >> SBI_EVENT_CAUSE_SHIFT) {
	case MACREG_INT_CODE_LINK_SENSED:
880
		lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
881 882 883
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
884
		lbs_deb_cmd("EVENT: deauthenticated\n");
885 886 887 888
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
889
		lbs_deb_cmd("EVENT: disassociated\n");
890 891 892 893
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_LINK_LOSE_NO_SCAN:
894
		lbs_deb_cmd("EVENT: link lost\n");
895 896 897 898
		libertas_mac_event_disconnected(priv);
		break;

	case MACREG_INT_CODE_PS_SLEEP:
899
		lbs_deb_cmd("EVENT: sleep\n");
900 901 902

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

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

		break;

	case MACREG_INT_CODE_PS_AWAKE:
914
		lbs_deb_cmd("EVENT: awake\n");
915 916 917

		/* handle unexpected PS AWAKE event */
		if (adapter->psstate == PS_STATE_FULL_POWER) {
918
			lbs_deb_cmd(
919 920 921 922 923 924 925 926 927 928 929 930 931
			       "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()
			 */
932
			lbs_deb_cmd("waking up ...\n");
933 934 935 936 937
			libertas_ps_wakeup(priv, 0);
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
938
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
939 940 941 942
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
943
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
944 945 946 947 948 949 950
		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:
951
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
952 953 954
		break;

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

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

986
	default:
987
		lbs_pr_alert("EVENT: unknown event id 0x%04x\n",
988 989 990 991 992 993 994
		       eventcause >> SBI_EVENT_CAUSE_SHIFT);
		break;
	}

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

996
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
997 998
	return ret;
}