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 63

	/* 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;
	adapter->rxpd_rate = 0;
64
	lbs_deb_cmd("current SSID '%s', length %u\n",
65 66 67
	            escape_essid(adapter->curbssparams.ssid,
	                         adapter->curbssparams.ssid_len),
	            adapter->curbssparams.ssid_len);
68

69
	adapter->connect_status = LIBERTAS_DISCONNECTED;
70

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

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

/**
 *  @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];

97
	lbs_deb_enter(LBS_DEB_CMD);
98 99 100 101 102 103 104 105 106 107 108
	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);
109
	lbs_deb_leave(LBS_DEB_CMD);
110 111 112 113 114
}

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

118
	lbs_deb_enter(LBS_DEB_CMD);
119 120

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

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

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

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

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

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

	default:
149
		ret = -1;
150 151
	}

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

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;

164
	lbs_deb_enter(LBS_DEB_CMD);
165 166 167

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

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

170
	lbs_deb_cmd("GET_HW_SPEC: firmware release %u.%u.%up%u\n",
171 172
		    adapter->fwreleasenumber[2], adapter->fwreleasenumber[1],
		    adapter->fwreleasenumber[0], adapter->fwreleasenumber[3]);
173
	lbs_deb_cmd("GET_HW_SPEC: MAC addr " MAC_FMT "\n",
174
	       MAC_ARG(hwspec->permanentaddr));
175
	lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	       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]) {
			adapter->regiontableindex = (u16) i;
			break;
		}
	}

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

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

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

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

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

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

223
	lbs_deb_enter(LBS_DEB_CMD);
224

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

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

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

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

265
	lbs_deb_enter(LBS_DEB_CMD);
266

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

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

296
	lbs_deb_enter(LBS_DEB_CMD);
297 298 299 300 301 302 303 304 305 306 307
	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);

308
	lbs_deb_enter(LBS_DEB_CMD);
309 310

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

			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;

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

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

			buf_ptr = end + 1;
		}
	}

352
	lbs_deb_enter(LBS_DEB_CMD);
353 354 355 356 357 358 359 360 361
	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;

362
	lbs_deb_enter(LBS_DEB_CMD);
363 364 365

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

366
	lbs_deb_enter(LBS_DEB_CMD);
367 368 369 370 371 372 373 374 375
	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;

376
	lbs_deb_enter(LBS_DEB_CMD);
377 378 379

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

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

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

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

392
	lbs_deb_enter(LBS_DEB_CMD);
393

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

399
	lbs_deb_leave(LBS_DEB_CMD);
400 401 402 403 404 405 406 407 408
	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;

409
	lbs_deb_enter(LBS_DEB_CMD);
410

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

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

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

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

432
	lbs_deb_enter(LBS_DEB_CMD);
433

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

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

443
	lbs_deb_enter(LBS_DEB_CMD);
444 445 446 447 448 449 450 451 452
	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;

453 454
	lbs_deb_enter(LBS_DEB_CMD);

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

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

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

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

474
	lbs_deb_leave(LBS_DEB_CMD);
475 476 477 478 479 480 481 482 483 484
	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;

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

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

510
	lbs_deb_enter(LBS_DEB_CMD);
511

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

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

519 520 521 522 523 524 525 526 527
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);

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

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

537 538 539 540 541 542 543 544
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;

545 546
	lbs_deb_enter(LBS_DEB_HOST);

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

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

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

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

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

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

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

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

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

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

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

		break;

603 604 605 606 607 608 609
	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):
610 611
		break;

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

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

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

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

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

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

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

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

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

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

		break;

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

726
	lbs_deb_enter(LBS_DEB_HOST);
727 728 729 730 731 732 733 734

	/* 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) {
735
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
736 737 738 739 740 741 742 743 744
		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);

745 746 747 748
	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);
749 750

	if (!(respcmd & 0x8000)) {
751
		lbs_deb_host("invalid response!\n");
752 753 754 755 756 757 758 759 760 761
		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. */
762
	adapter->cur_cmd_retcode = result;;
763

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

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

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

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

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

		__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 */
		memcpy(adapter->cur_cmd->pdata_buf, resp, resp->size);

		adapter->cur_cmd->cmdflags &= ~CMD_F_HOSTCMD;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
828 829
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
830 831 832 833
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
834 835
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
836
			lbs_deb_host("CMD_RESP: reset failed\n");
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 864 865
			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);
866
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
867 868 869 870 871 872 873 874 875 876 877 878 879
	return ret;
}

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

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

880
	lbs_deb_enter(LBS_DEB_CMD);
881

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

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

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

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

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

	case MACREG_INT_CODE_PS_SLEEP:
905
		lbs_deb_cmd("EVENT: sleep\n");
906 907 908

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

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

		break;

	case MACREG_INT_CODE_PS_AWAKE:
920
		lbs_deb_cmd("EVENT: awake\n");
921 922 923

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

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

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
949
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
950 951 952 953 954 955 956
		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:
957
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
958 959 960
		break;

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

976
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
977 978
		lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
		adapter->connect_status = LIBERTAS_CONNECTED;
979
		if (priv->mesh_open == 1) {
980 981
			netif_wake_queue(priv->mesh_dev);
			netif_carrier_on(priv->mesh_dev);
982
		}
983
		adapter->mode = IW_MODE_ADHOC;
984
		schedule_work(&priv->sync_channel);
985 986
		break;

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

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

	lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
998 999
	return ret;
}